loading

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.

<p>You forgot either a pulldown resistor or pinMode(switchPin, INPUT_PULLUP); and switch the digitalWrite statements.</p>
<p>I'm sorry I don't understand?</p>
<p>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</p>
<p>Does anyone know how to add a timer to count for how long has the switch been HIGH?</p>
<p>https://www.arduino.cc/en/Reference/Millis</p>
<p>What kind of reed switch did you use here? Can you post a link? </p>
<p>What kind of reed switch did you use here? Can you post a link? </p>

About This Instructable

37,395views

48favorites

License:

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