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:
- A circuit connecting a magnetic door sensor and an LED to the ESP32 S3
- A sketch that continuously monitors the door sensor output pin on the ESP32 S3
- A system that toggles the LED state each time the door is closed, alternating between on and off with every closing event
- A foundation for building door-activated alarms, entry counters, and access-monitoring projects
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 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 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.

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.

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:

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.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Wire the components following the wiring diagram shown above.
- Connect the ESP32 S3 board to your PC using a USB Type-C cable.
- Open Arduino IDE on your PC.
- Select the correct ESP32 S3 board and COM port from the Tools menu.
- Copy the above code and paste it into Arduino IDE.
- Click the Upload button to compile and upload the code to the ESP32 S3.

- Move the magnet close to the reed switch and then move it away. Repeat this several times.
- Observe the LED state toggle each time the magnet is brought close (door closed).
- Check the Serial Monitor to see the live door state and LED state printed in real time.
- 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.
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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.