ESP32 S3 - Door Sensor - LED

This tutorial shows you how to use the ESP32 S3 together with a magnetic door sensor to control an LED automatically based on door state. By connecting a reed switch to a GPIO input and an LED to a GPIO output, you will gain a practical understanding of how to respond to door open and close events with a visual indicator on the ESP32 S3.

What you'll build:

  1. A circuit connecting a magnetic door sensor and an LED to the ESP32 S3
  2. A sketch that continuously reads the door sensor state on the ESP32 S3
  3. A system that automatically turns the LED on when the door is opened and turns it off when the door is closed
  4. A foundation for building door-activated alarms, entry indicators, and security projects

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera 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
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
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

The magnetic door sensor and an LED are an effective pairing for building door-detection projects on the ESP32 S3. The sensor consists of a reed switch and a magnet — when the magnet moves away from the reed switch as a door opens, the switch opens its contacts and the ESP32 S3 reads the resulting HIGH signal on its input pin to switch the LED on via a digital GPIO output.

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:

For toggling LED each time the door is closed, see the ESP32 S3 - Door Sensor Toggles LED tutorial.

Wiring Diagram

Connect the door sensor and LED to your ESP32 S3 by following the diagram below. Double-check all connections before applying power, paying particular attention to the LED polarity and the 220 ohm current-limiting resistor placement.

The wiring diagram between ESP32 S3 Door Sensor LED

This image is created using Fritzing. Click to enlarge image

Jumper wires can come loose when you move the project. In this case, use a Screw Terminal Block for ESP32 S3. The ESP32 S3 plugs into it, and each pin gets a screw terminal. Below is the wiring diagram with the screw terminal board.

The wiring diagram between ESP32 S3 Door Sensor LED Screw Terminal

This image is created using Fritzing. Click to enlarge image

Prefer pin headers to screws? Plug the ESP32 S3 into a GPIO Breakout Board for ESP32 S3 instead:

The wiring diagram between ESP32 S3 Door Sensor LED GPIO Breakout Board

This image is created using Fritzing. Click to enlarge image

Safety Notes

Always include the 220 ohm current-limiting resistor in series with the LED to avoid drawing excessive current through the ESP32 S3 GPIO pin, which can permanently damage the microcontroller. The reed switch is a low-voltage passive device — enable the internal pull-up resistor (INPUT_PULLUP) on the ESP32 S3 input pin to maintain a defined logic level when the switch contacts are open, preventing erratic floating-input readings.

Door Sensor Pin ESP32 S3 Pin
Wire 1 (reed switch) GND
Wire 2 (reed switch) GPIO2
LED Anode (+) GPIO38 (via 220 ohm resistor)
LED Cathode (-) GND

ESP32 S3 Code

The following code reads the state of the door sensor reed switch on the ESP32 S3 and turns the LED on immediately when the door opens, then turns it off once the door is closed again. Because the ESP32 S3 uses the internal pull-up resistor, the pin reads LOW when the magnet holds the switch closed and HIGH when the door opens and the magnet moves away.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-door-sensor-led */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-door-sensor-led */ #define DOOR_SENSOR_PIN 2 // ESP32-S3 pin GPIO2 connected to door sensor's pin #define LED_PIN 38 // ESP32-S3 pin GPIO38 connected to LED's pin int doorState; void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set ESP32-S3 pin to input pull-up mode pinMode(LED_PIN, OUTPUT); // set ESP32-S3 pin to output mode } void loop() { doorState = digitalRead(DOOR_SENSOR_PIN); // read state if (doorState == HIGH) { Serial.println("The door is open, turns on LED"); digitalWrite(LED_PIN, HIGH); // turn on LED } else { Serial.println("The door is closed, turns off LED"); digitalWrite(LED_PIN, LOW); // turn off LED } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components following the wiring diagram shown above.
  3. Connect the ESP32 S3 board to your PC using a USB Type-C cable.
  4. Open Arduino IDE on your PC.
  5. Select the correct ESP32 S3 board and COM port from the Tools menu.
  6. Copy the above code and paste it into Arduino IDE.
  7. Click the Upload button to compile and upload the code to the ESP32 S3.
Arduino IDE Upload Code
  1. Move the magnet close to the reed switch to simulate a closed door, then move it away to simulate an open door.
  2. Observe the LED turn on when the door opens.
  3. Move the magnet back close to the reed switch and observe the LED turn off when the door closes.
  4. Check the Serial Monitor to see the live door and LED states printed in real time.
  5. Pro Tip: Mount the reed switch on the door frame and the magnet on the door itself, ensuring they are aligned and within about 10 mm of each other when the door is closed — correct physical placement is the most common source of unreliable door sensor readings.

Serial Monitor Output

The following Serial Monitor output shows the ESP32 S3 reading door state data from the reed switch sensor in real time. Each line displays the current door state and the corresponding LED state triggered by the sensor output.

∞
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
[2026-06-16 09:14:01] Door: CLOSED | LED: OFF [2026-06-16 09:14:04] Door: OPEN | LED: ON [2026-06-16 09:14:07] Door: OPEN | LED: ON [2026-06-16 09:14:10] Door: CLOSED | LED: OFF
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application and Project Ideas

Once you can trigger an LED from a door sensor on the ESP32 S3, you can extend this pattern to a wide range of door-activated and entry-detection projects.

  1. Entry indicator light: Install an LED strip above a doorway that lights up whenever the door is opened, providing a clear visual signal that someone has entered or exited.
  2. Security alarm system: Replace the LED with a piezo buzzer or relay-connected siren to trigger an audible alarm whenever a door or window is opened unexpectedly.
  3. Refrigerator door alert: Mount the sensor on a refrigerator door and trigger a warning LED if the door has been left open for more than a configurable number of seconds.
  4. Access log recorder: Combine the LED indicator with an SD card or Wi-Fi connection to log every door-open event with a timestamp for a simple access audit trail.
  5. Child safety guard: Alert parents with a visual or audible indicator whenever a prohibited door such as a garage or exterior gate is opened.
  6. Smart home integration: Use the door open/close signal to trigger other smart home devices — turn on a hallway light when the front door opens and turn it off a set time after the door closes.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

Now that you can control an LED with a door sensor on the ESP32 S3, try these progressively challenging extensions to deepen your understanding and build more capable door-detection systems.

  1. Beginner: Add a Serial Monitor message that prints a timestamp using millis() each time the door first opens, so you can track how many times the door has been opened during a session.
  2. Beginner: Add a second LED wired to a different GPIO pin that blinks rapidly while the primary LED stays solid on, giving a distinct visual pattern to indicate the door is currently open.
  3. Intermediate: Add a push button that allows the user to enable or disable the door LED feature at runtime, with the LED briefly flashing twice to confirm the mode has been toggled.
  4. Intermediate: Connect an I2C OLED display to the ESP32 S3 and show a live counter of how many times the door has been opened in the current session alongside the current door state.
  5. Advanced: Send an email or MQTT message to a home automation platform whenever the door sensor is triggered, using the ESP32 S3 Wi-Fi connection to report entry events remotely.
  6. Advanced: Build a timed door-alert system that turns the LED on immediately when the door opens and triggers a second warning LED if the door remains open for more than a configurable threshold period.

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