ESP32 S3 - Motion Sensor - Relay
This tutorial shows you how to use the ESP32 S3 with an HC-SR501 PIR motion sensor and a relay module to automatically switch a high-voltage device on and off based on detected motion. By the end, you will have a solid foundation for building real-world automation projects such as motion-activated lights, alarms, and security systems with your ESP32 S3.
What you'll build:
- A circuit connecting an HC-SR501 motion sensor and relay module to the ESP32 S3
- A sketch that activates the relay when motion is detected and deactivates it when the area clears
- A system that can switch high-voltage loads like lamps, sirens, or fans via the relay
- A foundation for extending into smart-home automation and security projects
We can extend this tutorial to use the motion sensor to control an LED strip, siren, light bulb, or actuator by connecting them to the relay.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Motion Sensor and Relay
The HC-SR501 PIR motion sensor and relay module are two of the most practical building blocks for automated ESP32 S3 control projects. The HC-SR501 outputs a digital HIGH signal whenever it detects movement from a warm object such as a person or animal, while the relay allows the ESP32 S3 to safely switch high-voltage or high-current loads that would otherwise damage the board's GPIO pins directly.
Key Specifications
We have dedicated tutorials covering each component in depth. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to the ESP32 S3, and Arduino code. Learn more about them at the following links:
Wiring Diagram
Connect the HC-SR501 motion sensor and relay module to your ESP32 S3 by following the diagram below. The sensor requires a 5V supply for reliable operation, so use the 5V rail on the ESP32 S3 rather than the 3.3V rail.

This image is created using Fritzing. Click to enlarge image
Want a cleaner and safer setup? Try a Screw Terminal Block for ESP32 S3: the ESP32 S3 sits on it, and each pin gets its own screw terminal. The pins do not change. Just connect each wire to the matching screw terminal:

This image is created using Fritzing. Click to enlarge image
You can also use a GPIO Breakout Board for ESP32 S3. It gives each GPIO its own S, 3.3V and GND pins, so module wires plug straight in:

This image is created using Fritzing. Click to enlarge image
Safety Notes
Always power the HC-SR501 from the 5V pin of the ESP32 S3, never from 3.3V, as insufficient supply voltage leads to erratic or no detection. On the relay side, verify that the COM and NO (or NC) terminals are securely fastened and that no bare wires are exposed before applying power — keep the high-voltage load wiring physically separated from the low-voltage control wiring to reduce the risk of accidental contact.
| Component | Pin | ESP32 S3 Pin |
|---|---|---|
| HC-SR501 | VCC | 5V |
| HC-SR501 | GND | GND |
| HC-SR501 | OUTPUT | GPIO42 |
| Relay | DC- | GND |
| Relay | DC+ | 5V |
| Relay | IN | GPIO9 |
Initial Sensor Settings
Before using the HC-SR501, configure these settings for optimal performance during initial testing.

| Setting | Configuration |
|---|---|
| Detection Range Adjuster | Fully screw clockwise (minimum range for testing) |
| Time Delay Adjuster | Fully screw counter-clockwise (minimum delay) |
| Repeat Trigger Selector | Position jumper as shown in image above |
ESP32 S3 Code
The following code reads the HC-SR501 motion sensor output connected to the ESP32 S3 and controls a relay accordingly — activating the relay when motion is detected and deactivating it when the area becomes clear. State transitions are tracked from loop to loop so the relay responds only to changes in motion state rather than continuously toggling.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Wire the components following the wiring diagram shown above.
- Connect the ESP32 S3 board to your PC using a USB Type-C cable.
- Open Arduino IDE on your PC.
- Select the correct ESP32 S3 board and COM port from the Tools menu.
- Copy the above code and paste it into Arduino IDE.
- Click the Upload button to compile and upload the code to the ESP32 S3.

- Move your hand in front of the HC-SR501 sensor.
- Observe the relay activating (you will hear a click) when motion is detected.
- Stop moving and observe the relay deactivating after the sensor's time delay expires.
- Pro Tip: Open the Serial Monitor at 115200 baud to view motion detection and relay state messages in real time — this makes it much easier to debug unexpected behavior or fine-tune the sensor's sensitivity and delay potentiometers.
Serial Monitor Output
The following Serial Monitor output shows the ESP32 S3 responding to motion detection and clearance events in real time. Each line is timestamped so you can verify the timing between detection and relay activation.
Application and Project Ideas
Once you master motion-controlled relay switching with the ESP32 S3, you can extend this pattern into a wide range of practical automation and smart-home projects.
- Automatic room lighting: Turn on a lamp or LED strip when someone enters a room and switch it off automatically after they leave.
- Motion-activated alarm: Trigger a 12V siren or warning light when the sensor detects an intruder outside business hours.
- Energy-saving office system: Automatically cut power to monitors, fans, or coffee machines when no motion is detected for a set period.
- Smart staircase light: Activate stairway lighting when someone begins climbing and turn it off shortly after they reach the top.
- Garage door alert: Use the relay to sound a buzzer whenever motion is detected near a vehicle or entry point.
- Greenhouse automation: Activate a ventilation fan whenever a technician enters the growing area, ensuring fresh air circulation on demand.
Relay vs MOSFET Module
A relay is not the only way to switch a device. If your device runs on DC power, such as a 12V LED strip, a 12V solenoid lock or a 12V fan, a MOSFET module can do the same job.
| Relay | MOSFET Module | |
|---|---|---|
| What it can do | turn the device on or off | turn the device on or off, and set its power (PWM) |
| Noise when switching | a click every time | no sound |
| How fast it switches | slow, a few milliseconds | very fast |
| Life | the contacts wear out after many switches | nothing moves, so it lasts long |
| Type of power | AC or DC | DC only |
| Device power supply | any voltage the relay is rated for | about 6V or more |
| Isolation from the ESP32 S3 | yes, between coil and contacts | yes, by the PC817 optocoupler |
| Good for | AC devices, or a simple ON/OFF | DC devices, or dimming and speed control |
Both the relay and the MOSFET module turn on when the ESP32 S3 pin is HIGH. The 3.3V level of the ESP32 S3 is plenty, because the LED in the PC817 optocoupler needs only a few milliamps. So for a simple ON/OFF, the code above stays the same. Only the wiring changes. For AC mains devices, always use the relay.
To learn the MOSFET module pinout, wiring, PWM code and safety tips, see the ESP32 S3 - MOSFET Module tutorial.
Video Tutorial
Watch the step-by-step video walkthrough for this ESP32 S3 project below.
Challenge Yourself
Now that you can control a relay with a motion sensor on the ESP32 S3, try these progressively challenging extensions to deepen your understanding and build more capable projects.
- Beginner: Add an LED indicator that lights up alongside the relay so you have a visual confirmation of motion detection without listening for the relay click.
- Beginner: Modify the code to print the total count of motion events to the Serial Monitor each time a new detection occurs.
- Intermediate: Implement a minimum ON time so the relay stays active for at least 10 seconds after motion is detected, even if the sensor clears sooner.
- Intermediate: Add a second HC-SR501 sensor to cover a wider area, activating the relay only when either sensor detects motion.
- Advanced: Add a real-time clock module and restrict relay activation to specific hours, effectively creating a timed motion-activated security system.
- Advanced: Extend the project with WiFi so the ESP32 S3 sends an email or push notification whenever the relay is activated by motion.