Ultrasonic Sensor HC-SR04
Hcsr04 can measure distance between an active range of 2 cm to 4 meters. Hcsr04 requires 5 volts and 15 mA of power for operation. Hcsr04 has four pins. Two are power pins. Vcc is +ve pin apply 5v to this pin and Gnd is ground pin connect -ve of 5v power source with it. The other two pins are Trigger and Echo.
- Trigger pin is triggered by external controller to out burst an ultrasound wave.
- Echo pin notifies external controller when receiver receives back the bounced back wave.
Module price range in between $1.5 from china to $3.95 from sparkfun.
Nodemcu esp8266 and Hcsr04 ultrasonic sensor power constraints
Project circuit diagram
Trigger and echo pins of hcsr04 ultrasonic sensor is directly connected to GPIO-2 and GPIO-0 or D4 and D3 pins of nodemcu. You people might have a question here why pins are directly connected if voltage levels of both devices are different. The answer is simple, trigger and echo pins output can easily be read by 3.3 volt devices. It worked for me with out any problem and the diy project is running continuously from 72 hours. If it did not work for you then you might insert a logic converter between the two modules. Rest of the connections can be seen in the circuit diagram given below.
Where Speed = speed of sound in air. Which is 340 m/s
Hence reorganizing the formula Distance= Time * 340(speed of sound in air).
The above formula is used in the below code to measure distance from time. At the beginning of the code i defined the nodemcu pins (trigP,echoP) interacting with the hcsr04 ultrasonic sensor. Onward two variables(distance,duration) are defined for storing the time and distance values. In the setup function trigger pin is declared output and echo pin is declared input. Serial communication of nodemcu is also initialized in setup function at 9600 baud rate.
In the loop function trigger pin remain low for 2 seconds after 2 seconds it is made high for 10 us. 10us is time in which the trigger pin sends an output ultrasound signal in 8 cycles. Then the pulse in function reads the bounce back waves and approximates the time. After wards the statement
Pulse in function used in the code is an old function used to measure distance with hcsr04 ultrasonic sensor. This function poorly performs with some ultrasonic modules especially which are old or whose control circuit is out of date. This function is also slow in measuring time. A new and fast library is released by arduino to work with ultrasonic sensors. The NewPing Library. This library utilizes internal arduino timers and it did not compile in arduino ide when used for nodemcu. So we can not use NewPing library with nodemcu.
Future Work
I just only showed how to interface hcsr04 ultrasonic sensor with nodemcu esp8266. One can bring upper project in practical functionality by making an automatic and intelligent garage car parking shed control system with it. Shed moves up when car comes home and goes down when the car is in and vise versa. Gsm can be made part of the project to send an sms alert to a house hold individual about car arrival. Nodemcu can be connected to WiFi and incoming car alert can be send to an online server which is maintaining a parking log.
Filed Under: Electronic Projects, ESP8266, Microcontroller Projects
Questions related to this article?
đAsk and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.