Hello~ Anybody here help me ?
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
while1: pinTrigger = machine.Pin(5, machine.Pin.OUT)
#defining the pins
pinEcho = machine.Pin(4, machine.Pin.IN)
#defining the pins
pinTrigger.high()
time.sleep(0.00001)
pinTrigger.low()
time.sleep(0.5)
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) sys.exit()
I have found more information like how HC-SR04 works from google, but I still can’t solve the problem, please help, appreciated.
Hey,
I do not know anything about the NodeMCU board. That being said, it seems your code differs from the selected answer on the stack overflow questions you linked to:
[code]import machine
import utime
while True:
trig=machine.Pin(5, machine.Pin.OUT)
trig.off()//stop reading
utime.sleep_us(2)
trig.on()
utime.sleep_us(10)
trig.off()
echo=machine.Pin(4, machine.Pin.IN)
while echo.value() == 0:
pass
t1 = utime.ticks_us()
while echo.value() == 1:
pass
t2 = utime.ticks_us()
cm = (t2 - t1) / 58.0
print(cm)
utime.sleep(2)[/code]The OP mentions this solved their issue, so I’d recommend trying that out first.
If this still does not work, you may want to verify you have everything wired and powered properly. It is quite common to look in software for an issue that can be as simple as inverted signal wires!
With a quick online search, you can find many other examples online for the NodeMCU (and compatible boards) and this ultrasonic “ping” sensor with the Arduino IDE, LUA, MicroPython, CircuitPython, etc.:
]instructables.com/id/Distance-Measurement-Using-HC-SR04-Via-NodeMCU//:m]
]14core.com/wiring-esp8266-nodemcu-with-hcsr04-ultrasonic-sensor//:m]
]github.com/vsserafim/hcsr04-nodemcu/:m]
]github.com/sza2/node_hcsr04/:m]
]github.com/rsc1975/micropython-hcsr04/:m]
]github.com/mithru/MicroPython-Examples/blob/master/08.Sensors/HC-SR04/HC-SR04.py/:m]
]circuitpython-hcsr04.readthedocs.io/en/latest//:m]
]itechnofrance.wordpress.com/2018/02/21/micropython-et-le-capteur-de-distance-ultrason-hc-sr04//:m]
]lemariva.com/blog/2018/06/tutorial-getting-started-with-micropython-sensors/:m]
]forum.pycom.io/topic/886/lopy-hc-sr04-adaptation/:m]
Please note that we did not review in depth any of those examples, but they should serve as a good starting point on the many ways to go about this. If you need assistance with any of these, we recommend to contact their maintainer or creator.
We hope this helps.
Sincerely,