Magic Button Activity

Learn how to detect magnetism around the micro:bit!

  • Get started
  • JavaScript Blocks
  • Inputs
Print lesson
  • Âges 8+
  • 30 min
  • MakeCode Editor

Introduction

This project uses the micro:bit’s compass to detect the presence of a magnet.

Teacher Guide

Open

Activity

6 steps

This is a simple magic trick you can perform to amaze your friends by magically switching the A and B buttons on the micro:bit!

How the trick works

Unfortunately, the only magic here is in the code. This trick uses a magnet, hidden in your hand, to tell the micro:bit to swap over the buttons so that when the magnet is near the microbit the A button starts working like the B button and the B button starts working like the A button.

About the authors

This project was contributed by Brian and Jasmine Norman, aka @MicroMonstersUK. You can checkout their micro:bit tutorials channel on youtube for more projects.

What you need

The only things you need for this trick are your micro:bit and any magnet that is small enough to fit in your hand, even a fridge magnet will work.


Step 1: getting the buttons to display A and B

Before we code the trick itself, we need to get the buttons working as you would expect them to so that pressing button A displays ‘A’ and pressing button B displays ‘B’:


input.onButtonPressed(Button.A, function () {
    basic.showString("A")
})
input.onButtonPressed(Button.B, function () {
    basic.showString("B")
})
          

Step 2: measuring magnetic force

We will use the micro:bit’s compass to detect the magnet. A compass tells us which direction we are facing by detecting the Earth’s magnetic field, but they can also detect any other magnet nearby. We will use that to check if our magnet is next to the micro:bit by using the magnetic force block found in the input menu’s ‘more’ section. As we only want to measure the strength we change the drop down to select ‘strength’.

Step 3: checking if the magnetic is nearby

Now we can measure the magnetic force near the microbit, we can check if the value we measure is so big that it means there must be a strong magnet nearby.

If you have ever played with magnets you know they have two ends, often called a north and south ‘pole’. Depending on which end of the magnet is pointing at the microbit the magnetic force measurement will either be a negative number like -100 or a positive number like 100. We just want to know if the strength is at least 100 we don’t care if its negative or positive so we also use the ‘absolute value of’ block from the maths menu to tell our code to ignore the negative sign and just treat -100 as if its 100.

So in the code below we will check if the absolute value of our magnetic field strength reading is more than 100 and save the result of that check in a new variable called ‘isSwitched’:


let isSwitched = false
let force = 0

force = Math.abs(input.magneticForce(Dimension.Strength))
isSwitched = force > 100
          

Step 4: running our ‘magnet nearby’ check all the time

At the moment our code to detect the magnet being nearby will only run once so we need to put it into a forever block so that it keeps getting run again and again checking for the magnet to come near to the micro:bit. We should also make sure ‘isSwitched’ is false when our program starts.


let isSwitched = false
let force = 0
basic.forever(function () {
    force = Math.abs(input.magneticForce(Dimension.Strength))
    isSwitched = force > 100
})
          

Step 5: swapping the buttons when we know the magnet is nearby

Now we can check the value of our variable ‘isSwitched’ whenever we want and we will know that the magnet is nearby if it’s value is ‘true’. Let’s use that to change how the buttons work and complete the code for our trick. We will add an ‘if, else’ block to each button’s code and check if we should swap over what each button displays because ‘isSwitched’ is equal to true:


let isSwitched = false
let force = 0
input.onButtonPressed(Button.A, function () {
    if (isSwitched) {
        basic.showString("B")
    } else {
        basic.showString("A")
    }
})
input.onButtonPressed(Button.B, function () {
    if (isSwitched) {
        basic.showString("A")
    } else {
        basic.showString("B")
    }
})
          

Step 6: practice your performance

Here is the finished code:


let isSwitched = false
let force = 0
input.onButtonPressed(Button.A, function () {
    if (isSwitched) {
        basic.showString("B")
    } else {
        basic.showString("A")
    }
})
input.onButtonPressed(Button.B, function () {
    if (isSwitched) {
        basic.showString("A")
    } else {
        basic.showString("B")
    }
})
basic.forever(function () {
    force = Math.abs(input.magneticForce(Dimension.Strength))
    isSwitched = force > 100
})
          

Now you just need to program your own micro:bit and practice the trick a few times before performing to friends. Try asking your friends to click the buttons after you have switched the labels and the trick won’t work for them as they don’t have a hidden magnet in their hand.

Challenge

Try to add the following functionality:

  • Add an animation when above a certain magnetic force strength
  • A live magnetism read-out option when “A+B” buttons pressed.
Selecting this opens external content from our support system, which adheres to their privacy policy.