ESP32 S3 - Door Sensor Toggle LED

This tutorial shows you how to use the ESP32 S3 together with a magnetic door sensor to toggle an LED each time the door is closed. By wiring a reed switch-based door sensor to a GPIO input and an LED to a GPIO output, you will gain a practical understanding of how to detect door state changes and respond with a toggled 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 monitors the door sensor output pin on the ESP32 S3
  3. A system that toggles the LED state each time the door is closed, alternating between on and off with every closing event
  4. A foundation for building door-activated alarms, entry counters, and access-monitoring 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

A magnetic door sensor pairs a reed switch element with a permanent magnet to detect whether a door or window is open or closed. When the magnet is brought close to the reed switch, the switch contacts close and pull the ESP32 S3 GPIO pin LOW; when the magnet is moved away, the contacts open and the pin returns HIGH. The ESP32 S3 detects this transition and toggles the LED state on each closing event, making the LED alternate between on and off with every door closure.

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 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 Toggle LED

This image is created using Fritzing. Click to enlarge image

Do you want the wiring to stay in place? Put the ESP32 S3 on a Screw Terminal Block for ESP32 S3, then tighten each wire under a screw. The wiring stays the same. Only the wires now go to the screw terminals, as shown below.

The wiring diagram between ESP32 S3 Door Sensor Toggle LED Screw Terminal

This image is created using Fritzing. Click to enlarge image

Here is the same circuit with a GPIO Breakout Board for ESP32 S3, which breaks out every GPIO to pin headers:

The wiring diagram between ESP32 S3 Door Sensor Toggle 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 magnetic door sensor is a passive reed switch that draws negligible current and can be connected directly to any digital GPIO input on the ESP32 S3 with the internal pull-up resistor enabled in firmware.

ESP32 S3 Code

The following code monitors the door sensor connected to the ESP32 S3 and toggles the LED state each time the door closes. The sketch uses edge detection to respond only to the falling edge of the sensor signal — the moment the door transitions from open to closed — ensuring the LED toggles exactly once per closure rather than continuously while the door remains shut.

/* * 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-toggle-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-toggle-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 // variables will change: int ledState = LOW; // the current state of LED int lastDoorState; // the previous state of door sensor int currentDoorState; // the current state of door sensor 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 currentDoorState = digitalRead(DOOR_SENSOR_PIN); } void loop() { lastDoorState = currentDoorState; // save the last state currentDoorState = digitalRead(DOOR_SENSOR_PIN); // read new state if (lastDoorState == HIGH && currentDoorState == LOW) { // state change: HIGH -> LOW Serial.println("The door-closing event is detected, toggles the state of LED"); // toggle state of LED ledState = !ledState; // control LED arccoding to the toggled state digitalWrite(LED_PIN, ledState); } }

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 and then move it away. Repeat this several times.
  2. Observe the LED state toggle each time the magnet is brought close (door closed).
  3. Check the Serial Monitor to see the live door state and LED state printed in real time.
  4. Pro Tip: In the code, ledState = !ledState is a concise way to flip the LED between HIGH and LOW. It is exactly equivalent to checking the current state with an if/else block and assigning the opposite value — use this pattern whenever you need to toggle any boolean variable in your ESP32 S3 sketches.

Serial Monitor Output

The following Serial Monitor output shows the ESP32 S3 reading door sensor data in real time. Each line displays the current door state and the corresponding LED state toggled by the sensor closing event.

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

Application and Project Ideas

Once you can toggle an LED from a door sensor on the ESP32 S3, you can extend this pattern to a wide range of door-monitoring and access-control projects.

  1. Entry counter: Increment a counter each time the door closes and display the total entry count on an OLED or LCD screen connected to the ESP32 S3.
  2. Security alert system: Replace the LED with a piezo buzzer or relay-connected siren that toggles on when the door is opened unexpectedly, providing an audible intrusion alert.
  3. Cabinet access logger: Log every door open and close event with a timestamp to an SD card so you can review a history of access to a secured cabinet or enclosure.
  4. Smart notification: Use the ESP32 S3 Wi-Fi connection to send an email or MQTT message to a home automation platform whenever the door sensor detects an opening or closing event.
  5. Automated lighting: Toggle a relay-controlled light each time a storage room door is opened, so the light turns on when you enter and off when you leave without requiring a wall switch.
  6. Child safety monitor: Alert a parent via a buzzer or web notification whenever a door to a restricted area — such as a medicine cabinet or garage — is opened.

Video Tutorial

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

Challenge Yourself

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

  1. Beginner: Add a Serial Monitor message that prints a running count of how many times the door has been closed since the ESP32 S3 was last powered on.
  2. Beginner: Add a second LED wired to a different GPIO pin that mirrors the door state directly — on when the door is closed and off when open — while the first LED continues to toggle with each closing event.
  3. Intermediate: Add a push button that resets the LED to OFF and clears a door-close counter back to zero without needing to restart the ESP32 S3.
  4. Intermediate: Connect an I2C OLED display to the ESP32 S3 and show a live counter of door-close events alongside the current LED state, updating the display in real time.
  5. Advanced: Send an email or MQTT message to a home automation platform whenever the door sensor closes, using the ESP32 S3 Wi-Fi connection to report door events remotely.
  6. Advanced: Build a multi-door monitoring system using two door sensors on separate GPIO pins and display the state of each door on an LCD, toggling independent LEDs for each door.

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