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:

  1. Learn how a reed switch and magnet work together
  2. Wire the door sensor to the ESP32 S3 Uno
  3. Read the door state with digitalRead() and INPUT_PULLUP
  4. Detect door-opening and door-closing events in code
  5. Build a simple door alarm with an LED and a buzzer
ESP32 S3 Uno door sensor

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)
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 .

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.

Door Sensor Pinout

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.

Door Sensor How It Works Pinout

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

ESP32 S3 Uno pinout diagram

Wiring Diagram

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

The wiring diagram between ESP32 S3 Uno Door Sensor

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.

  1. Set the ESP32 S3 Uno pin as an input with the internal pull-up resistor, using pinMode(). Here the pin is GPIO46 (D9).
pinMode(46, INPUT_PULLUP);
  1. Read the pin with digitalRead(). LOW means closed, HIGH means open.
int door_state = digitalRead(46);

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.

/* * 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 */ #define DOOR_SENSOR_PIN 46 // The ESP32 S3 Uno pin GPIO46 (D9) connected to door sensor'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 } void loop() { door_state = digitalRead(DOOR_SENSOR_PIN); // read state if (door_state == HIGH) { Serial.println("The door is open"); } else { Serial.println("The door is closed"); } }

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 Arduino IDE.
  6. Upload the code by clicking the Upload button.
  7. Test it by moving the magnet close to the reed switch, then away from it.
  8. Open the Serial Monitor at 9600 baud to see the result.
  9. Tip: Hold the magnet within about 1 cm of the reed switch. If it is too far, the switch may not close.
∞
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 open The door is open The door is closed The door is closed The door is closed The door is closed The door is closed The door is open The door is open
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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.

/* * 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 */ #define DOOR_SENSOR_PIN 46 // The ESP32 S3 Uno pin GPIO46 (D9) connected to door sensor's pin int door_state; // current state of door sensor int prev_door_state; // previous 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 door_state = digitalRead(DOOR_SENSOR_PIN); // read state } void loop() { prev_door_state = door_state; // save the last state door_state = digitalRead(DOOR_SENSOR_PIN); // read new state if (prev_door_state == LOW && door_state == HIGH) { // state change: LOW -> HIGH Serial.println("The door opening is detected"); // TODO: turn on alarm, light or send notification ... } else if (prev_door_state == HIGH && door_state == LOW) { // state change: HIGH -> LOW Serial.println("The door closing is detected"); // TODO: turn off alarm, light or send notification ... } }
  1. Copy the code and open it in Arduino IDE.
  2. Upload it to the ESP32 S3 Uno with the Upload button.
  3. Test it by bringing the magnet near the reed switch, then pulling it away.
  4. Check the Serial Monitor for the messages.
  5. Tip: Put your own action (LED, buzzer, notification) where the code says TODO.
∞
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 opening is detected
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

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.

/* * 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 */ #define DOOR_SENSOR_PIN 18 // ESP32 S3 Uno pin GPIO18 (D2) #define RED_LED_PIN 20 // ESP32 S3 Uno pin GPIO20 (D5) #define BUZZER_PIN 3 // ESP32 S3 Uno pin GPIO3 (D6) const unsigned long BLINK_INTERVAL = 200; unsigned long previousMillis = 0; bool alarmState = false; void setAlarmState(uint8_t state) { if(state){ tone(BUZZER_PIN, 2000); digitalWrite(RED_LED_PIN, LOW); } else{ noTone(BUZZER_PIN); digitalWrite(RED_LED_PIN, HIGH); } } void setup() { Serial.begin(9600); pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); pinMode(RED_LED_PIN, OUTPUT); pinMode(BUZZER_PIN, OUTPUT); digitalWrite(RED_LED_PIN, HIGH); noTone(BUZZER_PIN); } void loop() { int doorState = digitalRead(DOOR_SENSOR_PIN); if (doorState == HIGH) { Serial.println("The door is open"); } else { Serial.println("The door is closed"); } // Door open if (doorState == HIGH) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= BLINK_INTERVAL) { previousMillis = currentMillis; alarmState = !alarmState; setAlarmState(alarmState); } } // Door closed else { noTone(BUZZER_PIN); digitalWrite(RED_LED_PIN, HIGH); alarmState = false; } }

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

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