In this tutorial we will use one magnetic 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- Arduino uno
- Magnetic Contact Switch - Door Sensor
- Buzzer
- Breadboard and some cable
- Connect the positive pin (The Longer Lead) of the LED to pin 3 of the Arduino.
- Connect the negative pin (The Shorter Pin) of the buzzer pin to pin GND (Ground) of the Arduino. 3and 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.
- 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 4 of the Arduino.
Assemble the circuit
The connections are pretty easy, see the above image with the breadboard circuit schematic.
The Codeconst 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);
}
Well Done!You have successfully completed one more "How to" tutorial and you learned how to use a magnetic contact switch with Arduino.
Comments