In this sensor tutorial, we will use one magnetic door switch and an LED for detecting if a door is open or not. In a normal state, the reed is open. There is no connection between the two wires. One half is a magnet. When the magnet is close to the reed the switch closes.
What you will need to know?
Before continuing you should have read a couple of tutorials to understand how to use digital pins. This tutorial, will use a digital pin and connect the magnetic door switch to it. Also, you will need to know how a breadboard works. If you are unfamiliar with these concepts you can skip the following links:
Magnetic Door Switch
The magnetic door switch is a simple reed switch that closes when a magnet is close to the contact. Thanks to this effect you are able to sense whether a door/window is opened or closed. When the switch is closed it will send a HIGH signal to the Arduino and a LOW signal if the switch is open.
Materials you will need
Component | Number |
---|---|
Arduino Uno Rev3 | 1x |
Jumper Wires | 5x |
Breadboard 400 points | 1x |
220Ω resistor | 1x |
Magnetic door switch | 1x |
Circuit Diagram

Prepare The BreadBoard
The breadboard layout is quite easy for the magnetic door switch. You will not use the power lines in this tutorial since all the power to the components comes from the digital pins.
Firstly, you connect the ground to the breadboard. Connect the LED with the ground and connect the other anode through a 220-ohm resistor to digital pin 8.
Secondly, the magnetic door switch is connected to pin 4 and to the ground of the breadboard. This allows us to detect the signal.
The Code
// Arduino Sensor Tutorial: How to use a magnetic contact switch
// www.arduinoplatform.com
const int led = 8;
const int sensor = 4;
int state; // 0 close - 1 open wwitch
void setup()
{
pinMode(led, OUTPUT);
pinMode(sensor, INPUT_PULLUP);
Serial.begin (9600);
}
void loop()
{
state = digitalRead(sensor);
if (state == HIGH) {
digitalWrite (led, HIGH);
Serial.println("Open");
}
else {
digitalWrite (led, LOW);
Serial.println("Closed");
}
delay(300);
}
Code Explanation
Create the variables
Firstly, we define the variables that are used for controlling the LED and storing the state of the magnetic door switch.
const int led = 8;
const int sensor = 4;
int state; // 0 close - 1 open switch
Void Setup()
In the setup part of this sketch you will need to set the pinmode of the LED and the magnetic door switch. The LED is set to an output mode and the magnetic door switch variabe is set to an input mode. Furthermore, to be able to communicate with the Serial Monitor at the PC we start serial communication.
pinMode(led, OUTPUT);
pinMode(sensor, INPUT_PULLUP);
Serial.begin (9600);
Void Loop()
You come to the part in the code where you control the LED and read the state of the magnetic door switch. Firstly, you will need to read the state of the sensor (magnetic door switch) by using digitalRead. The state is then LOW or HIGH. Depending on the state of the sensor an IF / ELSE statement is created to either lit the LED up or to turn it off. Also, we print the state of the switch to the Serial Monitor.
void loop()
{
state = digitalRead(sensor);
if (state == HIGH) {
digitalWrite (led, HIGH);
Serial.println("Open");
}
else {
digitalWrite (led, LOW);
Serial.println("Closed");
}
delay(300);
}
Become a member
You just read a free post but there are 2 member-only posts that you don't currently have access to.
Subscribe for $14.99 monthly or $99.99 yearly.
Comments
Comments are for members only.
Please become a member or sign in to join the conversation!