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.

Raspberry Pi Pico door sensor

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Door Sensor
1×Jumper Wires
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)
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 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.
Door Sensor How It Works Pinout

※ 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

The wiring diagram between Raspberry Pi and Pico Door Sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Check Door Open and Close State

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-door-sensor """ from machine import Pin import utime DOOR_SENSOR_PIN = 1 # The Raspberry Pi Pico pin connected to the door sensor (GP1) # 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

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.
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 (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-door-sensor """ from machine import Pin import utime DOOR_SENSOR_PIN = 1 # The Raspberry Pi Pico pin connected to the door sensor (GP1) # 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 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.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The door closing is detected The door opening is detected
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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!