Introduction
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.
What you will need - Hardware
For this project you will need:
|
|
The Circuit
The connections are pretty easy, see the above image with the breadboard circuit schematic.
The code using Codebender
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. And that's it, you've programmed your Arduino with this sketch!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* Arduino Tutorial: How to use a magnetic contact switch Dev: Michalis Vasilakis // www.ardumotive.com // Date: 4/8/2016 */ const int buzzer = 3; const int sensor = 4; int state; // 0 close - 1 open wwitch void setup() { pinMode(sensor, INPUT_PULLUP); } void loop() { state = digitalRead(sensor); if (state == HIGH){ tone(buzzer, 400); } else{ noTone(buzzer); } delay(200); } |
Download the code from here and open it with Arduino IDE.
|
![]()
|
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.
I hope you liked this, let me know in the comments.