Door Sensor
Detect if door is opened or closed from a magnetic switch
Components Required
Introduction
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.
Learning outcomes:
- Using magnetic door sensor.
Theory
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
Methodology
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 in your Arduino IDE and see outputs while changing the door sensor module.
Figure 2: Door open state
Figure 3: Door closed state
Code
const int DOORpin = 32;
void setup() {
Serial.begin(9600);
pinMode (DOORpin, INPUT);
}
void loop() {
Serial.println(digitalRead(DOORpin));
delay(100);
}
Explanation
DOORpin: Defined input pin for door sensor