Micropython HC-SR04 - ESP8266

Internet of Things IoT Frameworks 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ 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 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() Please help.

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ Take a look at this library to get it easier to work with HC-SR04 Sensor driver in micropython.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links