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:

  1. Wire an HC-SR501 PIR motion sensor and an LED to the ESP32 S3 Uno
  2. Set the time delay, range and trigger mode of the motion sensor
  3. Detect motion with digitalRead() in Arduino code
  4. Turn an LED on and off when the motion starts and stops
ESP32 S3 Uno motion sensor LED

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×LED Kit
1×LED (red)
1×LED Module
1×Alternatively, Button and LED Kit
1×220Ω Resistor
1×Breadboard
1×Jumper Wires

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 .

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.

ESP32 S3 Uno pinout diagram

Wiring Diagram

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

The wiring diagram between ESP32 S3 Uno Motion Sensor LED

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 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 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.

/* * 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-led */ #define MOTION_SENSOR_PIN 14 // The ESP32 S3 Uno pin GPIO14 (D7) connected to the OUTPUT pin of motion sensor #define LED_PIN 17 // The ESP32 S3 Uno pin GPIO17 (D3) connected to LED's pin int motion_state = LOW; // current state of motion sensor's pin int motion_state_prev = LOW; // previous state of motion sensor's pin void setup() { Serial.begin(9600); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set the ESP32 S3 Uno pin to input mode pinMode(LED_PIN, OUTPUT); // set the ESP32 S3 Uno pin to output mode } void loop() { motion_state_prev = motion_state; // store old state motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state if (motion_state_prev == LOW && motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); digitalWrite(LED_PIN, HIGH); // turn on } else if (motion_state_prev == HIGH && motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); digitalWrite(LED_PIN, LOW); // turn off } }

Detailed Instructions

  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 the Arduino IDE.
  6. Upload the code by clicking the Upload button.
Arduino IDE Upload Code
  1. Wave your hand in front of the sensor.
  2. Watch the LED. It turns on when you move and turns off a short time after you stop.
  3. 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.
∞
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

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

※ 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!