Lesson 18 Reed Switch

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

A reed switch is also a sensor used to detect the magnetic field. Hall sensors are generally used to measure the speed of intelligent vehicles and count assembly lines, while reed switches are often used to detect the existence of a magnetic field.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Reed Switch module

– 1 * 3-Pin anti-reverse cable

– 1 * Magnet

Experimental Principle

A reed switch is a type of line switch component that realizes control by magnetic signals. It induces by a magnet. The “switch” here means dry reed pipe, which is a kind of contact passive electronic switch component with the advantage of simple structure, small size, and convenient control. The shell of a reed switch is commonly a sealed glass pipe in which two iron elastic reed electroplates are equipped and inert gases are filled. Normally, the two reeds made of special materials in the glass tube are separated. However, when a magnetic substance approaches the glass tube, the two reeds in the glass tube are magnetized to attract each other and contact under the function of magnetic field lines. As a result, the two reeds will pull together to connect the circuit connected with the nodes.

After external magnetic force disappears, the two reeds will be separated with each other because they have the same magnetism, so the circuit is also disconnected. Therefore, as a line switch component controlling by magnetic signals, the dry reed pipe can be used as a sensor to count, limit positions and so on. At the same time, it is widely used in a variety of communication devices.  

In this experiment, since an LED has been attached to pin 13, just connect pin SIG of the reed switch to digital pin 7 of SunFounder Uno board. When the reed switch identifies magnetic field signals, the LED will be on. Otherwise it will be off.

The schematic diagram of the module:.

Experimental Procedures

Step 1: Build the circuit

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno board

Now, use a magnet to approach the reed switch, and the LED on the reed switch module and that on the SunFounder board light up. Move the magnet farther and the LED will go out.

 Code

/*****************************************************
* name:Reed Switch
* function:use a magnet to approach the reed switch, and the LED on the reed switch module and that on the SunFounder board light up. Move the magnet farther and the LED will go out.
**************************************************/
// Email: support@sunfounder.com
// website: www.sunfounder.comconst int digitalInPin = 7;// reed switch attach to pin7
const int ledPin = 13;// pin13 built-in ledvoid setup()
{
  pinMode(digitalInPin,INPUT);// set digitalInPin as INPUT
  pinMode(ledPin,OUTPUT); // set ledPin as OUTPUT
}void loop()
{
  boolean stat = digitalRead(digitalInPin);//read the value of pin7 to stat
  if(stat == HIGH)// if it it HIGH
  {
    digitalWrite(ledPin,LOW);// then turn off the led
  }
  else //else
  {
    digitalWrite(ledPin,HIGH); // turn on the led
  }
}