ESP32 S3 UNO - Door Sensor
This ESP32 S3 Uno door sensor tutorial shows you how to check if a door or window is open or closed. You will wire a magnetic contact sensor (also called an entry sensor or window sensor) to the ESP32 S3 Uno form board and read its state with Arduino code. The Serial Monitor tells you each time the door opens or closes.
In this tutorial, you will:
- Learn how a reed switch and magnet work together
- Wire the door sensor to the ESP32 S3 Uno
- Read the door state with digitalRead() and INPUT_PULLUP
- Detect door-opening and door-closing events in code
- Build a simple door alarm with an LED and a buzzer

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 |
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Door Sensor
A door sensor is one of the simplest security parts you can use. It has no chip and needs no library, so it is a great first sensor for a home alarm project.
Pinout
The door sensor comes in two pieces. One piece is a reed switch with two wires. The other piece is a magnet with no wires at all.

The two wires of the reed switch are the same. Just like a push button, it does not matter which wire goes where.
How It Works
You stick the magnet on the part that moves (the door or window). You stick the reed switch on the part that stays still (the door frame). When the door is shut, the two pieces sit close together.
Magnet near the switch
The magnet pulls the metal contacts inside the reed switch together, so the circuit is closed.
Magnet away from the switch
The contacts spring apart, so the circuit is open.

※ NOTE THAT:
A reed switch does not send a HIGH or LOW signal by itself. It only opens or closes a circuit. If you connect it to the ESP32 S3 Uno without a pull-up or pull-down resistor, the input pin floats and gives random values. The easy fix is the built-in pull-up resistor of the ESP32 S3 Uno, which you turn on with INPUT_PULLUP.
Reading the state
Connect one wire of the reed switch to GND and the other wire to an ESP32 S3 Uno input pin with the pull-up turned on. Now the pin reads LOW when the magnet is near (door closed) and HIGH when the magnet is away (door open).
Reading the events
To catch the moment the door moves, compare the new reading with the last one. A change from LOW to HIGH means the door is opening. A change from HIGH to LOW means the door is closing.
ESP32 S3 Uno Pinout
The image below shows the pinout of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D9, GND…) and their GPIO numbers when you wire the door sensor.

Wiring Diagram
The door sensor needs only two wires. One goes to pin D9 and the other goes to GND.

This image is created using Fritzing. Click to enlarge image
| Door Sensor Pin | ESP32 S3 Uno Pin |
|---|---|
| Reed switch wire 1 | D9 (GPIO46) |
| Reed switch wire 2 | GND |
WARNING
D9 is GPIO46, a boot strapping pin of the ESP32 S3 Uno. The chip checks this pin at power-on to pick its boot mode. A reed switch to GND keeps the pin LOW, which is the safe default, so the board normally boots and uploads fine. Never connect D9 to 3.3V or 5V. If the board will not boot or upload, unplug the sensor wire, upload, then plug it back.
How To Program For Door Sensor
You only need two built-in functions to read a door sensor. No extra library is required.
- Set the ESP32 S3 Uno pin as an input with the internal pull-up resistor, using pinMode(). Here the pin is GPIO46 (D9).
ESP32 S3 Uno Code - Check Door Open and Close State
This sketch reads the door sensor again and again. It prints "The door is open" or "The door is closed" on every loop, so you always see the current state.
Detailed Instructions
- New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
- Wire it up as shown in the diagram.
- Connect the board to your computer with a USB Type-C cable.
- Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
- Copy the code above and paste it into Arduino IDE.
- Upload the code by clicking the Upload button.
- Test it by moving the magnet close to the reed switch, then away from it.
- Open the Serial Monitor at 9600 baud to see the result.
- Tip: Hold the magnet within about 1 cm of the reed switch. If it is too far, the switch may not close.
ESP32 S3 Uno Code - Detect Door-opening and Door-closing Events
Often you do not care about the state itself. You want to act at the moment the door moves, for example to start an alarm. This sketch saves the last reading and prints a message only when the state changes.
- Copy the code and open it in Arduino IDE.
- Upload it to the ESP32 S3 Uno with the Upload button.
- Test it by bringing the magnet near the reed switch, then pulling it away.
- Check the Serial Monitor for the messages.
- Tip: Put your own action (LED, buzzer, notification) where the code says TODO.
Video Tutorial
Watch the video below to see this ESP32 S3 Uno project step by step.
The demo in the video uses the code below. It adds a red LED on D5 (GPIO20) and a buzzer on D6 (GPIO3). When the door is open, the LED and the buzzer blink together as an alarm.
WARNING
In the demo, the door sensor is on D2 (GPIO18), and the buzzer is on D6 (GPIO3). GPIO3 is a boot strapping pin. A buzzer is an output, so it is usually safe, but if the board does not boot or upload, disconnect the buzzer and try again.
FAQ
Do I need a resistor to connect a door sensor to the ESP32 S3 Uno?
No. The code uses INPUT_PULLUP, which turns on the internal pull-up resistor of the ESP32 S3 Uno. Wire one reed switch wire to the input pin and the other to GND. The pin reads HIGH when the door is open and LOW when it is closed.
Can I use the UNO R4 door sensor code on the ESP32 S3 Uno?
Yes. The logic is the same. You only need to write the GPIO number instead of the Uno pin number. For example, pin 9 on the UNO R4 becomes 46 (GPIO46) on the ESP32 S3 Uno. Also keep in mind that ESP32 S3 Uno pins use 3.3V logic and are not 5V tolerant.
Is D9 a good pin for the door sensor on the ESP32 S3 Uno?
D9 is GPIO46, a boot strapping pin. With a reed switch to GND it works well, because the pin stays LOW at boot. If you have a free normal pin like D4 (GPIO19) or D7 (GPIO14), it is an even safer choice. Just change DOOR_SENSOR_PIN in the code.
Why does the Serial Monitor print random open and closed messages?
The input pin is floating. This happens if you forget INPUT_PULLUP or if a wire is loose. Check the code and push the wires firmly into place.
How many door sensors can I connect to one ESP32 S3 Uno?
Each door sensor uses one input pin and GND. You can connect one sensor per free digital pin. Give each one its own DOOR_SENSOR_PIN and read them one by one in loop().
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 are I/O, PWM and analog. GPIO45 and GPIO35 to GPIO42 are I/O and PWM. GPIO47 and GPIO48 are output only, with PWM. Solder pin headers to use them and write the GPIO number in code. For extra door sensors, GPIO35 to GPIO42, GPIO15 and GPIO16 are good choices. Do not use GPIO47 or GPIO48, because they cannot read inputs. Two warnings: GPIO47 and 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. Also, 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.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No COM port appears in Arduino IDE | Missing USB driver or the board is not in upload mode | Install the CP210x or CH340 driver, try another USB cable, or hold BOOT while pressing RESET |
| Always prints The door is open | Wire not connected to D9 or to GND | Check that one wire goes to D9 (GPIO46), the other to GND, and that the code uses pin 46 |
| Always prints The door is closed | Reed switch wire shorted to GND or magnet too close | Check the wires for a short and move the magnet away to test |
| Random open and closed messages | Floating input or loose wire | Use INPUT_PULLUP in the code and push the wires firmly into place |
| Nothing changes when the magnet moves | Magnet too far or on the wrong side of the switch | Hold the magnet within about 1 cm and line it up with the reed switch |
| Board does not boot or upload | Sensor wire on D9 (GPIO46) or buzzer on D6 (GPIO3) affects boot | Unplug the wire, upload the code, then plug it back |
| Serial Monitor shows strange characters | Wrong baud rate | Set the Serial Monitor to 9600 baud |