ESP32 S3 UNO - Motion Sensor - LED
This ESP32 S3 Uno motion sensor LED tutorial shows you how to turn on an LED when someone moves nearby. You will wire an HC-SR501 PIR sensor and an LED to the ESP32 S3 Uno form board. When the sensor sees motion, the LED lights up. When the motion stops, the LED turns off.
In this tutorial, you will:
- Wire an HC-SR501 PIR motion sensor and an LED to the ESP32 S3 Uno
- Set the time delay, range and trigger mode of the motion sensor
- Detect motion with digitalRead() in Arduino code
- Turn an LED on and off when the motion starts and stops

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 .
Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.
Overview of LED and Motion Sensor
This project joins one input and one output. The motion sensor tells the board when something moves, and the LED shows it. If you have not used these parts before, learn each one alone first. The tutorials below cover the pinout, how each part works, and how to program it.
How the project works
The HC-SR501 senses the infrared heat that people and animals give off. When a warm body moves in front of it, its OUTPUT pin goes HIGH. When the movement stops and the time delay ends, the pin goes back to LOW.
The ESP32 S3 Uno reads this pin all the time. It turns the LED on when the pin changes from LOW to HIGH, and turns the LED off when the pin changes from HIGH to LOW.
ESP32 S3 Uno Pinout
The image below shows the pinout of the ESP32 S3 Uno form board. Use it to match the Uno header pins (D3, D7) to their GPIO numbers when you build the circuit.

Wiring Diagram
The motion sensor's OUTPUT pin goes to D7. The LED goes to D3 through a 220 ohm resistor.

This image is created using Fritzing. Click to enlarge image
| Component Pin | ESP32 S3 Uno Pin |
|---|---|
| Motion Sensor VCC | 5V |
| Motion Sensor OUTPUT | D7 (GPIO14) |
| Motion Sensor GND | GND |
| LED Anode (+) via 220 ohm resistor | D3 (GPIO17) |
| LED Cathode (-) | GND |
※ NOTE THAT:
The HC-SR501 is powered from 5V, but its OUTPUT pin gives about 3.3V when HIGH. This is safe for the ESP32 S3 Uno, whose pins work at 3.3V and are NOT 5V tolerant. If you use a different PIR module, check that its output is not 5V.
Initial Setting
The HC-SR501 has two small knobs and one jumper on its back. Set them as shown below before you upload the code. With these settings, the LED reacts quickly and turns off soon after the motion stops.
| Time Delay Adjuster | Turn it fully anti-clockwise. |
| Detection Range Adjuster | Turn it fully clockwise. |
| Repeat Trigger Selector | Place the jumper as shown in the image. |

ESP32 S3 Uno Code
The sketch reads the motion sensor pin in a loop and remembers its last state. When the state changes from LOW to HIGH, it prints "Motion detected!" and turns the LED on. When it changes from HIGH to LOW, it prints "Motion stopped!" and turns the LED off.
Detailed Instructions
- 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 the Arduino IDE.
- Upload the code by clicking the Upload button.

- Wave your hand in front of the sensor.
- Watch the LED. It turns on when you move and turns off a short time after you stop.
- Tip: open the Serial Monitor at 9600 baud to see when motion starts and stops. The sensor needs about one minute after power-on to settle, so ignore early triggers.
Code Explanation
Each line of the sketch has a comment that explains it. The code keeps two values, motion_state and motion_state_prev, so it acts only when the sensor state changes, not on every loop. In the code, the pins are GPIO numbers: MOTION_SENSOR_PIN is 14 (header D7) and LED_PIN is 17 (header D3).
Video Tutorial
Watch the video below to see this ESP32 S3 Uno project step by step.
FAQ
Why does the code use 14 and 17 instead of 7 and 3?
The board has Uno header labels, but the code uses GPIO numbers. Header D7 is GPIO14 and header D3 is GPIO17. So MOTION_SENSOR_PIN is 14 and LED_PIN is 17.
Is the HC-SR501 output safe for the ESP32 S3 Uno?
Yes. The HC-SR501 needs 5V on VCC, but its output signal is about 3.3V. The ESP32 S3 Uno pins are not 5V tolerant, so this is important. If your PIR module gives a 5V output, add a voltage divider or a level converter.
Why does the LED stay on for a long time after I stop moving?
The Time Delay knob on the sensor sets how long the output stays HIGH. Turn it fully anti-clockwise for the shortest delay, which is about 3 seconds. Also check the Repeat Trigger jumper.
Why does the LED turn on by itself after I power the board?
The HC-SR501 needs about 30 to 60 seconds to warm up after power-on. During this time it can give false triggers. Wait a minute before you test it.
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 (I/O, PWM, analog), GPIO45 and GPIO35 to GPIO42 (I/O, PWM), and GPIO47/GPIO48 (output only, PWM). Solder pin headers to them and write the GPIO number in code. For a second motion sensor, use GPIO35 to GPIO42, and never GPIO47/GPIO48 because they are output only. For more LEDs, GPIO35 to GPIO42 are also good choices. Be careful with two things. First, GPIO47/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.
Does this code work the same as on the UNO R4?
Yes. digitalRead() and digitalWrite() work the same way on the ESP32 S3 Uno. Only the pin numbers change, and the board uses 3.3V logic instead of 5V.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port in Arduino IDE | USB driver missing or bad cable | Install the CP210x or CH340 driver, try a data USB cable, or hold BOOT while pressing RESET |
| LED never lights | LED is the wrong way round or wrong pin in code | Put the long leg (anode) to the resistor and D3, and use 17 in the code |
| Nothing happens when you move | Sensor not powered or OUTPUT not on D7 | Connect VCC to 5V, GND to GND, OUTPUT to D7, and use 14 in the code |
| LED turns on by itself after power-on | Sensor is still warming up | Wait 30 to 60 seconds after power-on before testing |
| LED stays on too long | Time Delay knob set high | Turn the Time Delay knob fully anti-clockwise |
| Sensor reacts from too far away | Detection Range set to maximum | Turn the Detection Range knob to reduce the range |
| Random triggers | Heat sources or air flow in front of the sensor | Keep the sensor away from heaters, lamps, and windows |
| Serial Monitor shows strange characters | Wrong baud rate | Set the Serial Monitor to 9600 baud |