Martin's corner on the web

Door sensor

I have recently purchased a magnetic contact switch to toy with. These are really cheap sensors that are used to detect door (or window) opening/closing events. The principle of work is simple, the sensor consists of two parts: one is a magnet and the other is a reed switch with a smaller magnet attached. Bringing the two parts closer than 1.5cm makes the magnets attract and the two wires hanging off the sensor make contact. magnet

The applications are numerous: detecting door/window being opened/closed, mail box door sensor to notify upon new mail, pet door to examine pet’s behaviour, garage door sensor, safe door sensor and so forth. The reading can be used to trigger some event, or simply logged for analysis.

The code to use with such sensor is trivial. The sensor is connected to Digital 2 pin of the Funky v2 that is also an interrupt pin. Any change of state (open or close) will trigger a wake-up and the state of the sensor will be transmitted. One wire of the sensor (doesn’t matter which) goes to D2 and the other to GND. A weak pull-up resistor holds D2 to read ‘HIGH’ while the door is open. When closed, the sensor pulls D2 to GND thus D2 will read ‘LOW’.  I used external pull-up resistor to minimize power consumption. A value of 470K will be appropriate for long term low power operation. I only had a 150K resistor, will swap that one later. The code will wake up every two hours to check-in with the base station, this is done so that its battery level can be monitored and if necessary replaced. The sleep current is about 25uA when running on 3V battery. This means it can run for at least a year on a coin cell battery and ages on couple AAs.

IMG_2891