I’m trying to connect the HC-SR04 Sensor to a robot for distance calculation. I keep getting that the distance is 0.0 cm for some reason. The code is below. VCC is connected to 5V, GND to GND, TRIG to P9 and ECHO to P4.
import sensor, image, time, car, pyb, utime
from pid import PID
from pyb import Pin, Timer
def dist() :
trig = Pin('P9', Pin.OUT_PP) #change as needed
echo = Pin('P4', Pin.IN, Pin.PULL_DOWN)
pulse_start = 0
pulse_end = 0
pulse_dur = 0
trig.low()
pyb.udelay(5)
trig.high()
pyb.udelay(10)
trig.low()
while echo == 0:
pulse_start = pyb.millis()
print("Pulse start: ",pulse_start,"ms")
while echo == 1:
pulse_end = pyb.elapsed_millis(pulse_start)
pulse_dur = pulse_end - pulse_start
print("Pulse_dur: ", pulse_dur,"ms")
pulse_dur = float(pulse_dur)
distance = (pulse_dur * 34300) / 2
#distance = round(distance, 2)
return distance
desired_dist = 15 #centimeters roughly 6 inches
dist_pid = PID(p =1, i = .5, d = .25, imax = 100)
while(1) :
actual_dist = dist() #find distance
print("Distance: ", actual_dist, "cm")
dist_error = desired_dist - actual_dist # calc error
print("Error: ", dist_error, "cm")
dist_output = dist_pid.get_pid(dist_error, 1) #use pid
if dist_output > 100:
dist_output = 100
print("PWM: ", dist_output, "%")
car.run(dist_output, dist_output) #drive the car
pyb.delay(1000)
Hi, you’d have to give us some pointer to issues with our code. We can’t debug a random sensor we don’t have driver support for.
Is there a specific thing going wrong?
There isn’t anything specific that I can tell, just curious why this code which was translated from Arduino to work with the H7. Don’t have access to an oscilloscope to verify that the outputs and inputs to the sensor are functioning correctly, but the code should work from what I can tell.
All code should work. Please try to debug it line by line and let me know when you have a particular question about something not working like you expect.
Do you connect it to an external 5V output ? The camera doesn’t have a 5V output, also if the sensor’s I/O is 5V it won’t work.
HugoMDM
September 20, 2022, 11:24pm
6
Hi, i’ve modified your code and i got a decent result, probably you need to adjust something.
It’s working for me
import sensor, image, time, pyb, utime
from pyb import Pin, Timer
def distancia():
trig = Pin('P4', Pin.OUT_PP)
echo = Pin('P5', Pin.IN, Pin.PULL_DOWN)
pulse_start = 0
pulse_end = 0
pulse_dur = 0
trig.value(1)
pyb.udelay(5)
trig.value(0)
pyb.udelay(10)
trig.value(1)
limit=20000 #this is to prevent lag
timer= pyb.micros()
while echo.value() == 0 and timer+limit>pyb.micros():
pulse_start = pyb.micros()
timer= pyb.micros()
while echo.value() == 1 and timer+limit>pyb.micros():
pulse_end = pyb.elapsed_micros(pulse_start)
pulse_dur = float(pulse_end)
distance = (pulse_dur) / 50 #Here you can calibrate the sensor
if distance>300: #max distance 300, if want higher aslo modify the limit variable
distance=300
return distance
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False,5000)
while True:
img=sensor.snapshot()
print(distancia())