Arduino MicroPython Door Sensor

This guide teaches you how to use a door sensor with an Arduino and MicroPython to check if a door or window is open or closed. You will learn:

Arduino MicroPython door sensor

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Door Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Mega/Giga
1×(Recommended) Enclosure For Arduino Giga
1×(Recommended) Power Splitter For Arduino Giga

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 Arduino 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 Arduino.

Connect one pin of the reed switch to GND and the other to an input pin on the Arduino with a pull-up resistor, either built-in or external.

  • When the magnet is close to the reed switch, the Arduino input pin reads LOW.
  • When the magnet is away from the reed switch, the Arduino input pin reads HIGH.

This setup allows you to:

  • Check if the door is open or closed by reading the Arduino input pin:
    • If the pin reads LOW, the door is closed.
    • If the pin reads HIGH, the door is open.
  • Detect when the door is opening or closing by monitoring changes in the input pin state:
    • A change from LOW to HIGH indicates the door is opening.
    • A change from HIGH to LOW indicates the door is closing.

Wiring Diagram

The wiring diagram between Arduino MicroPython Door Sensor

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code - Check Door Open and Close State

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-door-sensor """ from machine import Pin import utime DOOR_SENSOR_PIN = 'D2' # The Arduino Giga WiFi pin D2 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 run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the door sensor according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • 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 (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

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

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-door-sensor """ from machine import Pin import utime DOOR_SENSOR_PIN = 'D2' # The Arduino Giga WiFi pin D2 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 Arduino MicroPython code and paste it into Thonny's editor.
  • Save the code to your Arduino.
  • Click the green Run button (or press F5) to execute the code.
  • 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 (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

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!