A reed switch is an electrical switch operated by a magnetic field. It consists of two thin metal reeds sealed in a glass tube. When a magnet is brought near, the reeds attract and close the circuit; removing the magnet opens it. Reed switches are commonly used in security systems, appliances, and proximity sensors due to their simplicity, reliability, and low power consumption.
Contact Form | SPST (Normally Open) |
---|---|
Switching Voltage (Max) | 24 VDC |
Switching Current (Max) | 100 mA |
Temperature Range | -40°C to +125°C |
📄 Reed switch datasheet (97 kB)
The setup for reading a reed switch is analogous to a normal switch, which is connected between VCC and GND. A high-ohm resistor is required to prevent a short circuit and ensure that as little current as possible flows when closing the switch. A connection to a digital input is now placed between the reed switch and the resistor.
The following code reads the state of the reed switch at the input D2
of the Arduino UNO and writes
the corresponding transition to the serial console of the Arduino IDE.
#define PIN_REED 2
void setup() {
pinMode(PIN_REED, INPUT);
Serial.begin(9600);
}
void loop() {
static bool switchOn = false;
if (digitalRead(PIN_REED) == HIGH) {
if (!switchOn) {
switchOn = true;
Serial.println("Switch ON");
}
} else {
if (switchOn) {
switchOn = false;
Serial.println("Switch OFF");
}
}
delay(10);
}
The setup is almost identical to that of the Arduino, except that a different digital input is selected.
#define PIN_REED 19
void setup() {
pinMode(PIN_REED, INPUT);
Serial.begin(9600);
}
void loop() {
static bool switchOn = false;
if (digitalRead(PIN_REED) == HIGH) {
if (!switchOn) {
switchOn = true;
Serial.println("Switch ON");
}
} else {
if (switchOn) {
switchOn = false;
Serial.println("Switch OFF");
}
}
delay(10);
}