ESP32 C3 Super Mini - Door Sensor

A door sensor is a tiny, cheap part that tells your ESP32 C3 Super Mini when a door or window is open or closed. It is the heart of almost every DIY home security project. This tutorial shows you, step by step, how to wire and program a door sensor with the ESP32 C3 Super Mini.

In this tutorial, you'll learn:

ESP32 C3 Super Mini - Door Sensor

Hardware Preparation

1×ESP32 C3 Super Mini
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×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires

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 a magnetic switch that opens or closes a circuit when a magnet moves near it or away from it.

Other names for it:

  • Entry sensor
  • Contact sensor
  • Window sensor
  • Magnetic contact switch

Key Features:

  • Very cheap and very easy to use
  • No power supply needed by the sensor itself
  • Works like a simple button for your ESP32 C3 Super Mini
  • No moving parts to wear out
  • Easy to hide inside a door frame

Technical Specifications:

  • Type: Normally-open reed switch plus a magnet
  • Output: Simple ON/OFF contact (no voltage, no data)
  • Working distance: About 15-25 mm between magnet and switch
  • Logic level: Safe for the 3.3V-only ESP32 C3 Super Mini pins
  • Wires: 2 wires, not polarized

Door Sensor Pinout

The door sensor has two separate parts.

  • Magnet: The movable part. No wires.
  • Reed switch: The fixed part. It has two wires.
Door Sensor Pinout
  • Wire 1: No polarity. Can go to GND or to the ESP32 C3 Super Mini pin.
  • Wire 2: No polarity. Can go to GND or to the ESP32 C3 Super Mini pin.

Just like a button, we do NOT need to tell the two wires of the reed switch apart.

How Door Sensor Works

The magnet and the reed switch are mounted so that they sit side by side when the door is closed.

  • Magnet: Attach it to the door or the window (the part that moves)
  • Reed switch: Attach it to the door frame (the part that stays still)
  • Door closed: The two parts are close together
  • Door open: The two parts are far apart
Door Sensor how it works

What happens inside the reed switch:

  • Magnet near: The circuit is closed (the two wires are connected)
  • Magnet far: The circuit is open (the two wires are not connected)

※ NOTE THAT:

Just like a button, we MUST use a pull-up or pull-down resistor on the ESP32 C3 Super Mini pin that connects to the reed switch. Without it, the pin state is random. The good news: the ESP32 C3 Super Mini has a built-in pull-up resistor, so you do not need an extra part.

If we wire the reed switch with one wire to GND and the other to an ESP32 C3 Super Mini input pin with a pull-up resistor:

  • Magnet near the reed switch: The input pin is LOW
  • Magnet far from the reed switch: The input pin is HIGH

So, in your code:

  • Pin is LOW: The door is closed
  • Pin is HIGH: The door is open
  • Pin changes LOW to HIGH: The door is opening
  • Pin changes HIGH to LOW: The door is closing

Wiring Diagram between Door Sensor and ESP32 C3 Super Mini

Wiring a door sensor to the ESP32 C3 Super Mini takes only two wires.

  • Note: The ESP32 C3 Super Mini works at 3.3V only. Never feed a 5V signal into a GPIO pin.
  • Note: The reed switch has no polarity, so the two wires can be swapped.
  • Note: Keep the magnet and the reed switch within about 15 mm when the door is closed.
  • Note: Do not use GPIO8 or GPIO9. They are boot strapping pins.
The wiring diagram between ESP32 C3 Super Mini Door Sensor

This image is created using Fritzing. Click to enlarge image

Door Sensor Pin ESP32 C3 Super Mini Pin
Reed Switch Wire 1 GPIO7
Reed Switch Wire 2 GND

How To Program Door Sensor

Programming a door sensor on the ESP32 C3 Super Mini needs only two functions.

  • Set the pin as an input with the built-in pull-up by using the pinMode() function. For example, GPIO7:
pinMode(7, INPUT_PULLUP);
int door_state = digitalRead(7);
  • Tip: INPUT_PULLUP turns on the resistor inside the ESP32 C3 Super Mini, so no extra resistor is needed.

ESP32 C3 Super Mini Code - Check Door Open and Close State

This first example prints the door state again and again on the Serial Monitor.

What This Code Does:

  • Sets GPIO7 as an input with the internal pull-up resistor
  • Reads the door sensor state in every loop
  • Prints "The door is open" when the pin is HIGH
  • Prints "The door is closed" when the pin is LOW
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-door-sensor */ #define DOOR_SENSOR_PIN 7 // The ESP32 C3 SuperMini pin connected to door sensor's pin int door_state; void setup() { Serial.begin(115200); // Initialize the Serial to communicate with the Serial Monitor. pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set ESP32 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

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Wire the parts: Follow the wiring diagram above. Reed switch to GPIO7 and GND.
  • Connect the board: Plug the ESP32 C3 Super Mini into your computer with a USB Type-C cable.
  • Open Arduino IDE: Start the software on your computer.
  • Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Select the port: Pick the COM port of your board in Tools > Port.
  • Copy the code: Paste the code above into Arduino IDE.
  • Upload the code: Click the Upload button in Arduino IDE.
  • Open Serial Monitor: Set the baud rate to 115200.
  • Test it: Move the magnet close to the reed switch, then move it far away.
  • Check the result: Watch the Serial Monitor. It looks like the output below.
  • Pro Tip: If the state never changes, move the magnet closer. Most reed switches need it within about 15 mm.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 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
ESP32C3 Dev Module on COM15
2

ESP32 C3 Super Mini Code - Detect Door-opening and Door-closing Events

The first code tells you the state. This second code tells you the moment the state changes.

What This Code Does:

  • Saves the door state from the previous loop
  • Reads the new door state from GPIO7
  • Prints a message once when the door starts to open (LOW to HIGH)
  • Prints a message once when the door starts to close (HIGH to LOW)
  • Gives you a clean place to add an alarm, a light or a notification
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-door-sensor */ #define DOOR_SENSOR_PIN 7 // The ESP32 C3 SuperMini pin 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(115200); // Initialize the Serial to communicate with the Serial Monitor. pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set ESP32 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 event 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 event is detected"); // TODO: turn off alarm, light or send notification ... } }

Detailed Instructions

  • New to ESP32 C3 Super Mini? Complete our Getting Started with ESP32 C3 Super Mini tutorial first to set up your development environment.
  • Keep the same wiring: The reed switch stays on GPIO7 and GND.
  • Copy the code: Paste the code above into Arduino IDE.
  • Select the board: Make sure ESP32 C3 Super Mini is selected in Tools > Board.
  • Upload the code: Click the Upload button in Arduino IDE.
  • Open Serial Monitor: Set the baud rate to 115200.
  • Test it: Move the magnet close to the reed switch, then move it far away.
  • Check the result: Only one line is printed for each change.
  • Pro Tip: Look for the // TODO comments in the code. That is where you add your buzzer or your LED.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
The door-closing event is detected The door-opening event is detected The door-closing event is detected The door-opening event is detected
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

Line-by-line Code Explanation

The ESP32 C3 Super Mini code above contains line-by-line explanation. Please read the comments in the code!

Application/Project Ideas

Here are some fun ways to use a door sensor with your ESP32 C3 Super Mini.

  • Door Alarm: Sound a buzzer when the door opens while you are away.
  • Entry Counter: Count how many times the door was opened today.
  • Auto Light: Turn on an LED strip the moment a cupboard door opens.
  • Fridge Watcher: Warn you if the fridge door stays open too long.
  • Mailbox Alert: Send a message when the mailbox lid is opened.
  • Window Monitor: Check every window before you go to bed.

Challenge Yourself

Try these small projects to practice with the door sensor and the ESP32 C3 Super Mini.

  • Easy: Turn on an LED on GPIO3 while the door is open.
  • Easy: Print the door state only when it changes, not in every loop.
  • Medium: Add a buzzer that beeps three times on each door-opening event.
  • Medium: Count the door openings and print the total on the Serial Monitor.
  • Advanced: Start a timer when the door opens, and sound an alarm if it is still open after 30 seconds.
  • Advanced: Use the ESP32 C3 Super Mini WiFi to send a phone notification on each door-opening event.

Learn More

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