Dolphins and bats navigate the world using ultrasounds, which are sounds with frequencies above the range of human hearing. In this tutorial, you will learn how to use ultrasounds to make exciting STEM projects.
Project Overview
In this project, we will connect an Ultrasonic Sensor HC-SR04 to the NodeMCU ESP8266 and use it to measure distances. We will also learn how to send our sensor data to the ThingSpeak cloud so that we can access it wherever we are.
The ultrasonic sensor functions in the same way as a bat use ultrasound for navigation in the darkness of the night. The bat emits sound waves and then observe the time that it takes to receive the echo. That amount of time gives it the idea whether there is an object in front of it and how far it is.
The HC-SR04 as shown below, is a popular distance sensor for STEM and engineering projects. It can provide accurate measurements from 2cm up to 400cm with an accuracy of 3mm.
The three major parts are shown here labeled as T, R and Y1. T is the transmitter or trigger that emits the waves, R is the receiver that senses the echo, and Y1 is a crystal oscillator that is used to generate the pulses for timing.
In order to interface the Ultrasonic Sensor with the NodeMCU, there are 4 pins that must be connected. Vcc and GND will be connected to +5V and GND of the board. Trig and Echo will then be connected to the GPIO pins for programming.
Components List
We will be using the following materials for this experiment:
- NodeMCU ESP8266
- HC-SR04 Ultrasonic Sensor
- Breadboard and jumper wires
Wiring Diagram

Connect the VCC of Ultrasonic Sensor HC-SR04 to pin 18 of the NodeMCU. This pin is gets its supply directly from the USB power supply which means that it can supply the proper voltage for the Ultrasonic Sensor.
Source Code
const int trigPin = 16, echoPin = 5;
long pulseDuration;
float distance;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
//Set trigPin to LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
//Emit a HIGH pulse for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//Listen until a HIGH pulse by echoPin is received; record the duration in microseconds
pulseDuration = pulseIn(echoPin, HIGH);
//distance = (pulseDuration x speed of sound)/2
distance = pulseDuration * 0.034/2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
delay(1000);
}
Here is the line-by-line explanation of the code
const int trigPin = 16, echoPin = 5;
Assigns GPIO16 for trigPin and GPIO5 for echoPin.
long pulseDuration;
float distance;
Create variable pulseDuration with data type long and the variable distance as float.
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
For the setup(), set Serial Connection speed to 115200 bauds. Set the trigPin as an output and echoPin as an input.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
To ensure that the trigger pin is OFF, write a LOW signal to trigPin for 2 microseconds.
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
Send a HIGH pulse to the trigger pin for 10 microseconds and then shut it off.
pulseDuration = pulseIn(echoPin, HIGH);
Wait for a HIGH pulse from echoPin and record the duration as pulseDuration in microseconds.
distance = pulseDuration * 0.034/2;
Distance can be calculated using the formula distance = speed x time. The time used here is pulseDuration and the speed is 0.034 cm/s. The calculated distance is given in centimeters.
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
Prints the distance with proper formatting in the Serial Monitor.
delay(1000);
One second of delay is given to update the reading every second.
If the programming and connections have been done well, the Serial Monitor should have an output similar to this:

Have you tried this circuit? Post any question or suggestion in the comments section below.