Using the micro:bit to detect electrical current
Last updated: Nov 5, 2022
This blog is an ongoing record of using the micro:bit to detect electrical current.
Idea
The micro:bit has a magnetometer on the board. When a current flows through a wire, a magnetic field is produced. We can use the magnetometer to detect this.
This is part of an energy conservation project for classroom use.
Summary
The magnetometer on the micro:bit is used to detect the change in magnetic field produced in the mains cable to a hair dryer when it is turned on and off.
(max-min) magnetometer data for 1000ms windows, sampling at 15ms, hair dryer turned on and off
Both methods gave positive indications of when a hairdryer drawing 3A is turned on and off.
Theory
The magenetic field strength around a wire varies with the current through the wire and the distance from the wire as:
B = μ0 I/(2π r)
Where B is in Tesla, A is in Amps and r is in metres. There are, as always with Physics, quite a few assumptions with deriving this equation.
For our purposes, it is ‘good enough’.
μ0 is the ‘permeability of free air’ and is 1.2566 x 10-6 m kg s-2 A-2.
If we take 2mm as the distance from the middle of the power cable to the sensor, r=2x10-3m.
This gives:
B = 9.9997 x 10-5 A ≈ 10-4 A
So, with 1A of current, we get 10-4 T of magnetic field strength at 2mm from the centre of the wire core.
So, 0.1mT per Ampere of current. An Ampere is quite a lot of current in today’s world.
Turning a device on and off can cause an inductive spike, so the momentary current can be higher than the steady state current through the load.
Calcs
The micro:bit v1 has separate 3-axis accelerometer and magnetometer sensors. The 3-axis magnetometer is the MAG3110.
The micro:bit v2 has an integrated accelerometer and magnetometer, each 3-axis, the LSM303AGR.
I will assume micro:bit v1 is in use. The data sheet does not go into details on how the magnetometer works. It could well use the Hall effect.
The sensitivity of the MAG3110 is stated as 0.10μT with a range of ± 1000μT.
The maximum sample rate of the sensor is 80Hz. This becomes important when we start using it to measure AC devices (mains powered), as the frequency of mains current is 50Hz where I live.
One other piece of information: ‘Noise down to 0.25 μT rms’. I think that this means that if we put the magnetometer in a shielded box, so that the Earth’s magnetic field is absent, then the minimum magnetic field that the sensor could reliably measure is 0.25μT rms.
The Earth’s magnetic field intensity on the surface varies from 25 to 65μT.
Working with the results from the Theory section:
0.1 mT per Ampere is the same as 100μT per Ampere. So far, so good. We should be able to detect this. With one Amp of current. Which is a lot of current. How about 100mA? This gives 10μT of magnetic field strength in addition to the background magnetic field strength.
We should be able to detect this.
How about 10 mA? Should still be good.
How hard could it be? What could go wrong? The answer to this is that we need to consider:
Alternating Current
In the Real World, we are probably trying to monitor energy consumption from devices that run from alternating current (AC), not direct current (DC). There will be an alternating magnetic field from the wires that transfer the AC to and from our devices. But… there are two wires current carrying wires connected to the device, one for current going in to the device (the live wire), one for current out (the neutral wire). There may also be a third wire, the earth wire, which does not carry current unless a fault occurs in the device. These wires are twisted together in the power cable.
The currents flow in opposite directions in the live and neutral wires. The magnetic fields that are produced will be equal and opposite, so will cancel out to some degree.
Geomagnetic field
The reason that the micro:bit has a magnetometer is to use it as a compass. The magnetometer measures the Earth’s magnetic field. This field is pretty stable for a given location, but does vary geographically. You can find out what the field is at your location at this website. Where I live, the magnetic field strength is quoted as 49588.2nT, which is 49.5882μT. So we should always measure some magnetic field on the magnetometer, unless you somehow shield it from the geomagnetic field.
AC Testing
The AC current to a device drawing current varies sinusoidally with a 50Hz cycle rate (where I live). I am looking for variation in the magnetic field as the current builds and decays during each cycle. There should be a difference in the max-min magnetic field. Ideally, when the device is off and no current is drawn, there is no variation between the max and min magnetic field strength.
There are, of course, several Real World limitations.
Real World Limitations
Noisy data
In the Real World, the magnetometer sensor data is noisy. There is a variation in the magnetometer readings from one sample to another even when the device it is monitoring is switched off. This is a combination of internal noise in the electronics and external noise from the environment. You could look at this experiment as adding another source of external noise. In our case, the noise is the data we want to measure. One person’s noise is another person’s data.
Sensor sample rate limit
The magnetometer has a maximum sampling rate of 80Hz according to the data sheet. I don’t know what the max sample rate is when using the programming platform provided for the micro:bit. It clearly can’t be more than 80Hz though, as the sensor cannot transmit at a higher rate. So I need to get data over a number of 50Hz mains cycles to have any chance of picking up the max and min magnetic field strength. Ideally, we would sample at least 10 times the frequency of the data set that we are trying to characterise. Instead, we sample over a number of cycles and hope to catch values that approximate the max and min for each cycle.
micro:bit v1 limitations
During testing, I regularly ran out of memory on the micro:bit v1. I am using Micropython for development so that the code can be easily re-used and improved by the target end users of this project. It may well be that the Micropython implementation is creating the memory limitation. I may move to C if Micropython is too limiting.
Ideally, we choose the hardware to fit the project. In this case, I am mandated to use the micro:bit. Which is fair enough. The end product is to be lesson plans that use the micro:bit to teach about energy conservation.
AC Testing Setup
The maximum 80Hz sample rate for the magnetometer equates to a maximum sample interval of 12.5ms. I set the sample interval to 15ms. Nobody likes being pushed to their theoretical limit.
The results presented below are for a hair dryer, which has a current of about 3A when on, according to my ‘Plug-in Power & Energy Monitor’.
Please find the elegant and sophisticated experimental mounting arrangement presented below. The power cable is fixed over the top of the magnetometer, which is labelled as ‘COMPASS’ on the micro:bit.
micro:bit attached to the power cord of a hair dryer
The power to the hair dryer is turned on and off using the power switch on a socket switch. Please find a photograph of this below, which also shows the ‘Plug-in Power & Energy Monitor’ used to measure the current. I use the switch on the socket strip to turn the power on and off to the hair dryer to avoid disturbing the power cable position. Moving the power cable causes noise on the magnetometer data.

Plug-in Power & Energy Monitor used to measure the current going to the hair dryer
Results
Please find a graph from the Plotter tool in Mu below. This graph automatically scales. I added the labels. This shows the max-min (delta) values for each of the x, y, z axis. The time window is 1000ms and the sensor sample rate is 15ms. So there are about 66 samples in each time window.
(max-min) magnetometer data for 1000ms windows, sampling at 15ms, hair dryer turned on and off
Why do we see this marked effect on only two axis? My hypothesis is that as the magnetic field is circular around the wire, the magnetometer axis that is parallel to the wire will not see as much variation as the other two that are at right angles to the wire.
Direct current (DC) Testing
With DC, the magnetic field strength should not vary as the current does not fluctuate. So I intend to measure the absolute magnetic field strength and look for an increase when there is a DC current flowing through the wire to the device under test (DUT).
Setup
I used a USB powerbank connected to a mobile phone. I am not sure how much current it draws as my fancy USB power monitor is at the University and I am working from home due to the ongoing pandemic. I’ll add this data when I have it. I attached the micro:bit to the USB cable in much the same manner as with the AC power cord. Sophisticated, advanced rubber band technology. I may have to patent this tech. The current along the cable will vary as the mobile phone battery charges up. This variation changes over a time scale of tens of seconds, compared with the 20ms cycle rate from AC 50Hz mains.
Results
I can measure a 20% increase in the average total magnetic field strength when the phone is connected, compared to when the phone is disconnected.
Historical method - compass deflection
After completing the above experiments, I read that in the early 1800s, current was measured by looking at the amount that a magnetic compass was deflected near to the wire carrying the current. The article about this is here.
The micro:bit uses the magnetometer to get compass readings. So I tested seeing if there is a deflection to the compass reading of the micro:bit when the hairdryer is turned on or off, using the same setup as in the previous experiment, with the hairdryer drawing 3A.
I used the makecode editor to quickly lash up a program to poll the compass. I trid to poll the compass at 4Hz, but I think it maxes out at about 1Hz. The reading is plotted using the Mu editor. Please see the results below. The higher, stable line, is the compass bearing when the hairdryer is off, the lower noisy readings are when it is on.
Clearly, the magnetic field from the wire deflects the compass reading. I now have a digital version of the galvanoscope, a mere 200 years after the invention of the original.
Compass reading from a micro:bit attached to a hairdryer cable as the hairdryer is turned on and off
Where’s the code
On GitHub at: https://github.com/mattoppenheim/microbit_magentometer
Anticipated ‘why didn’t you…’
I tried to stick to tools that are easily available for classroom use, such as Micropython and the Mu editor.
I would like to put a space between numbers and units, as is recommended by journals. I couldn’t figure out how to stop the units splitting from the numbers across lines in my markdown editor. I’ve looked for ‘markdown non breaking spaces’. So far none of the suggested fixes work for me. Life is short.