How to use a reed switch with an Arduino: a door alarm
Beginner
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
Wire one leg of the reed switch to a digital pin and the other to GND, and set the pin to INPUT_PULLUP. A magnet near the switch closes it and the pin reads LOW. Take the magnet away and the pull-up takes it back to HIGH. That is all the wiring there is: no resistor, no power wire, and either leg either way around.
The circuit above is a door alarm. The reed switch is on the frame and the magnet is on the door. Press Run, drag the magnet to 5 mm and the serial monitor prints "door closed". Drag it out to 30 mm, which is the door opening, and it prints "door opened, count 1", and the buzzer and the red lamp start pulsing. They stay on even after you close the door again, until you press the arcade button.
What you need
- An Arduino Uno
- A reed switch and a small magnet (or a door sensor with both)
- An active buzzer, a red LED and a 220 ohm resistor
- A pushbutton or arcade button for reset
- A half-size breadboard and jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| Reed switch, one leg | Pin 2 | INPUT_PULLUP: LOW when the magnet is near |
| Reed switch, other leg | GND | Either way around |
| Reset button | Pin 3 and GND | INPUT_PULLUP too |
| Buzzer | Pin 8 and GND | |
| Red LED, through 220 ohm | Pin 9 |
How a reed switch works
Inside the glass tube are two thin steel blades that overlap without touching. A magnet nearby magnetizes them, they pull toward each other and snap shut. Take the magnet away and their own springiness pulls them apart. There is no chip and no power: it is a switch that a magnet presses.
That is how door and window sensors, bike speedometers and washing machine lids work. Electrically it is a pushbutton, so it is wired and read like one.
The simplest reed switch sketch
This sketch prints "closed" or "open" each time the switch changes, and lights the LED on pin 13 while it is open. With INPUT_PULLUP, LOW means the magnet is there. We ran it on a simulated Uno and moved the magnet in steps: 40, 20, 16, 15, 14, 16, 18, 19, 17, 15 and 14 mm. It printed "open" at the start, "closed" at 15 mm, "open" again only at 19 mm, and "closed" at 15 mm on the way back.
const int REED = 2;
const int LAMP = 13;
int last = -1;
void setup() {
pinMode(REED, INPUT_PULLUP);
pinMode(LAMP, OUTPUT);
Serial.begin(9600);
}
void loop() {
int now = digitalRead(REED);
if (now != last) {
last = now;
digitalWrite(LAMP, now == HIGH ? HIGH : LOW);
Serial.println(now == LOW ? "closed" : "open");
}
delay(20);
}Hysteresis: it lets go further out than it grabs
Look at those numbers again. Coming in, the switch closed at 15 mm. Going out, it stayed closed at 16 and 18 mm, and only opened at 19. Going back in, it stayed open at 17 mm and closed again at 15. Once the blades touch, the magnetic path through them is better, so they hold on further out than the distance that pulled them in.
Every reed switch datasheet shows this as an operate value and a release value. It is a good thing: a door that creaks half a centimeter does not make the switch chatter on and off. The distances depend on the magnet and the switch, so on a real door, test where yours closes and opens.
How the door alarm latches
The alarm sketch waits for the reading to hold for 20 ms before it believes a change, because the blades can bounce briefly as they close. It also reads the door once at power-up and starts from there, so a door that is already open when you plug it in is not counted as a break-in.
Once it has seen the door open it latches. Shutting the door prints "door closed" but does not stop the alarm, because the point of an alarm is that it happened. Only the button on pin 3 prints "alarm reset" and turns it off.
Common mistakes
Forgetting the pull-up. With plain INPUT and no resistor the open switch leaves the pin floating, and it reads random values. Use INPUT_PULLUP.
Reading it backwards. With INPUT_PULLUP, closed is LOW. On a door, closed door means magnet near means LOW.
Bending the legs at the glass. The tube cracks easily. Bend them a few millimeters away from it, and hold the leg with pliers while you do.
Expecting it to close at the same place it opens. It does not, and that is on purpose.
Switching big loads through it. A small reed switch handles signal currents, not a motor or a mains lamp. Use it to tell the Arduino, and let the Arduino switch a relay.
Questions
How do I connect a reed switch to an Arduino?
One leg to a digital pin and the other to GND, with pinMode(pin, INPUT_PULLUP). The pin reads LOW when a magnet is close and HIGH when it is away. No resistor is needed.
Does a reed switch need a resistor?
Not with INPUT_PULLUP, which switches on the Arduino’s own pull-up. Without it, use a 10k resistor from the pin to 5 V, or the pin floats while the switch is open.
How close does the magnet need to be?
It depends on the magnet and the switch. In the simulator the switch closes at 15 mm and does not open again until 19 mm, because a reed switch holds on further out than it grabs.
Is a reed switch normally open or normally closed?
The common ones are normally open: open with no magnet, closed with one. Some door sensors are normally closed or have both contacts. Check yours with a multimeter.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.