Use a Reed Switch with micro:bit

Difficulty
Moderate
Steps
19

A reed switch is an electrical switch which is operated when a magnetic field is brought near to it. 

In this guide, you will learn how to connect it to the micro:bit and turn an LED as well as an alarm on and off. We will then test it with a magnet.

Complete this guide and use it to create a security alarm system. Most alarm systems use a magnetic reed switch on doors and windows. They may come in different forms and styles, but they operate using the same principle!

Parts Used in This Guide

Buy

Step 1  The Module

The reed switch module has three pins:

  • Digital Output (DO)
  • GND: In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called  ground or GND.
    • Note: Voltage is the difference in potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to. 
  • 3.3V  :  'VCC' stands for Voltage Common Collector. We'll connect the VCC pin to 3.3V on the micro:bit

Step 2  Connect module to breadboard

Step 3  Connect P1 to DO

Step 4  Connect GND to GND

Step 5  Connect + to VCC

Step 6  Connect GND to LED (Negative lead)

Step 7  Add a resistor

Step 8  Connect P2 to resistor

Step 9  Connect + to VCC (Buzzer module)

Step 10  Connect P0 to I/O

Step 11  Connect GND to GND (Buzzer module)

Step 12  Connect 3.3V to +

Step 13  Show number!

Let's get started with programming the micro:bit using MakeCode editor

Start a new project and follow these steps:

  • From variables, grab a 'set item to 0' block.
  • Create a variable called 'signal'
  • Change the 'set item to 0' block to 'set signal' block and attach it to a 'digital read pin P1'. This is because we connected the digital output (DO) pin of the reed sensor to Pin 1 of the micro:bit.
  • Drag and drop an 'if then else' logic statement onto the editor, and give it a condition of 'if signal = 0'. 
  • Add a 'play tone Middle C for 1 beat' block and an 'show angry face icon' block
  • Under 'else', add a 'show heart icon' block
  • As you can see in the code, when the signal is '0' or LOW, the alarm sounds through a buzzer module.
  • To see this for yourself, add 'show number' blocks and replace its value with the variable, 'signal'. 
    • Why does this happen? There are two types of magnetic switches. The N.O. (normally open) and the N.C. (normally closed). 
    • In this guide, we used a normally closed reed switch where current passes when the magnet is not there.
    •  In the presence of a magnet, the switch opens and so the signal is '0'.

Step 14  Make the LED blink!

  • Now let's make the LED blink by adding some 'digital write pin P2 blocks'
    • We set its value to '1' to turn the LED on
    • Set its value to '0' to turn it off
  • To make it blink, we add 'pause (ms) 500' blocks

Step 15  Upload the hex file

  1. Connect the micro:bit to your computer using a microUSB cable
  2. Click on the 'Download' button in MakeCode editor
  3. Find the downloaded hex file in your Downloads folder
  4.  Open up Finder on the MacOS or Explorer on Windows, and drag the hex file into MICROBIT under 'Devices' on the macOS.
  5. Let the micro:bit blink as the program uploads!

Step 16  Start the Arduino IDE

Let's take a look at how we could program the micro:bit and reed switch with the Arduino IDE. If you do not have the Arduino IDE already installed, you may refer to our previous guide on how to set up the Arduino IDE for the micro:bit


Step 17  The code


              int reedSensor = 1; // the pin attached to reed switch digital output
int LED = 2; // the pin number of the LED positive leg

// variables will change:
boolean sigState = 0; // variable for reading the reed switch status
void setup()
{
  // initialize the LED pin as an output:
  pinMode(LED, OUTPUT); 
  // initialize the reed switch pin as an input:
  pinMode(reedSensor, INPUT); 
}
void loop()
{
  // read the state of the reed switch value:
  sigState = digitalRead(reedSensor);
  if (sigState == HIGH)
  { 
    // turn LED off: 
    digitalWrite(LED, LOW); 
  } 
  else
  {
    // turn LED on:
    digitalWrite(LED, HIGH); 
  }
}
            


  • We create two variables, 'reedSensor' and 'LED' which are assigned the numbers '1' and '2' respectively. This is because:
    •  the reed sensor's I/O pin is connected to Pin 
    •  the LED's positive leg is connected to Pin 2 of the micro:bit.
  • Then we assign a boolean variable, sigState with a value of '0'.
  • In void setup(), we need to state which pins are OUTPUT or INPUT.
  • Now in void loop() which goes on forever, we read the state of the reed switch value using:

    sigState = digitalRead(reedSensor);

  • If sigState is HIGH or '1', we turn the LED off.
  • Else if sigstate is LOW or '0', we turn the LED on.

Step 18  Security alarm


              int reedSensor = 1; // the pin attached to reed switch digital output
int LED = 2; // the pin number of the LED positive leg
int buzzer = 0; //the pin number of buzzer intput/output pin

// variables will change:
boolean sigState = 0; // variable for reading the reed switch status

void setup()
{
  // initialize the LED pin as an output:
  pinMode(LED, OUTPUT); 
  // initialize the reed switch pin as an input:
  pinMode(reedSensor, INPUT); 
  // initialize the buzzer pin as an output:
  pinMode(buzzer, OUTPUT);
}

void loop()
{
  // read the state of the reed switch value:
  sigState = digitalRead(reedSensor);
  if (sigState == LOW)
  { 
    // turn LED off: 
    digitalWrite(LED, LOW);
  } 
  else
  {
    // turn LED on:
    digitalWrite(LED, HIGH);
    beep(200);
    delay(1000);
    beep(200);
    delay(1000); 
  }
}

void beep(unsigned char){
  analogWrite(buzzer, 50);      // Any value can be used except 0 and 255
  delay(500);          // wait for half a second
  analogWrite(buzzer, 0);       // 0 turns it off
  delay(500);          // wait for half a second
}  
            

Now, let's try to prototype a security alarm system!

Most alarm systems use a magnetic reed switch. You could have a reed switch on the door frame and a magnet on the door. When the door is opened, it would separate the magnet and the reed switch, triggering the alarm.

From the code before, when the magnet is close to the reed switch, the LED lights up. Instead, now we want the LED to turn on when the magnet is not near the reed switch. 

  1. Simply change :   if (sigState == HIGH) to   if (sigState == LOW)
  2. We also added a new variable called 'buzzer' and assigned it a value of 0 because the buzzer module is connected to Pin 0 of the micro:bit. 
  3. Remember, it needs to be initialised as an OUTPUT. 
  4. A function 'beep' has been created to sound the alarm. You might be wondering what 'unsigned char' means here.

    If you are using character types as numbers, use:
    • signed char, which gives you at least the -127 to 127 range. 
    • unsigned char, which gives you at least the 0 to 255 range.

  5. Finally, we call it in void loop() using beep(200).

If the magnet is separated from the reed switch, the alarm will sound until the micro:bit is turned off or the security system is reset!

Step 19  Upload the sketch!

  1. In the Arduino IDE's top left hand corner, click on the 'Verify' button
  2. After it has been verified, click on the 'Upload' button next to it
  3. Once finished uploading, it should state 'Done uploading'.

The program should be running now, which means the alarm should be sounding and the LED flashing!