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 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 already familiar with these concepts you can skip the following links:
Arduino Tutorial: 4.0 Introduction into digital / analog pins
How to use a Breadboard
Understanding If / Else statement in Arduino
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.
Parts you will need

Arduino Uno

BreadBoard

Jumper Wires

LEDs

220 ohm resistors
Magnetic Door Switch
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 switch
void setup()
{
pinMode(led, INPUT_PULLUP);
pinMode(sensor, OUTPUT);
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.
void setup()
{
pinMode(led, INPUT_PULLUP);
pinMode(sensor, OUTPUT);
Serial.begin (9600);
}
Void Loop()
void loop()
{
state = digitalRead(sensor);
if (state == HIGH) {
digitalWrite (led, HIGH);
Serial.println("Open");
}
else {
digitalWrite (led, LOW);
Serial.println("Closed");
}
delay(300);
}
2 Comments. Leave new
You have a bug in the code. It should be pinMode(led, OUTPUT);
Thank you for notifying me. I changed it in the code.