Introduction: Magnetic Door Sensor and Arduino
Hello,
So I wanted to connect a magnetic door sensor to my door, to log basically if anyone came into my room while I was away at school, or if I entered the room after my school time, it'd play some cool welcoming sound. The following instructable is your guide to setting up a reed switch to do whatever you want with it. In this example I'll be connecting it to a simple LED, but so much more can be done.
Step 1: Materials
You don't need many items for this, but for this example you need:
- Arduino Uno
- Magnetic Door Sensor (Reed Switch)
- Jumper Wires
- LED
Step 2: Harware
LED:
Connect the positive pin (The Longer Lead) of the LED to pin 13 of the Arduino.
Connect the negative pin (The Shorter Pin) of the LED to pin GND (Ground) of the Arduino. 13 and GND should be next to each other.
Magnetic Door Sensor (Reed Switch):
Since the switch is non polar, you can plug in the wires any way.
I found that using jumper wires to connect the switch to the Arduino was easier as the switch's wires are strands instead of solid core, making it harder to plug into an Arduino pin.
Connect one wire of the switch to a jumper wire and plug the jumper wire pin to pin GND on the POWER side of the Arduino
Connect the other switch wire to another jumper wire, and plug that jumper wire into pin 2 of the Arduino.
Step 3: Code
/*
Rameez Saiyid
Reed Switch and Arduino
4.9.15
*/
const int switchPin = 2;
const int ledPin = 13;
void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(switchPin, HIGH);
}
void loop() {
if(digitalRead(switchPin) == LOW){
digitalWrite(ledPin, LOW);
}
else{
digitalWrite(ledPin, HIGH);
}
}
Step 4: All Done!
Once the code of this example has run, move a magnet away and closer to the sensor and watch how the LED reacts!
Congrats! You've successfully connected an Arduino to a Reed Switch. Now you're fully capable of modifying my code to change the LED output to anything you want to do when a magnet comes close to the switch.