Skip to content

Measuring the length of a pendulum with a microbit

June 12, 2019

Here’s a write up of another activity I’ve been working on as part of my sabbatical.

In college, I remember a dean telling us the joke about the student who was challenged by their professor to measure the height of a building using a barometer, and all the ingenious ways in which the student came up with complete the task: tying a piece of string to the barometer and lowering it from the side of the building to measure the heigh, dropping the barometer off the top of the building and measuring the time it takes to hit the ground, offering the barometer as a gift to the building superintendent in exchange for the height of the building, and more.

Inspired in part by this joke, I decided to see if we might be able to use a BBC micro:bit to measure the length of a piece of string. First, I want to give a serious shout out to the micro:bit as a pretty amazing tool for exploring computing. They are small (2in x 1in) integrated circuit computers that include an array of sensors (light, temperature, accelerometer, compass), a 5×5 LED display, 2 physical buttons, 25 pin outs, and bluetooth wireless connectivity. They’re also cheap ($15) and very easy to program in a block based javascript, or text based python. The platform has a ton of accessories and curricular materials, and seems to be in pretty wide use across the UK and Norway, and probably many other places as well. Microbit is the easiest microcontroller platform I’ve ever worked with, and I think they would be an ideal platform for any middle of high school STEM class looking to add some embedded systems programming to their work.

When I first started playing with micro:bit, it took me about 10 minutes to follow these instructions about Live data logging with Python and Mu (a great beginning python editor for programming micro:bit), and suddenly I had an accelerometer that could wirelessly transmit its readings to a second micro:bit serving as a receiver connected to my computer. This got me thinking, could I use the micro:bit as a pendulum?

Here’s a description of what I did, with some ideas for how to turn this into an actual activity for students in physics class.

Measuring the length of a piece of string with a microbit

The microbit is just about the perfect size to serve as pendulum bob, and it’s easy to attach a battery pack and string through one of the pre-drilled connection holes.

The first thing I needed to do was modify the program from the live data logging example to calculate the magnitude of the acceleration, rather than logging the 3 components for the x, y, and z axes. Thanks to the great documentation of the platform, and the built in tooltips of the mu editor, I was pretty easily able to come up with this program for the accelerometer:

from microbit import *
import radio
import math

while True:
a_x = accelerometer.get_x()
a_y = accelerometer.get_y()
a_z = accelerometer.get_z()
a_mag = math.sqrt(a_x2+a_y2+a_z**2)

and the receiver program was unchanged:

from microbit import *
import radio

while True:
message = radio.receive()

The beauty of the mu-editor is that it will automatically graph any data that is displayed using the print function with its built in plotter. (To take advantage of this feature, the data you print must be sent as a tuple, a single data value followed by a comma, and the radio.send function requires the transmission of a string, so this is the reason for the slightly strange notation of the radio.send argument in the transmitter).


Unfortunately, the mu-plotter is very limited in terms of what it can do—you can’t rescale the axis or do anything to manipulate the data. But mu-python also automatically saves each data run as a .csv file inside the “data_capture” folder within “mu_code”, which you can import into a spreadsheet. Once you’ve imported the data into the spreadheet, you can add a column of time measurements (this program takes readings every 20 milliseconds), and then graph and fit that data.

Since Geogebra is heavily used in Norwegian high school, I decided to try my hand at it for fitting sinusoid data. After pasting the data into the spreadsheet, you need to create a list of points, using the toolbar as follows:


You can then use the FitSin function to fit the list of points:

Knowing that the angular acceleration of the pendulum can be written in general as

\theta(t)=A\sin\left(\omega t -\phi\right)+C

The angular frequency for this pendulum must be 11.32 rad/s.

For a simple pendulum,

\omega^2 = \frac{g}{L}

which we can rearrange and solve for L:

L = \frac{g}{\omega^2}=\frac{\unit[9.8]{N/kg}}{\left(\unit[11.32]{rad/s}\right)^2}=

Hmm—something here isn’t right. It’s calculating a pendulum that is 0.07 m long.

Update: I realized the problem seems to be in the code—the sleep(20) statements in the both the pendululum and the receiver seem to combine to making the actual time between data points 0.04 seconds, rather than 0.02 seconds. Taking this into account would make the angular frequency of the pendulum 5.66 rad/s, giving a much more reasonable value for the lenght of the pendulum as 0.30m.

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: