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:
- How to connect the door sensor to ESP32 board and how to install in on the door.
- How to write MicroPython code for ESP32 to read the door's state.
- How to write MicroPython code for ESP32 to detect the door's openning/closing events.
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 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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
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.
※ 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
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and door sensor using screw terminal block breakout board
ESP32 MicroPython Code - Check Door Open and Close State
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.
ESP32 MicroPython Code - Detect Door-opening and Door-closing Events
- 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.