Reed Switch/Reed Sensor

A magnetic contact switch is basically a reed switch encased in a plastic shell so that you can easily apply them in a door, a window or a drawer to detect if the door is open or closed. The switch that we are going to use has two parts: the switch itself, that usually comes opened and the magnet. When you buy this switch, it also comes with 4 screws, so that you can attach it to your door.
TYPES OF REED SWITCH.
There are two fundamental types,
NORMALLY OPEN TYPE.
In a normally open switch, the two reeds, which are made from ferrous material such as a nickel-iron alloy are positioned so that they are not touching. When a magnet is moved close to the switch, it pulls one of the reeds towards the other so that they are touching, and therefore completing the circuit.
Remove the magnet and the reeds return to their original position breaking the circuit
NORMALLY CLOSE TYPE.
A normally closed switch works in the opposite way, when no magnetic field is present the reeds are in full contact, the electric circuit is complete and the device is ‘on’. When a magnet is moved close to a switch, or the switch close to a magnet, the reeds repel one another and split apart, breaking the circuit. ADVANTAGE OF REED SWITCH.
1. No mechanical wear – because there is no physical pressure applied to the switch, there is no mechanical wear and tear.
2. Operated through a non-magnetic material – sensors can be developed with such sensitivity that they can be embedded deep within an assembly out of sight but still actuated by a reasonably strong but discreet magnet.
3. No supply of voltage – as they are triggered by magnetism, there is no voltage required.
4. Compact – reed switches are incredibly compact compared to mechanical switches.
ARDUINO WITH REED SWITCH CIRCUIT:
REED SWITCH WITH ARDUINO CODE:
Copy and paste code or download below given link
// Circuito.io is an automatic generator of schematics and code
// Include Libraries
#include “Arduino.h”
#include “LED.h”
#include “Switchable.h”
#include “Button.h”
// Pin Definitions
#define LEDB_PIN_VIN 5
#define LEDG_PIN_VIN 6
#define REED_PIN_2 2
// Global variables and defines
// object initialization
LED ledB(LEDB_PIN_VIN);
LED ledG(LEDG_PIN_VIN);
Button reed(REED_PIN_2);
// define vars for testing menu
const int timeout = 10000; //define timeout of 10 sec
char menuOption = 0;
long time0;
// Setup the essentials for your circuit to work. It runs first every time your circuit is powered with electricity.
void setup()
{
// Setup Serial which is useful for debugging
// Use the Serial Monitor to view printed messages
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println(“start”);
reed.init();
menuOption = menu();
}
// Main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop.
void loop()
{
if(menuOption == ‘1’)
{
// LED – Basic Blue 5mm – Test Code
// The LED will turn on and fade till it is off
for(int i=255 ; i> 0 ; i -= 5)
{
ledB.dim(i); // 1. Dim Led
delay(15); // 2. waits 5 milliseconds (0.5 sec). Change the value in the brackets (500) for a longer or shorter delay in milliseconds.
}
ledB.off(); // 3. turns off
}
else if(menuOption == ‘2’)
{
// LED – Basic Green 5mm – Test Code
// The LED will turn on and fade till it is off
for(int i=255 ; i> 0 ; i -= 5)
{
ledG.dim(i); // 1. Dim Led
delay(15); // 2. waits 5 milliseconds (0.5 sec). Change the value in the brackets (500) for a longer or shorter delay in
milliseconds.
}
ledG.off(); // 3. turns off
}
else if(menuOption == ‘3’)
{
// Reed Switch – Test Code
//read Reed Switch state.
//if Switch is open function will return LOW (0).
//if it is closed function will return HIGH (1).
bool reedVal = reed.read();
Serial.print(F(“Val: “)); Serial.println(reedVal);
}
if (millis() – time0 > timeout)
{
menuOption = menu();
}
}
// Menu function for selecting the components to be tested
// Follow serial monitor for instrcutions
char menu()
{
Serial.println(F(“\nWhich component would you like to test?”));
Serial.println(F(“(1) LED – Basic Blue 5mm”));
Serial.println(F(“(2) LED – Basic Green 5mm”));
Serial.println(F(“(3) Reed Switch”));
Serial.println(F(“(menu) send anything else or press on board reset button\n”));
while (!Serial.available());
// Read data from serial monitor if received
while (Serial.available())
{
char c = Serial.read();
if (isAlphaNumeric(c))
{
if(c == ‘1’)
Serial.println(F(“Now Testing LED – Basic Blue 5mm”));
else if(c == ‘2’)
Serial.println(F(“Now Testing LED – Basic Green 5mm”));
else if(c == ‘3’)
Serial.println(F(“Now Testing Reed Switch”));
else
{
Serial.println(F(“illegal input!”));
return 0;
}
time0 = millis();
return c;
}
}
}
Download Full Arduino sketch Here.  reed switch code After downloaded extract and double click the Arduino sketch then compile and upload to your Arduino Board and test it.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.