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.

Comments

author
sspence (author)2015-09-04

You forgot either a pulldown resistor or pinMode(switchPin, INPUT_PULLUP); and switch the digitalWrite statements.

author
rsaiyid (author)sspence2015-09-05

I'm sorry I don't understand?

author
sspence (author)rsaiyid2015-09-05

Floating inputs. Inputs need to be pulled up to +5 or pulled down to ground with a resistor, around 10k is good, otherwise the input status is not confirmed. http://forum.arduino.cc/index.php?topic=26629.0

author
GuruI (author)sspence2017-06-26

Correct, just need to add

pinMode(REED_PIN, INPUT_PULLUP);

Im not sure if you need resistor here but for other boards which has no pullup you need 10K resistor?

author
ChadD49 (author)2017-08-05

I'm creating a project that includes a magnetic door sensor like this
and this helped immensely with getting me started. I appreciate the
effort! Don't forget the LED resistor though as that bad boy will burn
out if run without a resistor for too long.

author
CalvinM1 (author)2016-03-24

Does anyone know how to add a timer to count for how long has the switch been HIGH?

author
ThomasP123 (author)CalvinM12016-07-09

https://www.arduino.cc/en/Reference/Millis

author
atilt2 (author)2016-03-28

What kind of reed switch did you use here? Can you post a link?

author
atilt2 (author)2016-03-28

What kind of reed switch did you use here? Can you post a link?

About This Instructable

42,561views

52favorites

License:

Bio: Hi, I'm a 15 year old 10th grader interested in Electronics and Coding.
More by rsaiyid:Magnetic Door Sensor and ArduinoMini LED Tester
Add instructable to: