The BBC Micro:bit is small, different than other boards. But this board is packed with features. One, in particular, I use in this project is the onboard compass, aka a magnetometer. I wanted to use it as a metal detector!
The earth has a magnetic field that the Micro:bit can detect easily. My concept was to detect disruptions to the Earth’s magnetic field around the Micro:bit.
The magnetometer (compass) measures the magnetic field in 3D coordinates – x, y, z. When the Micro:bit starts up, the program records the magnetic field at the initial coordinate in space. This value becomes the “zero” point. As long as the Micro:bit wasn’t laying on some ferrous metals, it should react to ferrous metals. In other words, detects metals.
There are some nuances, but I’ll get to that in a moment.
First, let’s gather all the necessary components you’ll need to build this project. It isn’t much:
Any sort of Power Supply with a 5V USB output
Video demonstration of the project:
Load the code onto the Micro:bit. I am providing the hex file you need to drag and drop onto the Micro:bit, and the python code as two separate files. For the below process, just use the hex file.
Plug the MicroUSB cable into the Micro:bit, and plug the other end of the cable to a PC or MAC.
At this point, you are going to copy over the code to the Micro:bit. I am providing the program (code) in this posts that needs to be copied over. When the Micro:bit plugs into a computer, it shows up as a USB flash drive. All you have to do is copy the file over to the Micro:bit, like it’s a USB Flash Drive, and the Micro:bit will reset, and the program is active.
You could leave it plugged into your computer, or use that 5V USB power adapter to make the project a bit more mobile.
A little bit about the code:
I commented the code enough to figure out. However, here are some interesting points.
So, we have four images to see here. imgs(0)... imgs(3)
But the base image is a single central dot, defined here:
Here is where new data is sampled. This is the hard working part of the code:
After the magnetometer data is sampled, it is compared to the base sensitivity value and the correlating image is shown.
Here is where that choice is made:
Lay the Micro:bit on a table that is not magnetic, and no ferrous metals around. Lay it there with the LED side face up.
Power the Micro:bit. I recommend some sort of power bank and use a long USB cable. Keep the battery away from the magnetometer, in other words.
This Picture is of the Micro:bit "on" and ready to sense. (RIGHT) On the right, a piece of ferrous metal.
Once the Micro:bit is started, the LED screen should show a small dot or plus sign.
Now, hold the Micro:bit in the same orientation it was in when it started up. You will see the LED screen turn from a dot to a full-screen circle if there is enough metal to detect.
NOTE: If you tilt the Micro:bit, you change the magnetic field that the Micro:bit started out detecting. It’ll give false readings.
Picture - Now the Micro:bit is laying on top of the metal circle. The LED screen is showing a full sized circle - meaning, it has detected the metal object!
When you move the Micro:bit away, it’ll change back to a single dot or small plus.
However, if it doesn’t change between moving over the metal objects, move on to the next steps.
STEP 5 (Optional)
If you are not noticing a huge change in the “circle” image on the screen, then you may have to reset the initial magnetic field detection levels.
This is simple to do. Just move the Micro:bit into an area without any ferrous metals or magnets, and press any of the buttons on the board. They are labeled “a” and “b.”
After you reset it, the circle should be reduced to a dot, and you are now at the “zero” state. Move the Micro:bit over a ferrous metal object.
This is by far the simplest metal detector one can make.
Another use for this project would be for detecting strong magnetic fields. Or changes within one. It would be used in a test lab for detecting field changes.
It can also show if the room you are in is spinning around. But, I think there might be other warning signs for that one.
Microbit metaldetector.zip 214.6 KB