Raspberry Pi Pico - Door Sensor
This guide shows you how to check if your door or window is open or closed using a Raspberry Pi Pico and a door sensor. We will learn to set up the door sensor, connect it to the Raspberry Pi Pico, and program the Raspberry Pi Pico to detect the door's status from the sensor.
Hardware Preparation
1 | × | Raspberry Pi Pico W | |
1 | × | Raspberry Pi Pico (Alternatively) | |
1 | × | Micro USB Cable | |
1 | × | Door Sensor | |
1 | × | Jumper Wires | |
1 | × | Breadboard | |
1 | × | (Optional) Screw Terminal Expansion Board for Raspberry Pi Pico |
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 attached to the door or window that moves, while the reed switch is attached to the stationary door frame. When the door is closed, these two parts come close to each other.
- When the magnet is close to the reed switch, the circuit of the reed switch closes.
- When the magnet is far from the re doued switch, the circuit of the reed switch opens.
※ NOTE THAT:
The reed switch does not produce LOW or HIGH signals by itself. It can only be open or closed. The way we connect it to the Raspberry Pi Pico decides if the pin reads as LOW, HIGH, or gets a floating value (which is not stable). To avoid this unstable floating value, we should attach a pull-up or pull-down resistor to the pin on the Raspberry Pi Pico.
Connect one pin of the reed switch to GND and the other pin to an input pin on the Raspberry Pi Pico with a pull-up resistor. This resistor can be either built-in or added separately.
- When the magnet is close to the reed switch, the input pin of the Raspberry Pi Pico reads LOW.
- When the magnet is not close to the reod switch, the input pin of the Raspberry Pi Pico reads HIGH.
This way:
- Check if the door is open or closed by looking at the input pin of the Raspberry Pi Pico:
- If the pin is LOW, the door is closed.
- If the pin is HIGH, the door is open.
- To determine when the door opens or closes, observe any changes in the state of the input pin on the Raspberry Pi Pico:
- A change from LOW to HIGH indicates that the door is opening.
- A change from HIGH to LOW indicates that the door is closing.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code - Check Door Open and Close State
Detailed Instructions
Please follow these instructions step by step:
- Ensure that Thonny IDE is installed on your computer.
- Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
- If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
- Connect the Raspberry Pi Pico to the door sensor according to the provided diagram.
- Connect the Raspberry Pi Pico to your computer using a USB cable.
- Launch the Thonny IDE on your computer.
- On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
- In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
- Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Raspberry Pi Pico by:
- Click the Save button, or use Ctrl+S keys.
- In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
- Save the file as main.py
- Click the green Run button (or press F5) to run the script. The script will execute.
- 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.
If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.
Raspberry Pi Pico 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 Raspberry Pi Pico.
- 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.