Introduction
A reed switch is an electrical switch that when exposed to a magnetic field, closes a circuit and allows the flow of current.Â
In this guide, you will learn how to use a reed switch with an Arduino to turn an LED on and off.Â
After mastering this basic concept, you will be able to use reed switches in more advanced Arduino projects!
Tools
-
-
-
-
const int reed_pin = 11; // Pin connected to reed switch const int led_pin = 13; // LED pin - active-high void setup() { Serial.begin(9600); pinMode(reed_pin, INPUT_PULLUP); pinMode(led_pin, OUTPUT); } void loop() { int proximity = digitalRead(reed_pin); // Read the state of the switch if (proximity == LOW) // If the pin reads low, the switch is closed. { Serial.println("Switch closed"); digitalWrite(led_pin, HIGH); // Turn the LED on as magnet passes } else { digitalWrite(led_pin, LOW); // Turn the LED off } }