Hall Effect Sensor with micro:bit


A hall effect sensor consists of a thin rectangular conductor material, that can be used to detect the presence of magnetic fields. Normally, no current flows through the circuit. But in the presence of a magnetic field, the hall effect sensor turns 'ON' and current can flow through the circuit.

In this guide, we will learn to connect it to the micro:bit using a breakout board and breadboard, and get it to turn on an LED when a magnet is near.

Parts Used in This Guide


Step 1  The Module

First, let's take a closer look at the hall effect sensor module!

There are three pins:

  • S : This is the signal pin which we will connect to a GPIO pin on the micro:bit
  • 3.3V : Though it is unlabelled on the module, this middle pin will need to be connected to 3.3V on the micro:bit
  • GND: This pin is labelled '-' on the module, it is a 'GND' pin. What's '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. 
  • The hall effect sensor, similar to the reed switch, varies its output voltage in response to changes in the magnetic field. 

While both function the same in that way, the hall effect sensor has no moving parts. The second difference is that it activates when a magnetic field is perpendicular to the sensor.

Step 2  Connect GND to -

Step 3  Connect 3.3V to Middle pin

Step 4  Connect P0 to S

Step 5  Connect GND to LED (Negative lead)

Step 6  Add a resistor

Step 7  Connect P1 to resistor

Step 8  Set pull pin P0 to up

Once you've got MakeCode editor up and running, start a new project!

  • In the 'on start' block, we will add a 'set pin P0 to up' block.
  • Click on 'Advanced' > 'Pins' > ... more    
  • Scroll down till you see the 'set pull pin P0 to up' block. 
  • Drag and drop it to the editor.

This 'set pull pin P0 to up' acts as a pull-up resistor.

What is a pull-up resistor? A pull-up resistor is a simple fixed value resistor. Why is it necessary? 

Because digital input pins are very sensitive to change. As a result, its value will be a random, constantly changing value. When P0 is not LOW or HIGH, but fluctuating, it is said to be 'floating' at an unknown value. Now, if you were to skip to step 4,  remove this 'Set pull pin P0 to up' block and upload the code, the LED will not even light up.

A pull-up resistor, or the micro:bit's in-built solution counters this problem. You simply use the 'set pull pin P0 to up' block!

Step 9  Create the variable 'sensorVal'

Now in the 'forever' block, we will first create a variable!

  • Click on the 'Variables' tab. 
  • Click on 'create a new variable' 
  • Name it 'sensorVal' short for sensor value. 
  • Click on 'Variables' tab again, and drag and drop a 'set item to 0' block. 
  • Change its values to 'sensorVal'
  •  Replace '0' with a 'digital read pin P0' block that you can find in the 'Pins' tab.

Step 10  Create the if loop!

We will create the loop! 

  • Click on the 'Logic' tab and drag and drop a 'if true ... then... ' block
  • Click on the 'Logic' tab again and drag and drop a ' 0 = 0 ' block
  • Replace the values with 'sensorVal' and '1'. Because you have previously created the variable, you can find 'sensorVal' block under 'Variables' tab.
  • Click on the 'Pins' tab and drag and drop a 'digital write pin P0 to 0' block
  • Attach it in the 'then' block to complete the condition
  • Replace its values with 'P1' and '1' as shown
  • Click on the white plus (with a circular border) icon twice, to add an 'else if' block
  • Delete the extra 'else' block by clicking on the white minus (with a circular border) icon next to it.
  • Now duplicate the 'sensorVal = 1' block, change its value to 'sensorVal = 0'. 
  • Attach it to the 'else if...' block
  • Grab another digital write pin P0 = 0 block. This time, change its values to 'P1' and '0' as shown.
  •  Attach it to 'then ...' block to complete the condition.

Step 11  Show number!

  • Finally, we will add a 'show number' block. You can find it in the 'Basic' tab
  •  Click on it then drag and drop it into the editor
  • Place it after the loop
  • After uploading this code to the Micro:bit, grab a magnet from your fridge!
  • Place the magnet close to the hall effect sensor!
    • You will notice that the LED will light up as the 5x5 LED matrix shows '0'. 
    • Press the reset button on the back of the micro:bit and the LED will stop lighting up. The value will also be '1'.

Step 12  Upload the code

Finally, upload the code to the micro:bit and test it out.

  1. Connect the micro:bit to your computer using a microUSB cable
  2. Click on the 'Download' button on the bottom left corner in MakeCode editor
  3. Once downloaded, you can find the hex file in your 'Downloads' folder.
  4. Drag and drop it to the MICROBIT drive