Which way is open
HIGH means the sensor is closed, and closed means the magnet is beside the sensor's other half. Put the magnet on the door and the sensor on the frame and a shut door reads HIGH, an open door LOW. Some sensors are wired the other way round, so check yours with the LED before you write the sketch's labels.
Two halves, one switch
A sensor comes in two halves: the part with the wires, which contains the switch, and a part with a magnet in it. They are fixed facing each other, one on the frame and one on the door. With the door shut they face each other, and the magnet closes the switch.
The figure shows the two states. With the door shut the magnet is near the switch, the switch is closed, SIGNAL is joined to VCC and the pin reads HIGH. Open the door and the magnet moves away. The switch opens, the pull-down takes SIGNAL to 0 V and the pin reads LOW.
So the listing's words are this block's: magnet nearby, door closed, circuit closed, SIGNAL reads HIGH.
Check yours
The listing describes every sensor in the box as a magnetic reed sensor that closes when the magnet is near. A reed switch can also be made the other way, opening when the magnet arrives, and nothing printed on the kit says which one is in your hand. So test it:
- Wire GND and VCC, and screw the sensor into the terminal.
- With the magnet far away, the LED is dark. Bring it close and the LED lights.
If it does, the sensor is the normal kind, and HIGH means closed. If the LED is lit when the magnet is away and goes out when it arrives, it is the other kind, and the sketch's two branches swap. A meter set to continuity across the two wires gives the same answer without the board.
The sketch's words
door closed (magnet near) and door open are printed by the sketch from a
rule it cannot check. It assumes the magnet is on the door and the sensor
on the frame, and that the sensor is the normal kind. If you mount it the
other way, or have the other kind, the numbers are right and the words are
backwards. Change the two strings, not the wiring.
When it does not work
Either the magnet is not close enough with the door shut, or your sensor is the kind that opens when the magnet arrives. Watch the LED while you move the magnet: if the LED goes out with the magnet beside it, the sensor is that kind and the sketch must swap its two labels.
The magnet is still inside the range where the sensor stays closed. Mount the magnet closer to the sensor's face when the door is shut, so that opening the door a little takes it well out of range.
One half has the two wires and the other has none: the one with none is the magnet. Hold the half with the wires still and bring the other near: the LED on the board lights when the sensor closes.
LOW, as if the door were open, because an open sensor and a missing one look the same to the pin. That is a reasonable way to fail for an alarm: it shows open. It is a poor one for a counter, which counts a false opening.
Edit this page — content/books/door-sensor/which-way-is-open.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Door Sensor
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.