Let's get started with the PiicoDev® Magnetometer. In this guide, we'll connect the Magnetometer to our micro:bit, get it working as a compass, and read the magnetic field strength of a nearby magnet.

Transcript

I'm going to show you how to get started using a PiicoDev magnetometer and a Micro:Bit. We'll connect these two together and get some fun examples working. Now the Micro:Bit already has a magnetometer on board, but this article will be useful if you want to separate where you measure your magnetic fields from your Micro:Bit, like if this needs to move around. To follow along, you'll of course need a PiicoDev magnetometer and adapter for Micro:Bit, a Micro:Bit V2 and a PiicoDev cable. I'm using a 500mm PiicoDev cable to keep my magnetometer nice and far away from as many electronics as possible. You might also want to have a magnetic compass nearby if you want to compare readings, and some permanent magnets can be handy as well.

Start by plugging your Micro:Bit into the adapter so that the buttons are facing up. Connect your PiicoDev cable and connect the other end to your magnetometer, and connect to your computer with a USB cable. In the article for this tutorial, find the Downloads section, and there are three files that we need to download. Right click each link and Save Link As. I'll save these to a PiicoDev directory in My Documents. This last file, compass.py, I'll right click that and save that as main.py.

We'll use Thonny to program our Micro:Bit in this tutorial. If you haven't used Thonny before, check out our guide for that, or if you prefer to use a web-based programmer, check out our guide for programming the Micro:Bit using python.MicroBit.org. Open Thonny, connect to your Micro:Bit, and navigate to where you saved those three files. We'll upload all of them to the Micro:Bit. Click the first, hold down shift, and click the last, then right click, upload to Micro:Bit. We'll open up main.py on the Micro:Bit. This is a compass example. The magnetometer will sample magnetic field strength in three directions and do a little math to calculate the direction of the magnetic field.

We'll start with a bit of math to collapse the heading into a similar format to what you get out of a compass. The heading will be in degrees on the compass scale, ranging from 0 to 360-degrees on the Earth's surface.

To begin, we have a couple of imports to import the functionality for the magnetometer and a sleep function. We then initialize our magnetometer and call it compass. We also set the range to 800, with ranges between 200 and 3000 microtesla being the unit for magnetic field strength. After that initialization, we calibrate the magnetometer. In the infinite loop, we read the heading and print it to the shell. After running the script, we get a notice that we'll begin calibrating. We rotate the compass slowly and smoothly in a complete circle, with a progress bar that fills up and resets every time an event occurs for calibration. Once calibration is complete, we can move the magnetometer around and see the heading change in the shell and graph. We then bring out a magnetic compass to check the reading and capture the direction of north in the studio. When we bring the magnetometer back in, we find that it agrees exactly with the compass.

I'm getting north about zero degrees or around there. If I face south, I get 180-degrees. If I faced west, pointing this way, I should get about 270-degrees. You love to see it. Now if I stop the script and refresh this file pane using the refresh tool, I might see a new file has appeared. This is the calibration data that we captured during compass.calibrate. By saving this data to a file, we only need to run this calibration function once. From now on, we can rerun this script with compass.calibrate commented out and that way we don't have to go through that calibration procedure. Even though we don't need to calibrate the magnetometer every time, we may still have to if it's been near some strong magnets or has otherwise become magnetized.

Let's move on to the next example. And example two, detect a magnet and copy all of that code into Thonny. I'll just paste that straight into main. This script starts in a very similar way to the last one. We just start with the maximum range, 3000 microteslas, because we're going to be working with a magnet. There's a threshold programmed and in the infinite loop, we just call the read magnitude function to read the magnetic field strength. Then we check that if strength is greater than the threshold, we'll print a message strong magnet. When I run the script, we can see the magnetic field strength at about 80 microteslas. And as I move my magnet closer, that number ought to get higher and higher. And once we cross that threshold, we get the message strong magnet. If you're going to work with magnets and the sensor, do your best not to actually touch them. We don't want to magnetize components on the board itself, but it's fine to bring a magnet nearby and you can see that we can detect that really easily. As a remix, if I comment out this message and we change the delay to 100 milliseconds, we'll be able to see a much more quickly reacting plot. I'll rerun the script. And now you can see we can detect very small movements of this magnet by using this function. So you can detect like proximity using this function. I'm only moving it about a centimetre or two.

If you want to do your own signal processing of the raw data that comes out of this sensor, then example three is for you. Copy all of example three and paste that into main. This example calls the read function and that just reads the three axis measurements in micro Tesla. So when we run the scripts now, we actually have three plots here, one for each of the axes that are labelled on the sensor. And so if I rotate this magnet 90-degrees on the table, we can see that really affects X and Y, which are the blue and red traces. But the Z trace is practically unaffected and that's because the Z axis here is coming through the board and the magnetic lines on this magnet are moving in this axis only. There's no magnetic flux going out in this direction. If I were to stand the magnet up on its end, that should really affect the Z axis because now we have magnetic flux lines coming out of the poles of the magnet and going through the Z axis.

All these numbers being printed for the axes are in micro Tesla, but if you want the register level values coming out of the sensor, then you can use the read function with just the raw equals true argument. Now the values being printed are the actual register values from the sensor along with any calibration that we collected. So there you have a few fun starter projects for the board. PiicoDev has a great magnetometer and Micro:bit starter project. If you have something cool to show us or have any questions, please let us know in the comments for this article. We are full-time makers and would be more than happy to help. Until next time, catch you later!

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.