ESP32 S3 UNO - Door Sensor - LED

This ESP32 S3 Uno door sensor LED tutorial shows you how to control an LED with a magnetic door sensor on the ESP32 S3 Uno form board. The same circuit works as a window sensor or a reed switch alarm light. You will build two projects: one where the LED follows the door, and one where the door toggles the LED.

In this tutorial, you will:

  1. Wire a magnetic door sensor and an LED to the ESP32 S3 Uno
  2. Read the door state with digitalRead() and INPUT_PULLUP
  3. Turn the LED on when the door opens and off when it closes
  4. Toggle the LED each time the door closes
ESP32 S3 Uno door sensor LED

Project 1 - LED follows the door

The LED copies the door state. When the door opens, the ESP32 S3 Uno turns the LED on. When the door closes, it turns the LED off.

Project 2 - Door sensor toggles the LED

The LED changes state each time the door closes. The board watches for the moment the sensor state goes from HIGH to LOW. Then it switches the LED on if it was off, or off if it was on. Opening the door does not change the 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×Door 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 Door Sensor

This project joins one input and one output. The door sensor tells the board if the door is open or closed, and the LED shows the result. If these parts are new to you, learn each one alone first. The tutorials below explain the pinout, how each part works, and how to program it.

How the project works

A door sensor has two parts: a reed switch and a magnet. When the door is closed, the magnet is near the switch and the switch is closed. When the door opens, the magnet moves away and the switch opens.

The sensor pin uses the internal pull-up resistor of the ESP32 S3 Uno. So the pin reads LOW when the door is closed, and HIGH when the door is open. The board reads this pin in a loop and drives the LED.

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 (D2, D8) to their GPIO numbers when you build the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

One wire of the door sensor goes to D8 and the other goes to GND. The LED goes to D2 through a 220 ohm resistor.

The wiring diagram between ESP32 S3 Uno door sensor LED

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Door Sensor wire 1 D8 (GPIO21)
Door Sensor wire 2 GND
LED Anode (+) via 220 ohm resistor D2 (GPIO18)
LED Cathode (-) GND

※ NOTE THAT:

The door sensor is a simple switch with no polarity, so you can swap its two wires. It needs no extra power, because the code turns on the internal pull-up resistor. The ESP32 S3 Uno pins use 3.3V logic and are NOT 5V tolerant, so do not connect the sensor to 5V.

Application 1 - The LED state is in sync with the door sensor state

This first sketch is the simplest way to link the door and the LED. Each time through the loop, it reads the door and sets the LED to match.

ESP32 S3 Uno Code

The code sets the sensor pin as INPUT_PULLUP and the LED pin as OUTPUT. In the loop, it reads the door state. If the state is HIGH (door open), it turns the LED on and prints "The door is open". If the state is LOW (door closed), it turns the LED off and prints "The door is closed".

/* * 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-door-sensor-led */ #define DOOR_SENSOR_PIN 21 // The ESP32 S3 Uno pin GPIO21 (header D8) connected to door sensor's pin #define LED_PIN 18 // The ESP32 S3 Uno pin GPIO18 (header D2) connected to LED's pin int door_state; void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set ESP32 S3 Uno pin to input pull-up mode pinMode(LED_PIN, OUTPUT); // set ESP32 S3 Uno pin to output mode } void loop() { door_state = digitalRead(DOOR_SENSOR_PIN); // read state if (door_state == HIGH) { Serial.println("The door is open");; digitalWrite(LED_PIN, HIGH); // turn on LED } else { Serial.println("The door is closed"); digitalWrite(LED_PIN, LOW); // turn off LED } }

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. Open and close the door (or move the magnet near and away from the switch).
  2. Watch the LED. It is on while the door is open and off while the door is closed.
  3. Tip: open the Serial Monitor at 9600 baud to see the door state printed again and again.
∞
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
The door is closed The door is closed The door is open The door is open The door is closed
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Code Explanation

Every line of the sketch has a comment that explains it. In the code, the pins are GPIO numbers, not header labels: DOOR_SENSOR_PIN is 21 (header D8) and LED_PIN is 18 (header D2). Because the loop has no delay, the Serial Monitor prints the state very fast.

Application 2 - Door Sensor toggles LED

In this project, the LED does not copy the door. Instead, each closing of the door flips the LED. This is useful when you want to count or mark events, not just show the current state.

ESP32 S3 Uno Code - Door Sensor Toggles LED

The sketch keeps the previous and the current door state. When the state changes from HIGH to LOW, the door has just closed. Then the code flips led_state and writes it to the LED.

/* * 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-door-sensor-led */ #define DOOR_SENSOR_PIN 21 // The ESP32 S3 Uno pin GPIO21 (header D8) connected to door sensor's pin #define LED_PIN 18 // The ESP32 S3 Uno pin GPIO18 (header D2) connected to LED's pin int led_state = LOW; // the current state of LED int prev_door_state; // the previous state of door sensor int door_state; // the current state of door sensor void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set ESP32 S3 Uno pin to input pull-up mode pinMode(LED_PIN, OUTPUT); // set ESP32 S3 Uno pin to output mode door_state = digitalRead(DOOR_SENSOR_PIN); } void loop() { prev_door_state = door_state; // save the last state door_state = digitalRead(DOOR_SENSOR_PIN); // read new state if (prev_door_state == HIGH && door_state == LOW) { // state change: HIGH -> LOW Serial.println("The door closing is detected"); // toggle state of LED led_state = !led_state; // control LED according to the toggled state digitalWrite(LED_PIN, led_state); } }

Code Explanation

You can find the details in the comment lines of the ESP32 S3 Uno code above. The key line is led_state = !led_state. It reverses the LED state, and it does the same job as this longer code:

if(led_state == LOW) led_state = HIGH; else led_state = LOW;

Detailed Instructions

  1. Copy the code and open it in the Arduino IDE.
  2. Upload the code to the ESP32 S3 Uno.
  3. Open and close the door a few times.
  4. Watch the LED. It changes state once each time the door closes.
  5. Tip: if the LED sometimes flips twice, the switch is bouncing. Add a short delay or use a debounce method.
∞
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
The door closing is detected The door closing is detected The door closing is detected
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

The LED toggles once every time the door closes.

Video Tutorial

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

FAQ

Why does the code use 21 and 18 instead of 8 and 2?

The board has Uno header labels, but the code needs GPIO numbers. Header D8 is GPIO21 and header D2 is GPIO18. So DOOR_SENSOR_PIN is 21 and LED_PIN is 18.

Do I need a resistor for the door sensor?

No. The code uses INPUT_PULLUP, which turns on the internal pull-up resistor of the ESP32 S3 Uno. Connect one wire to D8 and the other to GND. Without the pull-up, the pin would float and give random readings.

Can I power the door sensor from 5V?

Do not connect the sensor to 5V. The ESP32 S3 Uno pins work at 3.3V and are not 5V tolerant. With INPUT_PULLUP, the sensor only needs the signal pin and GND.

Why does the LED in Application 2 sometimes toggle twice?

A reed switch can bounce, which means it opens and closes a few times very fast when the magnet moves. The code may see two closings. Add a short delay, such as 50 ms, or use a debounce method like in the button debounce tutorial.

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 more door or window sensors, 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(), digitalWrite() and INPUT_PULLUP 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 D2, and use 18 in the code
State never changes Sensor not on D8 or not on GND Connect one sensor wire to D8 and the other to GND, and use 21 in the code
Random open and closed readings Input is floating Keep pinMode set to INPUT_PULLUP and check the GND wire
Sensor always reads open Magnet too far from the switch Mount the magnet within about 1 to 2 cm of the switch when the door is closed
LED toggles twice on one close Reed switch bounce Add a short delay or debounce code
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!