Arduino and reed switch

A reed switch is a small device that when the device is exposed to a magnetic field, the two materials inside the switch pull together and the switch closes. When the magnetic field is removed, the two materials then separate and the switch will open.

Here is a picture of the reed switch breakout we have, it was part of a sensor kit

reed switch

Pin Label Arduino Connection
1 AO Analog input
2 G Ground (GND)
3 + 5 volt power
4 DO Digital input

 

We will basically connect this up, read the analog and digital signals and monitor using the serial monitor, we will then postion the reed switch near a magnetic source and see if the readings change.

 

Code

Same as a previous example

// Arduino pin numbers
//D2 and A0 used
const int digital = 2;
const int analog = 0;

void setup()
{
pinMode(digital, INPUT);
Serial.begin(115200);
}

void loop()
{
Serial.print(digitalRead(digital));
Serial.print(“-“);
Serial.println(analogRead(analog));
delay(250);
}

 

Result

Here are the results you will see

reed switch output

As you can see  there are two basic sets of values

1/ The reed switch open/ no magnet – 0 and 1023
2/ The reed switch closed/ magnet present – 1 and 0

Related posts

Examples on how to optimize Arduino memory usage

MH-ET LIVE Tiny88 : an Arduino Compatible development board

serial communication on the arduino nano iot