Introduction: How to Use a Magnetic Door Switch / Sensor With Arduino

Picture of How to Use a Magnetic Door Switch / Sensor With Arduino

In this tutorial we will use one magnetic - contact door sw / sensor and a buzzer for making a "noise" when a door is opened. Normally the reed is 'open' (no connection between the two wires). The other half is a magnet. When the magnet is less than 13mm (0.5") away, the reed switch closes.

Step 1: What You Will Need

Picture of What You Will Need

For this project you will need:

  • Arduino uno
  • Magnetic Contact Switch - Door Sensor
  • Buzzer
  • Breadboard and some cables

Step 2: The Circuit

Picture of The Circuit

The connections are pretty easy, see the above image with the breadboard circuit schematic.

Step 3: The Code

Picture of The Code

Here's the code, embedded using Codebender!

Try downloading the Codebender plugin and clicking on the "Run on Arduino" button to program your Arduino board with this sketch. And that's it, you've programmed your Arduino with this sketch!

Step 4: Well Done!

You have successfully completed one more "How to" tutorial and you learned how to use a magnetic contact switch with Arduino.

I hope you liked this, let me know in the comments.

There will be more of them, so make sure to click Follow button!


OkaNare (author)2017-11-11

I need to change

int state; // 0 close - 1 open wwitch


int state; // 1 close - 0 open wwitch.

How do I alter the code? Please help.

This is for my wardrobe light. I just swapped the buzzer for an led.

cpainy (author)2017-08-15

Thank you! Great tutorial.

Do you have any thoughts about how long the sensor wire can be?

Codingpro (author)cpainy2017-10-22

Any length should work.

About This Instructable




Bio: Arduino Tutorials by Team
More by codebender_cc:How to use a magnetic door switch / sensor with ArduinoHow to Measure LUX With ArduinoHow to Control an RGB LED Strip - Arduino Tutorial
Add instructable to: