Magnetic Door Sensor and Arduino





Introduction: Magnetic Door Sensor and Arduino

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


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


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



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);



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.



    • Casting Contest

      Casting Contest
    • Make it Move Contest

      Make it Move Contest
    • Clocks Contest

      Clocks Contest

    We have a be nice policy.
    Please be positive and constructive.




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

    3 replies

    I'm sorry I don't understand?

    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.

    Correct, just need to add


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

    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.

    1 reply

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

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

    1 reply

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

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