Reed Switch with Arduino

Use a reed switch to turn an LED on and off

Written By: Madeleine Schappi

Dash icon
Difficulty
Easy
Steps icon
Steps
4

Introduction

A reed switch is an electrical switch that when exposed to a magnetic field, closes a circuit and allows the flow of current. 

In this guide, you will learn how to use a reed switch with an Arduino to turn an LED on and off. 

After mastering this basic concept, you will be able to use reed switches in more advanced Arduino projects!

Step 1   Insert Reed Switch into Breadboard

  • Insert your Reed Switch into Breadboard so that the header pins are facing your Arduin
  • This module is quite delicate - so be careful when handling it!

Step 2   Connect Arduino Digital 11 to Reed Switch

  • Connect Arduino Digital 11 to Reed Switch.

Step 3   Upload the code

const int reed_pin = 11; // Pin connected to reed switch
const int led_pin = 13; // LED pin - active-high

void setup()
{
  Serial.begin(9600);
  pinMode(reed_pin, INPUT_PULLUP);
  pinMode(led_pin, OUTPUT);
}

void loop()
{
  int proximity = digitalRead(reed_pin); // Read the state of the switch
  if (proximity == LOW) // If the pin reads low, the switch is closed.
  {
    Serial.println("Switch closed");
    digitalWrite(led_pin, HIGH); // Turn the LED on as magnet passes
  }
  else
  {
    digitalWrite(led_pin, LOW); // Turn the LED off
  }
}
  • Copy this code to your Arduino IDE.
  • Upload it to your Arduino.
  • Open the Serial Monitor.
  • As you pass a magnet by the glass tube - the circuit will close and the LED on your Arduino should turn on.
  • The normal state of a Reed switch is closed.

Step 4   Connect Arduino GND to the Reed Switch

  • Connect Arduino GND to the 'S' of the Reed Switch.
  • The Reed Switch is not polarised so you could switch around the '–' and 'S' jumper wires.
© 2021 Little Bird Electronics Pty Ltd.
Made with ❤️ in SYD. All prices inc GST. ABN 15 634 521 449. We're 🐥 @lbhq on Twitter.