Johan10 - 2018-04-14

Hello everyone, I’ve made a sensor for my Robot to detect obstacles. Here is my schematic connexion, and python code. But does someone knows how to make it work with Codesys in ST langage? Thank’s a lot. I’ll be very grateful for your help.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

print "+-----------------------------------------------------------+"
print "| Mesure de distance par le capteur ultrasonore HC-SR04 |"
print "+-----------------------------------------------------------+"

Trig = 23 # Entree Trig du HC-SR04 branchee au GPIO 23 #trig input
Echo = 24 # Sortie Echo du HC-SR04 branchee au GPIO 24 #echo output

GPIO.setup(Trig,GPIO.OUT)
GPIO.setup(Echo,GPIO.IN)

GPIO.output(Trig, False)

repet = input("Entrez un nombre de repetitions de mesure : ") #Enter a number of repetition

for x in range(repet): # On prend la mesure "repet" fois #we take mesure « repet » time

time.sleep(1) # On la prend toute les 1 seconde #we take it each 1sec

GPIO.output(Trig, True)
time.sleep(0.00001)
GPIO.output(Trig, False)

while GPIO.input(Echo)==0: ## Emission de l'ultrason #Emits Ultrasonic
debutImpulsion = time.time()

while GPIO.input(Echo)==1: ## Retour de l'Echo # Return Echo
finImpulsion = time.time()

distance = round((finImpulsion - debutImpulsion) * 340 * 100 / 2, 1) ## Vitesse du son (sound speed)= 340 m/s

print "La distance est de : ",distance," cm" #the distance is

GPIO.cleanup()

IMG: 446C6252