Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Ultrasonic sensor interfaced with nodemcu esp8266 12e for distance measurement

By EG Projects April 10, 2019

This is a simple tutorial on interfacing ultrasonic sensor with nodemcu WiFi module and measure distance. Recently i made an internet of things project which involves an ultrasonic sensor and nodemcu WiFi module in it. Ultrasonic sensor which is used in the project is the most popular one among the diy circuit community, the hcsr04 ultrasonic sensor. You can find many tutorials on internet on how to interface hcsr04 ultrasonic sensor with arduino and other different microcontrollers? But interfacing hcsr04 ultrasonic sensor with nodemcu requires little more effort in understanding power requirements of the two modules. Numerous tutorials are present on internet about interfacing the popular hcsr04 ultrasonic sensor with nodemcu but not a single one worked for me, also many people in comments complaint about the circuit power constraints. So in this tutorial i will elaborate the power requirements of the two modules and how to interface them properly.      
Picture

HCSR04 ultrasonic sensor measuring distance

Ultrasonic Sensor HC-SR04

Hcsr04 ultrasonic sensor is composed of ultrasonic transmitter, ultrasonic receiver and a control circuit. Hscr04 ultrasonic transmitter transmits ultrasound waves at 40,000 Hz. Transmitted waves bounce back if they hit any flat surface/object in their path. Bounced back waves reaches the ultrasonic receiver. Ultrasonic receiver receives the bounced back waves and notifies the control circuit about it. Control circuit than calculates the time taken by waves to reach back after transmission. Time is than manipulated to approximate the distance traveled by waves or what is the distance between the sensor and the object? from which ultrasound waves bounced back.  
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

Nodemcu esp8266 12e works on 3.3 volts and its pins are also 3.3v TTL compatible. Where as ultrasonic sensor Hcsr04 works on 5 volts. We can not power Hcsr04 directly with nodemcu 3.3v power output pins. Many tutorials available online on this topic did this mistake and engineers 😀 are complaining about them. Their are some new Hcsr04 ultrasonic sensor modules coming from china which can work on 3.3 volts. If you have one then you can directly interface it with nodemcu. 

Project circuit diagram

I used a cheap power supply which can output 5 volts as well as 3.3 volts. I bought it online from china. 5 volts side of power supply is supplying power to hcsr04 and 3.3 volt side is used to power nodemcu. Note the grounds of 3.3v and 5v are made common.
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.  
Ultrasonic sensor HC-SR04 with nodemcu esp8266 WiFi module

Ultrasonic sensor HC-SR04 with nodemcu esp8266 WiFi module

Coming to project code. Code is written in arduino ide. It is open source you can use and modify it according to your needs. Before using the code please be sure first, you have installed the nodemcu esp8266 support package for arduino ide. If you haven’t then first install it. Lets move to wards the ultrasonic distance calculation before starting with  code statements. Ultrasonic sensor did not calculate the distance by it self. Rather it only grabs the time taken by waves to leave transmitter and bounce back to receiver. Time is converted in to distance using the speed of sound in air formula. According to universal speed of sound in air formula
                                                                            Time = Distance / Speed
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 
distance= duration*0.034/2 calculates the real distance between the ultrasonic sensor and object. If you cant understand the 10 us and other parameters like pulse in function i advise you to go with this tutorial it will explain each parameter graphically and mathematically with real world example.         

Build the circuit on bread board and upload the code in nodemcu. After uploading the code in nodemcu open the arduino ide serial monitor. Be sure your nodemcu is connected with your PC and right port and board is selected in the arduino ide. As soon as you open the serial monitor you will see the distance readings appearing in the arduino serial monitor window. Place some object in front of the ultrasonic sensor and measure the distance manually with scale. Than watch for the readings in arduino serial monitor both must be same. If not same must be in 3 mm(Hcsr04 error ratio) apart from the actual/manual reading. Results window is shown below.     
Ultrasonic HC-SR04 output on arduino serial monitor with nodemcu esp8266 12e

Ultrasonic HC-SR04 output on arduino serial monitor with nodemcu esp8266 12e

Attention
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.      

Download the project code. Folder contains the project arduino ide .ino file. Please provide us your feed back on the project. If you have any queries write them below in the comments section.
Code/Files


Filed Under: Electronic Projects, ESP8266, Microcontroller Projects

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • The Analog Gods Hate Me
  • is there vay for credit card size phone charger?
  • Innovus Scan Reorder deletes Scan In Pad
  • How to get 3D model of ACPL-847-300E
  • How to find the resonance frequency and impedance of a planar spiral coil in HFSS?

RSS Electro-Tech-Online.com Discussions

  • Fixing board, Easy question HEX SCHMITT
  • Can I make two inputs from one??
  • The Analog Gods Hate Me
  • How to make string LEDs?
  • It's Amazing What A Buck And A Quarter....

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How to monitor temperature and humidity on a TFT display with graphics
  • Tria modules integrate edge AI processing with multi-core processors
  • pSemi introduces RF switch with 52 dBm PMAX,PEAK and 90-dBm IIP3 linearity
  • XP Power launches 1.3 kW power supply with 58.9 W/cm³ density
  • How to enable Wi-Fi provisioning in ESP32-based IoT products

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe