ESP32 S3 UNO - Motion Sensor - Relay

This ESP32 S3 Uno motion sensor relay tutorial shows you how to switch a relay on and off with an HC-SR501 PIR motion sensor and the ESP32 S3 Uno form board. When someone walks by, the relay turns on. When the movement stops, it turns off again. You can use it to control a light bulb, a fan, an LED strip or a solenoid lock automatically.

In this tutorial, you will:

  1. Wire an HC-SR501 PIR motion sensor and a relay module to the ESP32 S3 Uno
  2. Set up the time delay, range and trigger mode of the motion sensor
  3. Detect when motion starts and stops with Arduino code
  4. Turn a relay on and off from the motion sensor state
ESP32 S3 Uno motion 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×HC-SR501 Motion Sensor
1×Alternatively, AM312 Mini Motion Sensor
1×Relay
1×Jumper Wires
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 Motion Sensor

This project puts two parts together. If one of them is new to you, read its own tutorial first. There you will learn the pinout, how it works and how to code it.

In short, the HC-SR501 senses the body heat of people and animals that move in front of it. Its OUTPUT pin goes HIGH when it sees motion and LOW when the motion is gone. The relay is an electric switch that the board turns on and off. The ESP32 S3 Uno reads the sensor and decides when the relay should switch.

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the D3 and D10 headers and their GPIO numbers when you wire the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The motion sensor output goes to D10 and the relay input goes to D3. Both modules get power from the 5V pin.

The wiring diagram between ESP32 S3 Uno Motion Sensor Relay

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Motion sensor VCC 5V
Motion sensor GND GND
Motion sensor OUTPUT D10 (GPIO10)
Relay VCC 5V
Relay GND GND
Relay IN D3 (GPIO17)

※ NOTE THAT:

The HC-SR501 needs 4.5V to 20V on its VCC pin, so power it from 5V. Its OUTPUT pin gives only about 3.3V when HIGH, so it is safe for the ESP32 S3 Uno input. The ESP32 S3 Uno pins are NOT 5V tolerant, so never send a 5V signal into D10.

If you use the optional solenoid lock, connect it to the relay's COM and NO screw terminals, together with the 12V power adapter and the DC power jack. Never power the lock from the ESP32 S3 Uno.

Initial Setting

The HC-SR501 has two small knobs and a jumper. Set them as below before you test the project. With these settings, the relay follows the motion closely and the sensor covers the largest area.

Time Delay AdjusterTurn it fully anti-clockwise.
Detection Range AdjusterTurn it fully clockwise.
Repeat Trigger SelectorPlace the jumper as shown in the image.
ESP32 S3 Uno motion sensor initial setting

ESP32 S3 Uno Code

The sketch reads the motion sensor in every loop and compares the new state with the old one. When the state changes from LOW to HIGH, motion has started, so the relay turns on. When it changes from HIGH to LOW, motion has stopped, so 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-motion-sensor-relay */ #define MOTION_SENSOR_PIN 10 // The ESP32 S3 Uno pin D10 (GPIO10) connected to the OUTPUT pin of motion sensor #define RELAY_PIN 17 // The ESP32 S3 Uno pin D3 (GPIO17) connected to the IN pin of relay int motion_state = LOW; // current state of motion sensor's pin int prev_motion_state = LOW; // previous state of motion sensor's pin void setup() { Serial.begin(9600); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set ESP32 S3 Uno pin to input mode pinMode(RELAY_PIN, OUTPUT); // set ESP32 S3 Uno pin to output mode } void loop() { prev_motion_state = motion_state; // store old state motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state if (prev_motion_state == LOW && motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (prev_motion_state == HIGH && motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); digitalWrite(RELAY_PIN, LOW); // turn off } }

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.
Arduino IDE Upload Code
  1. Open the Serial Monitor at 9600 baud.
  2. Wave your hand in front of the sensor, then stay still.
  3. Watch the relay. It clicks on when you move and clicks off a few seconds after you stop.
∞
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
Motion detected! Motion stopped! Motion detected! Motion stopped!
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: After power on, the HC-SR501 needs about 30 to 60 seconds to settle. Do not test it before that time.

Code Explanation

Every line of the sketch has a comment that says what it does. Read the comments in the code above to follow the logic step by step. The key idea is to act only when the sensor state changes, not on every loop, so the Serial Monitor shows one message per event.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

FAQ

Can I connect the HC-SR501 output directly to the ESP32 S3 Uno?

Yes. The HC-SR501 runs from 5V, but its OUTPUT pin gives about 3.3V when HIGH. This is safe for the ESP32 S3 Uno, whose pins are NOT 5V tolerant. If you use another motion sensor with a 5V output, add a voltage divider first.

Can I power the relay module from the 3.3V pin of the ESP32 S3 Uno?

Most relay modules have a 5V coil, so power their VCC from the 5V pin. The IN pin can still take the 3.3V signal from the ESP32 S3 Uno on most modules. If your relay does not click, use a 3V relay module or a module with a transistor driver.

How do I keep the light on longer after motion stops?

Turn the time delay knob on the HC-SR501 clockwise. The sensor will hold its output HIGH longer, so the relay stays on longer. You can also add a timer in the code with millis().

Is the code different from the UNO R4 code?

The logic is the same. Only the pin numbers change. On the ESP32 S3 Uno you write the GPIO number in code, so D10 is 10 and D3 is 17. Choose the ESP32S3 Dev Module board in Arduino IDE.

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 are I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 are I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in code. For a second motion sensor (an input), use GPIO15, GPIO16 or GPIO35 to GPIO42, never GPIO47 or GPIO48. For more relays, GPIO35 to GPIO42 are a good choice. Be careful with two things. 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.

Can I switch a 220V lamp with this relay?

The relay can switch mains power, but mains voltage is dangerous. Only do it if you know how to work safely with high voltage. For learning, use a 12V solenoid lock or a 12V LED strip instead.

Troubleshooting

Problem Possible Cause Solution
No COM port in Arduino IDE USB driver is missing or the board is not in upload mode Install the CP210x or CH340 driver, or hold BOOT while pressing RESET
Relay never switches Relay VCC or GND is not connected, or the relay needs 5V Power the relay VCC from 5V and check the GND wire
Motion is never detected The sensor has no power or is still warming up Power the sensor VCC from 5V and wait 30 to 60 seconds after power on
Relay turns on by itself The sensor sees heat changes from a heater, a window or the sun Point the sensor away from heat sources and lower the detection range
Relay stays on too long The time delay knob is turned too far clockwise Turn the time delay knob fully anti-clockwise
Relay works the opposite way The relay module is active LOW Swap HIGH and LOW in the digitalWrite() lines
Serial Monitor shows strange characters 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!