Skip to main content

Build. Learn.
Innovate.

Measuring Distance with an Ultrasonic Sensor

Written By:

Published on:

Project Overview

This project introduces students to the use of an HC-SR04 ultrasonic distance sensor with an ESP32 microcontroller. By measuring the time it takes for ultrasonic waves to bounce back from an object, the ESP32 calculates the distance. This data is displayed on the serial monitor and can optionally be shown on an OLED screen or web interface.

Educational Goals

  • Understand how ultrasonic distance measurement works
  • Calculate distance using time-of-flight of sound
  • Write MicroPython code to control sensors and interpret data
  • Explore applications in robotics, automation, and safety systems

Detailed Parts List

  • 1x ESP32 Dev Board (36-pin)
  • 1x HC-SR04 Ultrasonic Sensor
  • 1x 1kΩ Resistor
  • 1x 2kΩ Resistor (for voltage divider to protect ESP32)
  • Jumper wires
  • Breadboard
  • USB Cable (for flashing and power)

Circuit Diagram Description

  • Trig pin connected to ESP32 GPIO5
  • Echo pin connected to a voltage divider then to GPIO18
  • Voltage Divider: Echo → 2kΩ → GPIO18, and from that junction a 1kΩ to GND

ESP32 GPIO5  —> Trig (HC-SR04)
ESP32 GPIO18 <—+— 2kΩ — Echo (HC-SR04)
|
1kΩ
|
GND
VCC → 5V
GND → GND

Software Functionality

  • Sends a 10µs pulse from the Trig pin
  • Measures time for the Echo pulse to return
  • Calculates distance using distance = (time * speed_of_sound) / 2
  • Displays distance in centimeters

Web Interface Features (Optional Extension)

  • Web page to show live distance readings
  • Graphical indicator for proximity
  • Warning indicator if object is too close

Implementation Steps

  1. Build the circuit with proper voltage divider for Echo pin
  2. Flash ESP32 with MicroPython firmware
  3. Write and upload “** code using your IDE (e.g., Thonny)**
  4. View results in Serial Monitor
  5. (Optional) Extend with OLED or web interface

Extensions and Challenges

  • Display data on OLED
  • Add a buzzer to beep when object is too close
  • Send data to a web dashboard
  • Mount sensor on servo for scanning
  • Log distance data over time

Troubleshooting Guide

  • Always reads 0: Check Trig/Echo pin connections and ground
  • Very high/low values: Ensure delay and timing are correct
  • ESP32 crash or reboot: Echo pin may be sending 5V — use a voltage divider
  • Sensor not powering on: Use 5V pin, not 3.3V

Project Code


from machine import Pin, time_pulse_us
from time import sleep

# Pin configuration
trig = Pin(5, Pin.OUT)
echo = Pin(18, Pin.IN)

# Function to get distance
def get_distance():
trig.off()
sleep(0.002)
trig.on()
sleep(0.00001)
trig.off()
duration = time_pulse_us(echo, 1, 30000)  # timeout after 30ms
distance = (duration * 0.0343) / 2  # cm
return round(distance, 2)

# Main loop
while True:
d = get_distance()
print("Distance:", d, "cm")
sleep(1)

STEM Benefits

  • Science: Understand the physics of sound waves and how they travel through different media.
  • Technology: Learn how ultrasonic sensors work for real-world applications like robotics and automation.
  • Engineering: Build systems that respond to distance data, introducing concepts like feedback loops and sensor integration.
  • Math: Apply the speed-distance-time formula to calculate distances accurately from time delays.

Project Code

Projects
ShowCase

Real-World Projects
with Code & Hardware

Insights, Ideas
& How-Tos

Help, Support, and
Common Questions

What types of projects can I find on your website?

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.