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

How to design a distance meter using ESP32

By Ayush Jain January 21, 2025

The Internet of Things (IoT) has transformed how we interact with one another and our environment. As devices become smarter and more connected, do-it-yourself (DIY) projects have become increasingly popular. One such project involves developing a distance meter using the ESP32, a versatile microcontroller with built-in Wi-FI, Bluetooth connectivity, and diverse capabilities.

This project highlights the potential of IoT technology while serving as a gateway for creators to advance their skills in smart device development.

Figure 1. The prototype

Understanding the project

Before diving into the technical implementation, it’s important to understand how a distance meter works. It determines the distance between itself and an object using a sensor to emit and receive signals.

In this project, we’ll use an ultrasonic sensor, which transmits high-frequency sound waves and calculates the time it takes to reflect back after hitting an object. Knowing the speed of sound allows for accurate distance measurement.

The HC-SR04, a widely used and cost-effective ultrasonic sensor, operates by transmitting and receiving ultrasonic waves. Its measurement is based on the time it takes for the sound waves to reach an object and return to the sensor. The process works as follows:

  1. Triggering: The ESP32 microcontroller sends a brief trigger signal to the HC-SR04 sensor.
  2. Wave emission: The sensor emits an ultrasonic wave upon receiving the trigger signal.
  3. Reflection: When the wave encounters an object, it reflects back toward the sensor.
  4. Echo detection: The sensor detects the returning wave and generates an echo signal.
  5. Distance calculation: By calculating the time interval between wave emission and echo detection and factoring in the speed of sound, the distance to the object is determined.

Components

For this project, you’ll require:

  • ESP32 development board: It’s responsible for processing data and controlling the system.

Figure 2. ESP32 development board

  • Ultrasonic sensor (HC-SR04): It measures distance by sending and receiving ultrasonic waves.

Figure 3. Ultrasonic sensor

  • OLED display: It displays the distance measured by the sensor and controller.

Figure 4. OLED display

  • Jumper wires: These wires are used for connecting various components to the breadboard.
  • Breadboard: It provides a platform for easy circuit prototyping.
  • USB cable: This cable is used for programming and powering the ESP32 board.
  • Computer/laptop: Required for programming the ESP32.

Building the circuit

Figure 5. Circuit diagram

1. Connect the ultrasonic sensor to ESP32:

    • VCC to 5V pin on the ESP32
    • GND to GND pin on the ESP32
    • Trig to GPIO pin (e.g., GPIO 5)
    • Echo to GPIO pin (e.g., GPIO 18) 

2. Connect the OLED display to ESP32:

    • VCC to 5V pin on the ESP32
    • GND to GND pin on the ESP32
    • SCK to GPIO pin (e.g., GPIO 21)
    • SDA to GPIO pin (e.g., GPIO 22)

 3. Power on the ESP32

    • Connect ESP32 to your computer using the USB cable

Coding the distance meter

After the circuit is completed, it’s time to program ESP32. We’ll use the Arduino IDE for this task.

Here are the steps:

1. Install the ESP32 board package:

    • Open the Arduino IDE by navigateing to File > Preferences
    • In the Additional Board Manager URLs field, paste https://dl.espressif.com/dl/package_esp32_index.json
    • Go to Tools > Board > Boards Manager, search for “ESP32,” and install the package

2. Write the code:

    • Below is the sketch to get you started

3. Connect ESP32 to your computer:

    • Select the correct board and port from the Tools menu
    • Click the upload button

Testing and calibration

Once the code is uploaded successfully, open the Serial Monitor in Arduino IDE. You should see the distance measurements displayed in centimeters. To test its accuracy, simply place objects in front of the ultrasonic sensor.

Figure 6. Test set-up

Conclusion

Integrating the HC-SR04 ultrasonic sensor with an ESP32 microcontroller delivers a reliable and efficient solution for distance measurement across various applications. Its versatility and ease of use make it a valuable tool in robotics, IoT projects, and automation.

By providing accurate, real-time data, this system proves indispensable in advancing smart technologies and enhancing the functionality of modern innovations.

Video

https://www.engineersgarage.com/wp-content/uploads/2025/01/Distance_meter_final-1-1.mp4

You may also like:


  • How to capture images using ESP32-CAM and an SD card

  • The top IoT projects for enhancing everyday life

  • How ESP32 boards can communicate without a router or the…

  • How to make an IoT-based analog clock using ESP32

  • How to design an IoT-based digital watch using ESP32

  • How to build a facial recognition system using ESP32-CAM

  • How to manage data on ESP32 for IoT projects

Filed Under: Electronic Projects, Video
Tagged With: bluetooth, circuitdesign, connectivity, distancemeter, diy, electronicproject, ESP32, meter, video
 

Next Article

← Previous Article
Next Article →

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



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

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

  • How to simulate power electronics converter in PSpice?
  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • High Side current sensing
  • Voltage mode pushpull is a nonsense SMPS?
  • Layout IRN reduction in Comparator

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

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