Skip to content

Activity

Python data logger

Advanced | Python | Accelerometer, Radio, USB interface | Data handling, Energy, Forces, Information handling, Radio waves, Sensors, Statistics & graphs, Weather & climate

Step 1: Make it

What is it?

Use micro:bit as a wireless data logger recording readings from its sensors.

micro:bit showing X, Y and Z axes across, up and down and front to back next to graph of data collected

How it works

  • Flash the transmitter program onto a micro:bit with battery pack and either attach it to something that moves (like the inside of a salad spinner) or get ready to play catch with it. The program takes constant accelerometer readings of the forces in 3 dimensions (x, y and z axes) and transmits them by radio.
  • Connect the receiver micro:bit to a computer by USB and flash the logger program on to it using the Mu Python editor app.
  • This micro:bit receives the accelerometer data and send as serial data to your computer. Click on the ‘Plotter’ button in Mu and you should see graphs of the live data readings appear on the screen.
  • Place the sensor micro:bit on each side and see how the readings in each axis change. Throw it in the air, spin it in a salad spinner: what do you see?
  • Mu saves the numerical data as a CSV (comma separated values) file in your computer's home folder. Look in 'mu_code' and then the 'data_capture' folder.
  • You can open the CSV file in a spreadsheet program to analyse. If you delete the second and third time columns, leaving only the first, you can plot the data on a scatter graph in your spreadsheet showing how the forces change over time.
spreadsheet showing graph made from micro:bit accelerometer readings

What you need

  • 2 micro:bits and one battery pack
  • laptop or desktop computer to record data on
  • Mu Python editor app: https://codewith.mu/
  • optional salad spinner

Step 2: Code it

Sensor / transmitter

1from microbit import *
2import radio
3radio.config(group=99)
4radio.on()
5
6while True:
7    sleep(20)
8    radio.send(str(accelerometer.get_values()))
9

Receiver / logger

1from microbit import *
2import radio
3radio.config(group=99)
4radio.on()
5
6while True:
7    message = radio.receive()
8    sleep(20)
9    print(message)
10

Step 3: Improve it

  • Record other micro:bit sensor readings remotely this way, such as temperature, light or magnetic compass readings.
  • Conduct a physics experiment into the forces acting on a micro:bit as it spins in a salad spinner (centrifuge). Do you see what you expect? (Bear in mind that the accelerometer on the micro:bit can only read forces up to 2g, twice the force of the Earth’s gravity – if you spin it fast it may experience forces that are too large for it to register.)