HC-SR04 Ultrasonic Sensor troubles

5 years 9 months ago #29533976 by circuit12
Good day,

I am trying to get the NodeMCU ESP8266 dev board to work with an HC-SR04 Ultrasonic Sensor. I keep getting a reading of "0", which is not the reality.I have the ground and 5v pins of the sensor hooked up to a 5v source, and the Echo and Trigger pins on GPIO pin 4 and 5. In theory, everything should be working, but I just keep getting "0". Perhaps there is something wrong with my code? See below:
import machine
import sys
import time
time.sleep(1) #Just for everything to settle down

while 1:

pinTrigger = machine.Pin(5, machine.Pin.OUT) #defining the pins
pinEcho = machine.Pin(4, machine.Pin.IN) #defining the pins



start = time.time()
stop = time.time()

while pinEcho ==0:
start = time.time() #Starting the time when sending out the signal

while pinTrigger ==1:
stop = time.time() #Stopping the time when the signal comes back

elapsed = stop-start #working out the time.

distance = elapsed * 34000 #multiply by speed of sound to get distance

distance = distance / 2 #divide by 2 becuase it was there and back

print ("Distance : %.1f" % distance)

I have found more information like how HC-SR04 works( www.hqew.net/news/news-33872https://www.....net/news/news-33872 ) from google, but I still can't solve the problem, please help, appreciated.

Please Log in or Create an account to join the conversation.

5 years 9 months ago #29533978 by Gabriel_Isko
Replied by Gabriel_Isko on topic HC-SR04 Ultrasonic Sensor troubles
Hey, unfortunately I am unfamiliar with that dev board. We recommend using the Parrallax PING sensor as a ultrasonic distance sensor with our motor controllers through a pulse input.

Please Log in or Create an account to join the conversation.

Moderators: tonysantoni
Time to create page: 0.083 seconds