How to Use a Magnetic Door sensor

NextPCB
3 min readJun 3, 2021

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.

This project Sponsor by NextPCB

NextPCB is one of the most experienced PCB manufacturers in Global, has specialized in the PCB and assembly industry for over 15 years. Not only could NextPCB provide the most innovative printed circuit boards and assembly technologies in the highest quality standards, the fastest delivery turnaround as fast as 24 hours.

Guys if you have a PCB project, please visit their website and get exciting discounts and coupons

Only 0$ for 5–10pcs PCB Prototypes :- Click Here

Register and get $100 from NextPCB :- Click Here

See more info about PCB Assembly Capabilities: Click Here

Step 1: What You Will Need

  • Arduino uno
  • Magnetic Contact Switch — Door Sensor
  • Buzzer
  • Breadboard and some cable

The Circuit

  • 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 Code

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);
}

Well Done!

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

--

--