Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

For more information on how Google's third-party cookies operate and handle your data, see: Google Privacy Policy

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

No cookies to display.

Les PounderMagnetic Alarm

About the Author

Les is a maker and trainer who has worked with the Raspberry Pi Foundation and the BBC to deliver computing training.

@biglesp bigl.es

Magnets are wonderful things to experiment with and in this project, we shall use a magnet to alter the built-in compass of the micro:bit. Changing where it believes North will be and using it to create a simple alarm.

Parents: Please note that children should only use fridge magnets and not rare earth/neodymium magnets as they can be harmful if swallowed.

For this project, we shall be using the Javascript Blocks Editor https://microbit.org/code/ and we should already be comfortable copying code to our micro:bit.

Step 1 - Startup Screen

Step 1 - Magnetic Alarm Startup Screen

When our micro:bit starts up we need to test that our compass is working, and so we use a repeat 4 times loop found in the Loops section.

Step 2 - Check your heading

We can find the compass heading block in Inputs. Drag the block over. But how can we use it? We need to use a block to scroll it across the micro:bit.

Step 3 - Show The Heading

Step 3 - Showing the Heading Code

Inside of the repeat loop we use the Show Number block from Basic to scroll the compass heading across the micro:bit. This tests that our compass is working.

Step 4 - A Little Logic Goes A Long Way

Step 4 - Adding Logic Code

From Logic, we need to grab the ”if true then..else” block and place it inside the forever block. This will check to see if a certain condition has been met.

Step 5 - Checking values

Step 5 - Checking the Values Code

From Logic we use the “0 < 0” and place it over “true”, then we use another “compass heading” block in the first 0 and change the other to 200.

Step 6 - So what happens if...

Step 6 - Adding an if condition to the code

Compass headings can be between 0 and 360, the value 200 found using the startup script and noting the normal value. But if the value is below 200, then we show the surprised icon from Basic.

Step 7 - All Is Normal

Step 7 - All is Normal Code Block

But if the alarm has not been triggered, then the magnet is still in place and so we use the else condition and another “show icon” block to show a happy face.

Step 8 - Final Code

Step 8 - Completed Code

Here is the final code for the project, does it match yours?

Check for any bugs before downloading the code to your micro:bit.

Step 9 - Magnetic Alarm in Action

Magnetic Alarm in Action

The compass is located in the top right of the micro:bit, and when we use the magnet it alters where the micro:bit thinks magnetic north is so we can control the direction.

That’s it, now download the code to your micro:bit and when it has finished, move the magnet towards and away from the micro:bit and watch the screen change.

Well done you have made your own magnetic alarm.

You can attach this to a door and add a speaker to make loud noises to scare away parents/pets/siblings. You can learn how to connect a speaker by reading Issue 2 where we make a countdown timer.