door sensor/Reading it/07. Which way is open
Reading it · 07 of 10

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.

What the pin reads and what the door is doing
The door
Sensor type
Door
shut
Sensor
normally open
Pin reads
HIGH
The door is shut, so the magnet sits beside the sensor and its contact closes: SIGNAL gets VCC and reads HIGH, and the LED lights. ACTIVE HIGH means HIGH while the contact is closed. Which way that is for the door depends on the type: to find out which you hold, put the magnet to the sensor and watch the LED. Lit with the magnet near is normally open, which is what the listing describes for the sensors in the box.

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:

  1. Wire GND and VCC, and screw the sensor into the terminal.
  2. 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

My door reads open when it is shut.

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.

It reads closed with the door open a crack.

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.

I cannot tell which half of the sensor holds the magnet.

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.

What does it read if the sensor wire comes off?

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.

Where this goes next

Surface, recessed or metal: which one goes where.

Choosing a sensor →

Edit this page — content/books/door-sensor/which-way-is-open.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

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.

Browse Modules and blocks on the forum →