MechatronicsLAB
2 min readJun 7, 2021

How Test Reed Switch in Arduino |The Ultimate Beginner’s Guide

A reed switch detects the presence of a magnet. A cylindrical glass tube that detects the current magnet field from a magnet is the main feature of a reed switch. In this guide, I show you how you will use the Arduino to test a reed switch.

Parts List

if you want to complete How Test Reed Switch in Arduino Tutorial, you have to need the following component

  1. Arduino uno — — — amazon/ banggood/ aliexpress
  2. Reed switch Module — — — amazon/ banggood/ aliexpress
  3. Breadboard — — — amazon/ banggood/ aliexpress
  4. Connecting Wires —amazon/ banggood/ aliexpress

Schematic Diagram

Arduino with Reed Switch

Arduino — — — - Reed switch Connection

3.3-V — — — — -VCC

GND — — — — GND

Digital pin 7 — — — D0 pin

Program Arduino with Reed Switch Test

int ReedSwitchPin = 7;
int RawValue = 0;

void setup()
{
pinMode(ReedSwitchPin, INPUT);
Serial.begin(9600);
Serial.println(“Reed Switch Test ….”);
}

void loop()
{
RawValue = digitalRead(ReedSwitchPin);
if (RawValue == 0)
{
Serial.print(“Reed Switch Detects Magnet … “);
}
else
{
Serial.print(“No Magnet Detected …”);
}

// 1 is off, 0 is activated by magnet
Serial.print(“ , RawValue: “);
Serial.println(RawValue);
}

Upload your Arduino program and start the Serial Monitor. The output shows the magnetic field detection status and the associated raw value read from the reed switch. Take a magnet and put it near the reed switch. You should see the messages being printed to the Serial Monitor to indicate a magnetic field has been detected.

Program Analysis

int ReedSwitchPin = 7;
Assigning the D0 pin on the reed switch to the Arduino digital pin 7 to output the state of the magnetic field detection.

int RawValue = 0;
Initialization of RawValue, which holds the read value from the reed to 0.

void setup()
Defining the setup() function, which:

pinMode(ReedSwitchPin, INPUT);
Sets the mode of the pin that reads in data from the reed switch to INPUT.

Serial.begin(9600);
Serial.println(“Reed Switch Test ….”);
Starts the Serial Monitor and prints a message that the program is running.

void loop()
Defining the loop() function, which:

RawValue = digitalRead(ReedSwitchPin);
Reads the value from the reed switch.

if (RawValue == 0)
If this value is 0, then the reed switch has detected a magnetic field.

Serial.print(“Reed Switch Detects Magnet … “);
If this value is 1, then the reed switch has not detected a magnetic field.

Serial.print(“No Magnet Detected …”);

Serial.print(“ , RawValue: “);
Serial.println(RawValue);
Print out the status of the magnetic field and the raw value read from the reed switch to the Serial Monitor.

More Arduino tutorial

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

MechatronicsLAB
MechatronicsLAB

Written by MechatronicsLAB

0 Followers

Our main focus will be on Mechatronics Technology where we will work in #Industrial_Automation, #PLC, #IoT, #Raspberry_Pi, #Arduino, #Microcontroller

No responses yet

Write a response