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:

  1. Wire an HC-SR04 ultrasonic sensor and a relay to the ESP32 S3 Uno
  2. Measure distance with the ultrasonic sensor in Arduino code
  3. Turn on the relay when an object is near
  4. Turn off the relay when the object moves away
ESP32 S3 Uno ultrasonic sensor relay

Hardware Preparation

1×ESP32 S3 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Ultrasonic Sensor
1×Relay
1×Jumper Wires
1×Recommended: Logic Level Converter for the ECHO pin
1×Optionally, Solenoid Lock
1×Optionally, 12V Power Adapter
1×Optionally, DC Power Jack

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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.

ESP32 S3 Uno pinout diagram

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.

The wiring diagram between ESP32 S3 Uno Ultrasonic Sensor Relay

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.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-ultrasonic-sensor-relay */ #define TRIG_PIN 14 // The ESP32 S3 Uno pin D7 (GPIO14) connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 3 // The ESP32 S3 Uno pin D6 (GPIO3) connected to the ultrasonic sensor's ECHO pin #define RELAY_PIN 4 // The ESP32 S3 Uno pin A5 (GPIO4) connected to Relay's pin #define DISTANCE_THRESHOLD 50 // centimeters float duration_us, distance_cm; void setup() { Serial.begin (9600); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set ESP32 S3 Uno pin to output mode pinMode(ECHO_PIN, INPUT); // set ESP32 S3 Uno pin to input mode pinMode(RELAY_PIN, OUTPUT); // set ESP32 S3 Uno pin to output mode } void loop() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; if(distance_cm < DISTANCE_THRESHOLD) digitalWrite(RELAY_PIN, HIGH); // turn on Relay else digitalWrite(RELAY_PIN, LOW); // turn off Relay // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

Detailed Instructions

Follow these steps in order:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into Arduino IDE.
  6. Upload the code by clicking the Upload button.
  7. Open the Serial Monitor and set the baud rate to 9600.
  8. Put your hand in front of the sensor, then move it away.
  9. Watch the relay click on and off, and check the distance on the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
distance: 112.43 cm distance: 108.97 cm distance: 35.24 cm distance: 21.86 cm distance: 98.61 cm
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. 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

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!