ESP32 MicroPython Door Sensor

This tutorial instructs you how to use the door sensor with ESP32 and MicroPython to check if your door or window is open or closed. In detail, we will learn:

ESP32 MicroPython door sensor

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Door Sensor
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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

Pinout

The door sensor consists of two components:

  • One reed switch with two metal connectors
  • One magnet
Door Sensor Pinout

Just like a normal switch or button, we do not need to worry about the two pins of the reed switch.

How It Works

The magnet is fixed to the moving part of the door or window, while the reed switch is attached to the door frame. When the door is closed, the magnet and the reed switch are near each other.

  • When the magnet is near the reed switch, the reed switch circuit closes.
  • When the magnet is away from the reed switch, the reed switch circuit opens.
Door Sensor How It Works Pinout

※ NOTE THAT:

The reed switch doesn't create LOW or HIGH signals on its own. It can only be in an open or closed state. How we connect it to the ESP32 determines if the pin reads as LOW, HIGH, or a floating value (which is unstable). To prevent this unstable floating value, we should connect a pull-up or pull-down resistor to the pin on the ESP32.

Connect one pin of the reed switch to GND and the other pin to an input pin on the ESP32, using a pull-up resistor. This resistor can be built-in or added separately.

  • When the magnet is near the reed switch, the ESP32 input pin reads LOW.
  • When the magnet is away from the reed switch, the ESP32 input pin reads HIGH.

This allows you to:

  • Check if the door is open or closed by reading the input pin on the ESP32:
    • If the pin is LOW, the door is closed.
    • If the pin is HIGH, the door is open.
  • Determine when the door is opening or closing by detecting changes in the input pin state:
    • A change from LOW to HIGH means the door is opening.
    • A change from HIGH to LOW means the door is closing.

Wiring Diagram

  • How to connect ESP32 and door sensor using breadboard
The wiring diagram between ESP32 MicroPython Door Sensor

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and door sensor

ESP32 MicroPython Code - Check Door Open and Close State

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-door-sensor """ from machine import Pin import utime DOOR_SENSOR_PIN = 19 # The ESP32 pin GPIO19 connected to the door sensor # Create a Pin object to handle the input from the door sensor # Input pull-up mode is activated by setting Pin.PULL_UP door_sensor = Pin(DOOR_SENSOR_PIN, Pin.IN, Pin.PULL_UP) while True: # Read the current state of the door sensor door_state = door_sensor.value() # Check if the door is open or closed based on the state if door_state == 1: print("The door is open") else: print("The door is closed") # Sleep for a short period to reduce output flooding utime.sleep(0.5)

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the door sensor according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Move the magnet close to the reed switch and then pull it away.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 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
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

ESP32 MicroPython Code - Detect Door-opening and Door-closing Events

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-door-sensor """ from machine import Pin import utime DOOR_SENSOR_PIN = 19 # The ESP32 pin GPIO19 connected to the door sensor # Set up the door sensor pin with input pull-up mode door_sensor = Pin(DOOR_SENSOR_PIN, Pin.IN, Pin.PULL_UP) # Read the initial state of the door sensor door_state = door_sensor.value() prev_door_state = door_state # Initialize previous state to the current state while True: # Update the previous state prev_door_state = door_state # Read the new state of the door sensor door_state = door_sensor.value() # Check for state change from LOW to HIGH (door opening) if prev_door_state == 0 and door_state == 1: print("The door opening is detected") # Add actions here such as turning on an alarm, light, or sending a notification # Check for state change from HIGH to LOW (door closing) elif prev_door_state == 1 and door_state == 0: print("The door closing is detected") # Add actions here such as turning off an alarm, light, or sending a notification # Sleep for a short period to debounce the sensor utime.sleep(0.1)
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32.
  • Click the green Run button (or press F5) to execute the script.
  • Hold a magnet close to the reed switch and then move it away.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The door closing is detected The door opening is detected
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Video Tutorial

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!