ESP32 S3 UNO - Ultrasonic Sensor - Relay
This ESP32 S3 Uno ultrasonic sensor relay tutorial shows you how to switch a relay on and off by distance with the ESP32 S3 Uno form board. The ultrasonic distance sensor measures how far away an object is. When the object comes close, the relay turns on, so you can control a light, a motor or a solenoid lock.
In this tutorial, you will:
- Wire an HC-SR04 ultrasonic sensor and a relay to the ESP32 S3 Uno
- Measure distance with the ultrasonic sensor in Arduino code
- Turn on the relay when an object is near
- Turn off the relay when the object moves away

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Relay and Ultrasonic Sensor
This project joins two parts that each have their own tutorial. If you know how each one works, the code here is very easy to follow.
The ultrasonic sensor sends out a short sound pulse and times how long the echo takes to come back. The relay is a switch that your board controls with one pin, so it can turn a higher-voltage device on and off. To learn the pinout and the basic code of each part, read these tutorials first:
ESP32 S3 Uno Pinout
The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the D6, D7 and A5 headers and their GPIO numbers when you wire the circuit.

Wiring Diagram
The sensor uses two digital pins, and the relay uses one more pin. Both modules take power from the 5V pin of the ESP32 S3 Uno.

This image is created using Fritzing. Click to enlarge image
| Component Pin | ESP32 S3 Uno Pin |
|---|---|
| Ultrasonic Sensor VCC | 5V |
| Ultrasonic Sensor TRIG | D7 (GPIO14) |
| Ultrasonic Sensor ECHO | D6 (GPIO3) |
| Ultrasonic Sensor GND | GND |
| Relay DC+ (VCC) | 5V |
| Relay DC- (GND) | GND |
| Relay IN | A5 (GPIO4) |
WARNING
The HC-SR04 runs at 5V, so its ECHO pin sends out a 5V signal. The ESP32 S3 Uno pins are NOT 5V tolerant. Put a logic level converter or a simple voltage divider (for example 1 kΩ from ECHO to the pin and 2 kΩ from the pin to GND) between ECHO and D6 (GPIO3). The TRIG pin is fine, because the sensor accepts a 3.3V trigger signal.
WARNING
D6 is GPIO3, a boot strapping pin on the ESP32 S3 Uno. The board reads it at startup. If the ECHO line holds it at a wrong level while the board resets, the board may not boot or may fail to upload code. If that happens, unplug the ECHO wire, upload the code, then plug it back in.
WARNING
The relay output side can switch high voltage. Make sure the COM and NO (or NC) wires are tight and no bare wire is exposed before you power the circuit. Keep the load wiring away from the board and the sensor wires.
ESP32 S3 Uno Code
The sketch sends a 10-microsecond pulse to the TRIG pin and times the echo on the ECHO pin. It turns this time into a distance in centimeters. If the distance is less than 50 cm, the relay turns on. Otherwise, the relay turns off.
Detailed Instructions
Follow these steps in order:
- New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
- Wire it up as shown in the diagram.
- Connect the board to your computer with a USB Type-C cable.
- Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
- Copy the code above and paste it into Arduino IDE.
- Upload the code by clicking the Upload button.
- Open the Serial Monitor and set the baud rate to 9600.
- Put your hand in front of the sensor, then move it away.
- Watch the relay click on and off, and check the distance on the Serial Monitor.
- Tip: Change DISTANCE_THRESHOLD in the code to set how close an object must be before the relay turns on.
Code Explanation
The comments in the source code explain each line, so read them from top to bottom. The main idea is simple: measure the distance, compare it with the threshold, then set the relay pin HIGH or LOW.
※ NOTE THAT:
This code is made for learning. The ultrasonic sensor picks up a lot of noise, so a single bad reading can make the relay click by mistake. To get stable results, filter the readings. You can learn how in how to filter noise for ultrasonic sensor.
Video Tutorial
Watch the video below to see this ESP32 S3 Uno project step by step.
FAQ
Can I power the HC-SR04 ultrasonic sensor from 3.3V on the ESP32 S3 Uno?
The classic HC-SR04 needs 5V to work well, so power it from the 5V pin. Then its ECHO signal is also 5V, which is too high for the ESP32 S3 Uno pins. Use a level converter or a voltage divider on the ECHO line. Some newer sensors, like the HC-SR04P, also run at 3.3V and do not need this.
Will a 5V relay module work with the 3.3V signal of the ESP32 S3 Uno?
Most 5V relay modules with a transistor or optocoupler input switch fine with a 3.3V signal. Power the module from 5V and connect IN to A5 (GPIO4). If the relay does not click, use a relay module made for 3.3V logic or add a transistor driver.
Why does the relay click on and off by itself?
The ultrasonic sensor gives noisy readings, and a reading near the threshold can jump above and below it. Filter the readings, or use two thresholds (for example, on below 45 cm and off above 55 cm). This stops the relay from chattering.
Is it safe to use D6 for the ECHO pin on the ESP32 S3 Uno?
D6 is GPIO3, a boot strapping pin. It works fine while the code runs, but a wrong level at startup can stop the board from booting or uploading. The wiring image uses D6, so this tutorial keeps it. If you have upload problems, unplug the ECHO wire during upload.
What if my project needs more pins than the Uno headers give?
The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 support I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 support I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in your code. For more sensors or relays, GPIO35 to GPIO42 are a good choice. Do not use GPIO47 or GPIO48 for the ECHO input, because they are output only. Keep two warnings in mind. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port shows up in Arduino IDE | USB driver missing or board not in upload mode | Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET |
| Upload fails or the board does not start | ECHO line pulls D6 (GPIO3) at boot | Unplug the ECHO wire, upload the code, then plug it back in |
| Distance is always 0 | ECHO or TRIG wire loose or on the wrong pin | Check TRIG on D7 (GPIO14) and ECHO on D6 (GPIO3) |
| Distance values jump around | Noise or a soft surface in front of the sensor | Filter the readings and point the sensor at a flat, hard object |
| Relay never clicks | Relay not powered or IN wire on the wrong pin | Power the relay from 5V and connect IN to A5 (GPIO4) |
| Relay clicks on and off quickly | Distance is close to the threshold | Filter the readings or use two thresholds |
| Board resets or acts strangely | 5V ECHO signal on a 3.3V pin | Add a level converter or voltage divider on the ECHO line |
| Nothing prints on the Serial Monitor | Wrong baud rate | Set the Serial Monitor to 9600 baud |