Interfacing Magnetic Reed Switch with Arduino

800 views

Introduction

Magnetic reed switches are commonly used in electronic circuits to detect the presence or absence of a magnetic field. They are widely used in security systems, door sensors, and automation control systems. In this article, we will explore how to interface a “Magnetic Reed Switch with an Arduino” microcontroller. We will cover the basic working principle, hardware requirements, and programming steps to build a functional magnetic reed switch circuit.

What is Magnetic Reed Switch?

A magnetic reed switch is an electronic switch that operates based on the presence or absence of a magnetic field. It is made up of two ferromagnetic reeds, which are thin strips of magnetic material such as nickel, iron, or cobalt. The reeds are hermetically sealed within a glass or metal tube and are held slightly apart by a spring.

When a magnetic field is applied to the switch, the magnetic force causes the reeds to attract each other and make contact, closing the switch. When the magnetic field is removed, the reeds spring back to their original position, and the switch opens. This makes magnetic reed switches useful for detecting the presence or absence of a magnetic field in a wide range of applications, including door and window sensors, security systems, and automotive applications.

Hardware Components

To interface Magnetic Reed Switch with Arduino, you’ll need the following hardware components to get started:

ComponentsValueQty
Arduino UNO1
USB Cable Type A to B1
DC Power for Arduino1
LED 1
ResistorΩ1
Magnetic Switch1
Jumper Wires1

Magnetic Switch Pinout

Magnetic Switch Pinout
Pin NamePin Description
GNDGround pin
SIGSignal Pin

Magnetic Reed Switch Circuit

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoLED LIGHTMagnetic Switch
GNDGNDGND
D5SIG
D12ANODE +

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

//Choose the Pin to which you have connected the LED
int LED = 12;
 
//Choose the Pin to which you have connected the Reed switch
int reed_switch = 5;
 
//Variable to store the reed switch status
int reed_status;
 
 
void setup()
 
{
  pinMode(LED, OUTPUT);
 
  //Enable internal pullup
  pinMode(reed_switch, INPUT_PULLUP);
}
 
void loop()
 
{
  reed_status = digitalRead(reed_switch);
 
//check if the magnet is not present
  if (reed_status == 1) {
    digitalWrite(LED, LOW);
  }
 
//check if the magnet is present
  else
  {
    digitalWrite(LED, HIGH);
  }
  delay(1000);
}

Code Explanation

This Arduino code is used to interface a magnetic reed switch with an LED connected to the Arduino board.

First, the code defines two variables – one for the LED pin (12) and one for the reed switch pin (5). It also defines a variable called reed_status to store the status of the reed switch.

In the setup function, the LED pin is set as an output, and the reed switch pin is set as an input with an internal pullup enabled.

In the loop function, the code reads the status of the reed switch using the digitalRead function and stores it in the reed_status variable.

If the reed switch status is HIGH (1), which means the magnetic field is not present, then the LED is turned off by setting its pin to LOW. If the reed switch status is LOW (0), which means the magnetic field is present, then the LED is turned on by setting its pin to HIGH.

Finally, a delay of 1000 milliseconds is added to the loop to give time for the LED to be observed before the code runs again.

Applications

Here are some applications of magnetic reed switches:

  • Security systems and burglar alarms
  • Door and window sensors
  • Water level detection systems
  • Proximity sensors
  • Speed sensors in machinery
  • Robotics and automation systems
  • Flow meters in liquid and gas pipelines
  • Position sensing in linear and rotary actuators
  • Traffic counters and signal controls
  • Medical equipment such as infusion pumps and blood analyzers

Conclusion.

Magnetic reed switch with an Arduino opens up a world of possibilities for electronic projects. With the ability to detect magnetic fields, this simple component can be used in a wide range of applications.