STEP 1 / 6ESP + IOT

ESP8266 NodeMCU with HC-SR04 Ultrasonic Sensor with Arduino IDE

418.ESP8266 NodeMCU with HC-SR04 Ultrasonic Sensor with Arduino IDE This project shows how to use the Arduino core to connect the HC-SR04 ultrasonic sensor to the ESP8266 NodeMCU board. S…

ultrasonic sensor to figure out how far away an object is.
PROJECT#418
TRACKIoT
PARTS06
STAGES06
STEP 1 / 6 · Overview

Know the mission before touching a wire.

Understand what you are making, prepare the right tools, and make the workbench safe.

01

Project details

ESP8266 NodeMCU with HC-SR04 Ultrasonic Sensor with Arduino IDE is a iot project. 418.ESP8266 NodeMCU with HC-SR04 Ultrasonic Sensor with Arduino IDE This project shows how to use the Arduino core to connect the HC-SR04 ultrasonic sensor to the ESP8266 NodeMCU board. S…

Source pages
981-984
Named parts
6
Build goal
Working, tested prototype
02

Tools you need

  • Digital multimeter
  • Wire stripper and side cutters
  • Soldering iron with a fine tip
  • Current-limited bench supply
  • Computer with a data-capable USB cable

Use eye protection, good lighting, and a clean insulated surface throughout the build.

03

Safety precautions

  • Disconnect every power source before changing a connection.
  • Check component polarity, pinout, and supply voltage twice.
  • Use a current limit for the first power-up.
  • Keep liquids, loose metal, and uninsulated wires away from the bench.
Ready to continue?
STEP 2 / 6 · Parts library

Gather, identify, and understand every part.

Use the standardized inventory, then open What's this? to learn each part's role, advantages, limitations, handling, and specifications.

NAMED PROJECT INVENTORY6 PART LINES
PARTTYPEQTYREADY
MESP8266MODULE1
What's this?Image, role, pros, cons, handling & specifications
ultrasonic sensor to figure out how far away an object is.MODULE LEARNING VIEW

ESP8266

A programmable controller that reads inputs, makes decisions, and drives the project's outputs.

What it does here

It is the control centre and must use the documented board, pin map, supply, and logic level.

Buy / compare this part

Advantages

  • Reprogrammable and reusable
  • Large learning ecosystem
  • Complex behaviour remains changeable

Limitations

  • GPIO voltage and current are limited
  • Some pins affect boot or communication
  • Loads normally need a driver

Handling

  • Disconnect power before rewiring
  • Avoid static discharge
  • Never power motors, relays, or pumps directly from GPIO

Specifications to verify

  • Use the exact model, value, package, and rating listed for ESP8266; similar-looking parts are not always interchangeable.
  • Confirm operating voltage, logic level, pinout, memory, USB interface, and maximum GPIO current.
MNodeMCUMODULE1
What's this?Image, role, pros, cons, handling & specifications
NodeMCU ESP8266 development boardMODULE LEARNING VIEW

NodeMCU

A programmable controller that reads inputs, makes decisions, and drives the project's outputs.

What it does here

It is the control centre and must use the documented board, pin map, supply, and logic level.

Buy / compare this part

Advantages

  • Reprogrammable and reusable
  • Large learning ecosystem
  • Complex behaviour remains changeable

Limitations

  • GPIO voltage and current are limited
  • Some pins affect boot or communication
  • Loads normally need a driver

Handling

  • Disconnect power before rewiring
  • Avoid static discharge
  • Never power motors, relays, or pumps directly from GPIO

Specifications to verify

  • Use the exact model, value, package, and rating listed for NodeMCU; similar-looking parts are not always interchangeable.
  • Confirm operating voltage, logic level, pinout, memory, USB interface, and maximum GPIO current.
MUltrasonic sensorMODULE1
What's this?Image, role, pros, cons, handling & specifications
ultrasonic sensor to figure out how far away an object is.MODULE LEARNING VIEW

Ultrasonic sensor

A sensor converts a physical condition into an electrical signal the circuit can measure.

What it does here

It provides project input as an analogue, digital, resistive, frequency, or calibrated signal.

Buy / compare this part

Advantages

  • Adds real-world awareness
  • Can usually be tested independently
  • Often supports calibration

Limitations

  • Readings can drift
  • Placement affects results
  • Some sensors need warm-up or calibration

Handling

  • Protect the sensing surface
  • Observe supply voltage and polarity
  • Keep signal leads away from noisy power wiring

Specifications to verify

  • Use the exact model, value, package, and rating listed for Ultrasonic sensor; similar-looking parts are not always interchangeable.
  • Confirm supply range, output type, measurement range, accuracy, response time, and pin order.
MOLED displayMODULE1
What's this?Image, role, pros, cons, handling & specifications
ultrasonic sensor to figure out how far away an object is.MODULE LEARNING VIEW

OLED display

An output component turns an electrical control signal into light, sound, motion, switching, or displayed information.

What it does here

It presents the circuit result or acts on the physical world.

Buy / compare this part

Advantages

  • Makes system state visible
  • Can be tested separately
  • Supports clear troubleshooting

Limitations

  • Loads may exceed controller current
  • Polarity or driver direction can matter
  • Inductive loads create voltage spikes

Handling

  • Use the documented driver stage
  • Check polarity and load current
  • Add flyback protection for inductive loads

Specifications to verify

  • Use the exact model, value, package, and rating listed for OLED display; similar-looking parts are not always interchangeable.
  • Confirm voltage, current, polarity, interface, driver requirements, and duty cycle.
PHC-SR04PART1
What's this?Image, role, pros, cons, handling & specifications
ultrasonic sensor to figure out how far away an object is.PART LEARNING VIEW

HC-SR04

A named project component whose exact role is defined by the source circuit and build guide.

What it does here

It performs a documented electrical, control, interface, or construction function in this project.

Buy / compare this part

Advantages

  • Selected for this project
  • Can be checked independently
  • Supports modular troubleshooting

Limitations

  • Substitutes may differ
  • Generic names can hide variants
  • Pinouts and ratings vary

Handling

  • Compare the received part with the source
  • Keep it labelled
  • Do not force connectors or adjusters

Specifications to verify

  • Use the exact model, value, package, and rating listed for HC-SR04; similar-looking parts are not always interchangeable.
  • Confirm dimensions, ratings, connection method, polarity, and environmental limits.
PNEARESTPART1
What's this?Image, role, pros, cons, handling & specifications
ultrasonic sensor to figure out how far away an object is.PART LEARNING VIEW

NEAREST

A named project component whose exact role is defined by the source circuit and build guide.

What it does here

It performs a documented electrical, control, interface, or construction function in this project.

Buy / compare this part

Advantages

  • Selected for this project
  • Can be checked independently
  • Supports modular troubleshooting

Limitations

  • Substitutes may differ
  • Generic names can hide variants
  • Pinouts and ratings vary

Handling

  • Compare the received part with the source
  • Keep it labelled
  • Do not force connectors or adjusters

Specifications to verify

  • Use the exact model, value, package, and rating listed for NEAREST; similar-looking parts are not always interchangeable.
  • Confirm dimensions, ratings, connection method, polarity, and environmental limits.
Ready to continue?
STEP 4 / 6 · Source code

Confirm the hardware-only control path.

This project does not include firmware in the source. The circuit itself provides the required behaviour.

01

How to connect

  1. Match every controller label to the circuit view and source pin map.
  2. Join grounds before signal wires when separate low-voltage supplies are used.
  3. Keep motors, relays, pumps, and other loads on a suitable driver and external supply.

Common mistakes

Reversed VCC/GND, board-label versus GPIO-number confusion, missing common ground, and charge-only USB cables.

Troubleshoot

Disconnect loads, continuity-test one path at a time, then test with a current limit.

02

Software preparation

No IDE, board package, library, or firmware upload is required for this project.

If you add a programmable controller as an extension, document its pin map separately.

03

How to upload code

The original design is implemented entirely in hardware, so proceed after verifying the circuit and supply.

Ready to continue?
STEP 5 / 6 · Build

Assemble, deploy, test, and troubleshoot.

Use the complete source notes in build order, then pass the final checks before calling the project finished.

ASSEMBLY

Build in functional stages

  • Power and regulation
  • Controller or processing stage
  • Inputs and sensors
  • Outputs and loads
  • Enclosure and strain relief
TEST

Power up safely

  • Inspect unpowered continuity first
  • Apply the lowest safe current limit
  • Measure supply rails before signals
  • Add one load at a time
  • Record expected and actual results
TROUBLESHOOT

Work from simple to complex

  • Confirm power, ground, polarity, and orientation
  • Compare each pin with the source
  • Test inputs separately from outputs
  • Replace only one variable at a time
  • Power off before every correction
PROJECT-SPECIFIC BUILD NOTES

Follow the documented instructions.

These notes come from this project's source and remain in their original order.

01

Project overview

Project build note

418.ESP8266 NodeMCU with HC-SR04 Ultrasonic Sensor with Arduino IDE This project shows how to use the Arduino core to connect the HC-SR04 ultrasonic sensor to the ESP8266 NodeMCU board. Sonar is used by the ultrasonic sensor to figure out how far away an object is. We'll show you how to connect the sensor to the ESP8266 and give you a few examples sketches for using the HC-SR04 to figure out how far away

something is.

02

The HC-SR04 Ultrasonic Sensor

Project build note

Sonar is used by the HC-SR04 ultrasonic sensor to figure out how far away an object is. This sensor has a reading range of 2 centimetres to 400 centimetres (0.8 inches to 157 inches) and an accuracy of 0.3 centimetres (0.1 inches), making it suitable for the majority of projects undertaken by hobbyists. Also, this module has ultrasonic transmitter and receiver modules built in. How does an ultrasonic sensor like the HC-SR04 work? Sonar is used by the ultrasonic sensor to figure out how far away an object

03

is. How it works is as follows

Project build note

The high-frequency sound is sent out by the ultrasound transmitter (trig pin) (40 kHz). The sound is carried by the air. It goes back to the module if it finds something.

The reflected sound is picked up by the echo pin, which is an ultrasound receiver (echo). We can figure out how far away something is by figuring out how fast the sound is travelling through the air and how much time has passed since the

04

signal was sent and received. Here's how it works

Project build note

distance to an object = ((speed of sound in the air)*time)/2 speed of sound in the air at 20oC (68oF) = 343m/s Parts Needed Here’s a list with the parts required to complete this example: HC-SR04 Ultrasonic Sensor ESP8266 (read Best ESP8266 development boards) 0.96-inch I2C OLED Display SSD1306 Breadboard Jumper wires Schematic Diagram: ESP8266 with HC-SR04 and OLED Display

After the uploading process is complete, start the Serial Monitor with a baud rate of 115200. If you restart the board by pressing the RST button that's built into it, the Serial Monitor will begin to display the distance between the board and the nearest object. Something like what is in the picture. Code to Display the ESP8266 Distance Sensor (HC-SR04) on an OLED Display, to follow along with this example, you will need to ensure that the Adafruit SSD1306 and Adafruit GFX libraries are installed on your computer. Through the Arduino Library Manager, you'll be able to install these libraries on your computer. You can get the SSD1306 library from Adafruit by going to Sketch > Library > Manage Libraries , searching for "SSD1306," and then installing it.

Ready to continue?
PROJECT ACHIEVED

You built ESP8266 NodeMCU with HC-SR04 Ultrasonic Sensor with Arduino IDE.

You followed the full workflow from understanding the mission to testing the finished project. That is a real engineering achievement - well done.

ESP8266 NodeMCU with BMP388 Barometric/Altimeter Sensor project thumbnail featuring BMP388 sensor module, ESP8266 (read Best ESP8266 development boards), Schematic for ESP8266 NodeMCU with BMP388
NEXT IOT ADVENTURE

ESP8266 NodeMCU with BMP388 Barometric/Altimeter Sensor

Ready to reuse what you learned in another project from the same track?

Start this project
Browse all 500 projects