Learn to code

ARDUINO & CODES

Previous Post
Detecting touch with Capacitive Touch Sensor (TTP223B)
Next Post
Reading analog or digital values from Infrared sensors with Arduino

How to use a Magnetic Contact Switch with Arduino

in Arduino Sensors

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

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 switch

void setup()
{
  pinMode(sensor, 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(sensor, INPUT_PULLUP);
  pinMode(sensor, OUTPUT);
  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);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu