Logging accelerometer data with the micro:bit (Part 2A: Saving to memory)

What would we need to do to have the micro:bit be a useful logger of acceleration data?

  • Set a sampling rate (covered in the first part, here)
  • Store data on the micro:bit’s local storage
  • Play the data back or transfer it to a computer for analysis

In the first part, I took a look at setting/limiting the sampling rate. I did this for a couple reasons:

  1. I made the assumption that having a super high sampling resolution (100+ times per second) is not necessary for the kinds of applications we might want
  2. While we can, indeed, read/sample data quite fast, the micro:bit is quite limited in terms of where we can put the data.

Imagine this like a pipe with water (the water is the data) flowing through it. A huge amount of water could be flowing through a pipe, but to store it, you’d need to block up one end of the pipe. If you do that, how fast would the pipe become full of water and burst? It depends on the size of the pipe, or if you could, say, siphon some of the water out and put it somewhere else.

Memory and storage

(Note: a bit technical, but worthwhile for understanding how this stuff works)

The micro:bit has two places to store data: the random access memory (RAM, or just “memory”), which is where our code is running, and a file system, which is where our code is stored. Your cell phone works much the same way: apps that are running are probably in your phone’s RAM, whereas your photos, videos, and podcasts are stored on your phone’s file system. The storage space is usually larger than your memory, sometimes by a large amount:

  • On your phone, you may have around 4 GB (gigabytes, or billion-bytes) of memory and 64 GB of storage
  • On the micro:bit, before anything is running, you may have about 16 KB (kilobytes, or thousand-bytes) of memory, and 32 KB of storage.

Let the difference between the space available in memory versus storage–and between your phone and the micro:bit–sink in for a moment. Best case, you have about half as much space available in your memory compared to your storage. Also, your phone may have 250 000x more memory and 2 000 000x more storage than the micro:bit.

It turns out that when you’re running Micropython (as we are), we have a little bit under 9.5 KB free (this could vary slightly). You can check this yourself, with the following code:

from microbit import *

import gc # Gives us access to the "Garbage Collector"

print("Size of free memory: {} bytes".format(gc.mem_free()))

The garbage collector is responsible for helping us manage memory by cleaning up things that are no longer used. We can get an idea of available memory by asking the garbage collector how much is available with gc.mem_free()

Storing data in memory

When you run out of memory, you get what’s called a MemoryError. This happens when you try to store too many things in the micro:bit’s memory and it can’t find anywhere to put them. For example, say we wanted to each set of values from the accelerometer in a list. We could accomplish this by creating a new list variable and adding an append() statement to our loop, like so (note: you WILL get a MemoryError from this):

from microbit import *

data = []

while True:


Instead, we need to limit the number of data points that get recorded. From some testing, 230 appears to be a “safe” number, as in, it should not result in a MemoryError. You can try it yourself with the code below, which does two things:

  1. On reset, records 230 data points
  2. Prints them out constantly as part of the loop
from microbit import *

data = []

frequency = 50 # Sample 50 times per second

display.show(Image.NO, wait=False) # Show an X before recording

for i in range(0,230): # Record 230 data points


display.show(Image.YES, wait=False) # Show a checkmark after recording

while True:

    for d in data:

        sleep(1000/50) # Fixed printout rate

You can see below an example of what it looks like if you try to only wiggle the micro:bit in the x direction:

Your data has been saved to memory! This is only temporary, though: when you reset your micro:bit, your data gets lost–but this is the first step in eventually finding a more permanent home (in the micro:bit’s file system) for your data.

This has already run quite long, so that’ll have to wait until next time!

One Reply to “Logging accelerometer data with the micro:bit (Part 2A: Saving to memory)”

Leave a Reply

Your email address will not be published. Required fields are marked *