Accelerometer data collection on a MicroBit
-
- Posts: 1
- Joined: Wed Nov 20, 2019 11:32 pm
Accelerometer data collection on a MicroBit
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!
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!
Re: Accelerometer data collection on a MicroBit
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.
So something like this would return 10 seconds of data to a CSV file for 10 seconds after pressing the A button.
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).
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')
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)
- rcolistete
- Posts: 352
- Joined: Thu Dec 31, 2015 3:12 pm
- Location: Brazil
- Contact:
Re: Accelerometer data collection on a MicroBit
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).
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).
Re: Accelerometer data collection on a MicroBit
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.
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.
Re: Accelerometer data collection on a MicroBit
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:
The micro:bit data receiver:
Writing collected data to a file:
lujo
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)
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)
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
Re: Accelerometer data collection on a MicroBit
My guess is that this error is stopping your program.
The error is likely caused by something like
Code: Select all
x, y = something()
Code: Select all
x, y = msg.split(' ')
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
Re: Accelerometer data collection on a MicroBit
Thanks for your replies!!
I'll use the programs and review my program.
I have not realized that sometimes messages can get corrupted...
I'll use the programs and review my program.
I have not realized that sometimes messages can get corrupted...
Re: Accelerometer data collection on a MicroBit
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.
----
---
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()