How to Use a Magnetic Door Switch / Sensor With Arduino

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

About: Arduino Tutorials by Codebender.cc Team

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

For this project you will need:

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

Step 2: The Circuit

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

Step 3: 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!

1 Person Made This Project!

Recommendations

  • One Board Contest

    One Board Contest
  • Photography Challenge

    Photography Challenge
  • Anything Goes Contest 2021

    Anything Goes Contest 2021

8 Comments

0
shukriluccen
shukriluccen

Question 10 months ago on Step 4

instead of buzzer how i can send the signal to the smartphone?

0
vledbetter1
vledbetter1

1 year ago

Does any one know if I could use the switch to power the arduino? My thought is a battery operated system so I am wanting a light and an audio file to play when the door is open. So I am hoping for battery conservation to only power the arduini when the door is open. I am thinking. Open door closes the circuit to the power source. If not, i know i can do an if statement with the switch but i can not find the idle battery consumption. Any ideas would be greatly appreciated.

0
KhineP
KhineP

Question 3 years ago on Step 1

what types of using magnetic Switch?

0
OkaNare
OkaNare

4 years ago

I need to change

int state; // 0 close - 1 open wwitch

to

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.

0
ArjunP49
ArjunP49

Reply 3 years ago

use PNP transistor for switching

0
cpainy
cpainy

4 years ago

Thank you! Great tutorial.

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

0
Codingpro
Codingpro

Reply 4 years ago

Any length should work.