Getting Started with an Ultrasonic Sensor Module on The ESP32-S3

Cartoon robot uses an ESP32 and ultrasonic sensor to measure distance to a cone with playful sound waves background illustration

An ultrasonic sensor measures distance using sound waves, not light. This makes it a great choice for beginner robotics projects, because it works well in bright light, darkness, and most indoor environments.

A very common ultrasonic sensor is the HC-SR04. It works by sending out a short burst of high-frequency sound, which humans cannot hear. This sound travels through the air, hits an object, and then bounces back to the sensor.

When using an ultrasonic sensor with an ESP32, the ESP32 controls when the measurement starts. It sends a short signal to a pin on the sensor called TRIG. This tells the sensor to send out its sound pulse.

After the sound is sent, another pin called ECHO changes state. The ECHO pin stays active until the reflected sound is detected. The ESP32 measures how long this pin stays active, which tells it how long the sound took to travel to the object and back.

Once the ESP32 knows the time, it can calculate the distance. Sound travels through air at a known speed, so time can be turned into distance. Because the sound has travelled to the object and back, the ESP32 first divides the measured time by two.

The ESP32 then multiplies this value by the speed of sound in air, which is about 343 metres per second. This gives the distance from the sensor to the object.

For example, if the measured time is 0.01 seconds, the ESP32 divides this by two to get 0.005 seconds. Multiplying this by 343 gives 1.715 metres, so the object is about 1.7 metres away.

This simple timing method is why ultrasonic sensors are so popular with beginners. They are easy to understand, easy to wire up, and very useful for things like obstacle detection and basic robot navigation.

You can read more about how ultrasonic ranging sensors work here.

Parts List

Wire up the circuit

In order to run the project firstly the IHC-SR04 ultrasonic sensor module needs to be connected as shown in the diagram below. The circuit diagram below is shown for clarity and the actual connection on the breadboard follow.

The circuit looks as shown below on the set up that was used to create this tutorial.

Running the Code

In order to get started you will need to follow this tutorial, which will ensure that your environment and ESP32 are configured correctly.

Now create a file called main.py on the ESP32-S3 with the following content.

Next, click the run button and you should see the following in the console.

The output represents the distance from the sensor to the nearest object in front of it. You can try moving objects in front of the sensor closer and further away to see the sensor working.

Main Topic

ESP32 microcontroller

Photo shows an ESP32 on breadboards atop a black mat, with jumper wires nearby and the title The ESP32 visible.

ESP32 microcontroller overview explains processing power memory wireless connectivity variants and compares it with Raspberry Pi Pico for practical projects

Other Tutorials in this Topic

Cartoon illustration of ESP32-S3 connected to small colour display showing Tinkimo logo on breadboard with simple wires and clean background

Controlling an ST7789 Display With an ESP32-S3

Learn how an ESP32-S3 drives an ST7789 colour display to show images smoothly using simple code and…

Cartoon robot happily learning ESP32 S3 programming with Thonny on laptop holding board at bright desk classroom scene simple friendly

Getting Started With The ESP32-S3

A friendly introduction to getting started with the ESP32 S3 using Thonny showing simple coding and playful…

Cartoon ESP32 S3 connected to amplifier and speaker playing music while a sunglasses wearing camel dances happily in desert scene

Using the MAX98357A with the ESP32-S3

Learn how the ESP32 S3 sends digital audio over I2S to the MAX98357A amplifier to power a…

Cartoon penguin adjusts PWM speed dial controlling conveyor belt full of colorful fish using ESP32 and motor driver board

ESP32 Motor Control Using the L9110 Control Board

Learn how the ESP32 uses PWM and an H bridge motor driver to control motor speed and…

Cartoon zebra pushes small robot while oscilloscope shows encoder waveforms and laptop displays motor counts in workshop setting

Using a Motor Shaft Encoder With The ESP32

Explains quadrature encoders, state machines, decoder tables and interrupts to measure motor direction, distance travelled and wheel…