Accelerometer data collection on a MicroBit

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
Lightning42
Posts: 1
Joined: Wed Nov 20, 2019 11:32 pm

Accelerometer data collection on a MicroBit

Post by Lightning42 » Wed Nov 20, 2019 11:38 pm

Hi there.

This is my first forum post here so bear with me until I get the ropes. I am looking to use the accelerometer on the MicroBit to measure the velocity of a rocket I will be launching at a medium PSI. I have figured out how to display where it is tilting, but I have no clue how the accelerometer measures these things and how it can save data. Essentially, what I'm asking is, is there any way I can collect the data from the accelerometer in a file which I can then copy and use for analysis? If so, how can I do this and what are the units that it measures in?

Thank you for your time!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Accelerometer data collection on a MicroBit

Post by jimmo » Thu Nov 21, 2019 2:57 am

Hi,

The accelerometer returns values on each axis between -1024 and 1024, which correspond to an actual experienced acceleration of -2 to 2 g. i.e. you would expect an micro:bit sitting flat on the table to return approximately 1g (i.e. 512) in one axis, and 0 on the other two axes. More info here: https://microbit-micropython.readthedoc ... meter.html

On the micro:bit, you can access the filesystem using the standard Python APIs.

Code: Select all

f = open('path.txt', 'w')
f.write('hello\n')
So something like this would return 10 seconds of data to a CSV file for 10 seconds after pressing the A button.

Code: Select all

from microbit import *
# Wait for button to be pressed to start recording.
while not button_a.was_pressed():
  pass
with open('data.csv', 'w') as f:
  for i in range(1000):
    f.write('{},{},{},{}\n'.format(running_time(),accelerometer.get_x(), accelerometer.get_y(), accelerometer.get_z())
    sleep(10)
To access the files later, I recommend using Mu (https://codewith.mu/) Note that the filesystme on the micro:bit is unrelated to the USB drive that appears when you plug it into your computer -- that's only for updating the firmware. (This is different to other MicroPython boards such as Pyboard).

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: Accelerometer data collection on a MicroBit

Post by rcolistete » Fri Nov 22, 2019 4:17 am

There is also :
MicroFS - A simple command line tool and module for interacting with the limited file system provided by MicroPython on the BBC micro:bit.

Mu Editor is simpler to use, but in some Linux distributions it is a lot harder to install (PyQt dependencies issues, etc).
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

Syu
Posts: 2
Joined: Thu Dec 05, 2019 12:08 am

Re: Accelerometer data collection on a MicroBit

Post by Syu » Thu Dec 05, 2019 12:58 am

Hi,

I also want to make the similar accelerometer data logger made of micro:bit, so I use two micro:bits, radio techniques, serial communication by MicroPython cording. But, measuring in a few minutes, suddenly the serial communication stops and the logger does not work.
The error message is : ERROR: not enough values to unpack (expected 2, got 1)


I refer to this page. I'm lucky to find this page. If possible, I would like to use the serial data, for example, measured every five seconds in a day.
Finally I want to use that data in csv file.
Is there any way to get such a data log?


I’m no good at using English and I'm a beginner about programs and one-board computer so I need long time to write and read the messages.

Any help will be appreciated.

lujo
Posts: 24
Joined: Sat May 11, 2019 2:30 pm

Re: Accelerometer data collection on a MicroBit

Post by lujo » Thu Dec 05, 2019 9:27 am

Hi,

I'm also using two micro:bits for data collection. One micro:bit generates data, sends the data to a second micro:bit. The second micro:bit sends the data to a computer. Copied from a minimal working setup:

The micro:bit data sender:

Code: Select all

radio.config(group=17, power=5)
radio.on()

while True:
    radio.send(str(accelerometer.get_x()))
    sleep(500)
The micro:bit data receiver:

Code: Select all

radio.config(group=17, power=5)
radio.on()

while radio.receive():
    pass

while True:
    data = radio.receive()
    if data:
        print(data)   # sends data to computer
        display.set_pixel(2,2,9)
        sleep(50)
        display.set_pixel(2,2,0)
Writing collected data to a file:

Code: Select all

#!/usr/bin/python3

import serial
import time

port = "/dev/ttyACM0"
ser = serial.Serial(port=port, baudrate=115200, timeout=1)

while ser.read():
  pass

with open('microbit.dat', 'a') as f:
  while True:
    try:
      data = ser.readline().strip().decode("ascii")
      if data:
        print(int(time.time()), data, file=f, flush=True)
    except:
      pass
lujo

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Accelerometer data collection on a MicroBit

Post by jimmo » Fri Dec 06, 2019 3:30 am

Syu wrote:
Thu Dec 05, 2019 12:58 am
suddenly the serial communication stops and the logger does not work.
The error message is : ERROR: not enough values to unpack (expected 2, got 1)
My guess is that this error is stopping your program.

The error is likely caused by something like

Code: Select all

x, y = something()
Maybe you have something like:

Code: Select all

x, y = msg.split(' ')
But if msg doesn't split into two things, then you'll get that error.

The important thing with the radio is that sometimes messages can get corrupted and your program needs to be able to handle that. So something like

Code: Select all

parts = msg.split(' ')
if len(parts) == 2:
  # valid message
  x, y = parts

Syu
Posts: 2
Joined: Thu Dec 05, 2019 12:08 am

Re: Accelerometer data collection on a MicroBit

Post by Syu » Fri Dec 06, 2019 4:43 am

Thanks for your replies!!

I'll use the programs and review my program.
I have not realized that sometimes messages can get corrupted...

Anders_Fg
Posts: 1
Joined: Wed Apr 13, 2022 7:30 am

Re: Accelerometer data collection on a MicroBit

Post by Anders_Fg » Wed Apr 13, 2022 7:41 am

This is how I use one microbit to collect data.
I make a new file with a random name and can then move the file to a computer.
Press A for start reading and B for stop reading.
----

Code: Select all

from microbit import *
from random import random
tal = str(random())
f = open(tal + '.txt','w')
f.write("  x       y        z \n")
def skriva_fil():
    while True:
        x=str(accelerometer.get_x())
        y=str(accelerometer.get_y())
        z=str(accelerometer.get_z())
        f.write(x +"    "+ y + "     " + z + '\n')
        if button_b.is_pressed():
            f.close()
            break
        sleep(1000)
while True:
    if button_a.is_pressed():
        skriva_fil()
---

Post Reply