Magnetic Door Sensor and Arduino

63,223

58

11

Introduction: Magnetic Door Sensor and Arduino

About: Hi, I'm a 15 year old 10th grader interested in Electronics and Coding.

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.

Be the First to Share

    Recommendations

    • Toys & Games Contest

      Toys & Games Contest
    • Big vs Small Challenge

      Big vs Small Challenge
    • Fix It Challenge

      Fix It Challenge

    11 Comments

    0
    sspence
    sspence

    5 years ago on Introduction

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

    0
    sspence
    sspence

    Reply 5 years ago on Introduction

    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

    0
    GuruI
    GuruI

    Reply 3 years ago

    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?

    0
    atilt2
    atilt2

    5 years ago

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

    0
    nbbarud
    nbbarud

    Reply 3 months ago

    I just got a random one off amazon and it works fine.

    0
    ChadD49
    ChadD49

    3 years ago

    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.

    0
    MANISH KR
    MANISH KR

    Reply 3 years ago

    sir i am also using magnetic sensor. but not working can you send me the code ?

    0
    CalvinM1
    CalvinM1

    5 years ago

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

    0
    ThomasP123
    ThomasP123

    Reply 4 years ago

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

    0
    atilt2
    atilt2

    5 years ago

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