Micropython Lesson 10
Using a reed switch as a door sensor
A magnetic contact switch is a reed switch encased in a plastic shell so that you can easily apply them in a door, a window or a drawer to detect if the door is open or closed.
- Using magnetic door sensor.
Almost all door and window sensors use a “reed switch” to determine when a protected area has been breached. A reed switch consists of a set of electrical connectors placed slightly apart. When a magnetic field is placed parallel to the electrical connectors, it pulls them together, closing the circuit. Door sensors have one reed switch and one magnet, creating a closed circuit. If someone opens an armed door or window, the magnet is pulled away from the switch, which breaks the circuit and triggers an event.
Figure 1: Working principle of magnetic door sensor
First, take the Magicbit door sensor and connect it with the Magicbit core. In this example, we use the 32nd pin of Magicbit to implement this. After connecting the door sensor, upload the following code for your Magicbit. Then open your serial monitor and see outputs while changing the door sensor module.
from machine import Pin import time motion_sens = Pin(32, Pin.IN) while True: print(motion_sens.value()) time.sleep_ms(1000)
When some human being is detected by the motion sensor, the output will display ‘1’, otherwise ‘0’.