bbc micro:bit some data-logging and control examples · bbc micro:bit – some data-logging and...

10
BBC Micro:bit – some data-logging and control examples. Adrian Oldknow [email protected] 12 th January 2018 A very basic experiment for a simple pendulum is show on the right. I have suspended a BBC micro:bit from a retort stand using its own USB serial cable. The main part of the program, written in the Microsoft MakeCode block editor, is below. It sends sensed data to the PC through the USB cable, using the `Serial’ blocks in the Advanced menu. It uses 3 functions. `Setup’ initialises the variables `samples’ and `gap’ which control the duration and frequency of the sampling. The variable `pressed’ is used to check when button A is pressed. The function `WhenA’ waits until button A is pressed before starting to log the data. `SENDserial’ writes data to the PC’s USB serial port. When all the data have been sent, the micro:bit displays `F’. The free Tera Term VT software is used to receive the data from the micro:bit. It detects which Com port is being used, and you need to use Setup to change the baud rate to 115200, the speed with which the micro:bit sends data. The `SENDserialfunction writes data as strings and puts a “,” between them.

Upload: duongphuc

Post on 03-Apr-2018

231 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

BBC Micro:bit – some data-logging and control examples.

Adrian Oldknow [email protected] 12th January 2018

A very basic experiment for a simple

pendulum is show on the right. I have

suspended a BBC micro:bit from a retort

stand using its own USB serial cable. The

main part of the program, written in the

Microsoft MakeCode block editor, is below.

It sends sensed data to the PC through the

USB cable, using the `Serial’ blocks in the

Advanced menu. It uses 3 functions.

`Setup’ initialises the variables `samples’ and `gap’ which

control the duration and frequency of the sampling. The

variable `pressed’ is used to check when button A is pressed.

The function `WhenA’ waits until button A is pressed before starting to log the data.

`SENDserial’ writes data to the PC’s USB serial port. When all the data have been

sent, the micro:bit displays `F’.

The free Tera Term VT software is used to receive the data from the micro:bit. It

detects which Com port is being used, and you need to use Setup to change the baud

rate to 115200, the speed with which the micro:bit sends data. The `SENDserial’

function writes data as strings and puts a “,” between them.

Page 2: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

You can copy and paste the data into PC software, such as Excel. But not with great success!

Using the `Spreadsheet’ view in the free

GeoGebra software does the trick. Now

you can do some analysis in GeoGebra,

or copy and paste it back into Excel. I

have created columns C and D by

dividing the data in each of columns A

and B by 1000.

In order to construct

the scattergram of D

against C, I have first

selected the D column

and then, with the Shift

key held down, the C

column. One of the

options in the

Spreadsheet view is to

perform a `Two

Variable Regression

Analysis’. Selecting this

creates a scattergram

and offers you a choice

of `Regression Model’. The screen shot

shows an attempt to fit a sine function.

Copying and pasting columns C and D into

Excel allows you create a scattergram there

instead. You can also fit a `trendline’ to your

data, although this does not include a sine

function fit. Of course these are not the only

applications which can be used with our data

set.

Page 3: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

A very powerful Science tool for data-

logging is Vernier’s Logger Pro 3

software. This is designed to be used

with a wide range of Vernier sensors,

but you can also paste in your own

data. The Time and Acc data have

been pasted into the Data Set to

produce the scattergram. Logger Pro

has computed its best fit sine function

for the data, and added an extra CC

column of computed data for

comparison. You can also take a

video clip of the experiment and

import that into Logger Pro. You can

play it manually, or you can

synchronise it with the data so

that tracing a point on the graph

moves to the corresponding video

frame. The UK distributor, IDS,

sells a school site-licence for £350

which covers all students and staff

having copies of the software in

school and at home. I very much

hope that the software will be

developed to accept sensed data from micro:bits by either USB serial cable or via BlueTooth Low

Energy (BLE) wireless.

A science application which will collect data wirelessly using BLE is the very

friendly Bitty Data-logger App for Android and Apple mobile devices. Here is a

second experiment with a micro:bit and battery strapped to a heavy wooden

model elephant suspended from a large spring. You need to download the hex

file for the micro:bit here. Once this has been uploaded to the micro:bit, you will

be asked to `Draw a circle’ to calibrate the on-board magnetometers. Once the

micro:bit is set up and waiting, you press the `Scan’ button on App to see which

micro:bits are set up with

BLE. It has found mine, so

when I click on it I can start

entering the settings for my

experiment. You now need

to press `Settings’ and make some choices about how to collect the data.

Page 4: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

The first trial just

shows accelerometer

data from the y-

(vertical) sensor.

When you press

`Start’, the data-

logging and graphing

begin. When you press `Stop’, the data is stored in a `comma separated variables’ file (CSV). You

press `Results’ to see the name of your file, which includes todays’ date and time. In order to

export it to the PC you use `Share’, which gives you plenty of options. I usually just e-mail the file to

myself with the CSV file as the attachment. Then I can open it from the message in my PC’s e-mail

Inbox directly into Excel and chose which columns to use for graphing data. As the elephant only

moves up and down, the x-accelerations are zero. As the micro:bit is not quite vertical on Ellie’s

back, there is a small z-component of acceleration. Now we have the data in Excel we can do some

plotting and analysis. The red-blue graph is a scatterplot of the y (blue) and z (red) components of

acceleration. The units are those of gravity: g ms-2. The red-green-blue graph shows x-, y- and z-

against t. Column E is the values from A divided by 1000 to give times in seconds. Column F is the

negative of the magnitude of the acceleration. The blue graph is the data from column F against

that from column E – so it is the acceleration-time graph for Ellie’s swings. I have computed the

mean value from column F as -1.02372. The expected value is -1, so a pretty accurate result.

The data logger will also capture data from probes attached to the micro:bit’s analog pins P1 or P2.

Page 5: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

This technique uses my expensive Samsung smart phone to collect the micro:bit data wirelessly and

to send it my PC via my home wireless broadband. We can do that in a much cheaper and simpler

way by using two micro:bits. One micro:bit, call it M1, will be collecting data and sending it using

MakeCode’s `Radio’ commands to the second micro:bit M2, which will be attached by a USB cable

to a PC. This will use `Radio’ commands to receive the data, and then `Serial’ commands to transmit

them. Here is the program for M1. There are just three blocks. `On start’ turns on the radio and

displays `R’. The `On radio’ block waits for a string to be sent from M2 to trigger the data-logging

and to pass values for the variables `max’ and `gap’. The `senddata’ function uses a counted loop

to collect and send the data to M2. `tim’ is the elapsed time in milliseconds and `acc’ is the size of

the acceleration in milli-g.

The program for M2 also has 3 blocks. Button A is pressed to start the data-capture. This sends 3

values to M1 which trigger the data-logging and radio transmission. Each time a value is received

from M1, the `on radio’ block stores it in a variable. When the `acc’ value is received it calls the

`serialline’ function which sends the 3 variables, count, time and acc, as a serial line.

Page 6: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

So first open Tera Term and then press button A on M2. After 10

seconds we have collected all the data, ready to copy and paste

them into a PC application, such as GeoGebra, where we can do

the display and modelling.

Instead of using Tera Term

to collect the transmitted

data, we can send it

directly to Excel using a

free plug-in from

Microsoft called Project

Cordoba. You need to

request a copy of Cordoba

from the Microsoft

Garage Project’s site. Use

this link. You should get a quick

response. When you install it

there will be an extra `Project

Cordoba’ tab at the end of the

toolbar.

The first option is to `Connect a

Device’, which will detect that you

have attached a micro:bit. It also

tells you which COM port it is

using, COM10 in my case.

Page 7: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

If you click on `Start Data’ and then on `Stop Data’, you

will see that three extra tabs have been added at the

foot of the sheet. The display shows the `Data In’ sheet.

This has a row of cells which will receive incoming data,

and a block beneath them which will build up the

received data set. `Data Out’ has a row of cells which

will transmit

data to the

micro:bit.

`Settings’ allows you set

the rate of transmission for

the data in ms, the size of

the storage area and the

number of channels to use

for sending and receiving.

There is one other

important setting to make, which is the baud rate for serial

communication, set to 9600 by default.

Click on the `Advanced’ icon to open Cordoba’s `Console’. Change

the 9600 baud setting to the micro:bit’s value of 115200.

We already have the code to

send data from micro:bit M1, on

the pendulum, wirelessly to

micro:bit M2, connected to the

PC. We just need to tweak the code for M2 to receive M1’s data

and to forward it down the serial cable to Excel’s `Data In’. We can

also use Excel to output the signal to start the experiment’s data-

logging. First, we’ll try a little experiment to use Excel to send data

to M2 to signal the start of the experiment. In `Settings’I have

chosen to have just 3 Data Channels. On the `Data Out’ page I will

use cell A5 (CH1) to control the output from Excel to micro:bit M2.

Page 8: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

Looking at the `Console’ display you can see that each output TX is a line

consisting of the contents of cells A5, B5 and C5 separated by commas.

A new output is sent whenever any cell is changed.

Here is the receiving program for the serial output test. When you press

Button A, the program will continuously read the lines of transmitted

data as a string. The variable CH1 will hold the first character from the

received string. Although it was stored as a number

in Excel, it is sent as a string for the serial output.

The program carries on receiving and displaying

incoming data from the `Data Out’ cell A5 until the

value `1’ is entered.

M2 will be sending 3 readings per sample: the

sample count, the calculated time in ms and the

`acceleration strength’ in thousandths of g ms-2. We

now have a few decisions to make about the

number of samples to collect, the gap between

samples and the sensed data to transmit. I have

chosen to collect 200 samples

at 50 ms intervals. So we can

set these up on the Excel

`Settings’ sheet. The number

of data rows is stored in the

Excel variable

`DataRowCountSettings’, and

the data interval is in

`DataDelaySetting’. These can be used to copy the values from `Settings’

into CH2 and CH3 of `DataOut’. The new code for M2 is below.

Page 9: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

In order to receive and decode the comma-separated, serial

string sent from Excel, we need to do some text-handling and

decoding to extract the data from CH2 and CH3. The line of

data is held in the variable `string’. The first character is tested

until a `1’ is received. Then `collectstring’ function stores the

length of the string in `length’ and calls the `splitstring’

function which uses commands from the `Advanced’ and `Text’

blocks. The variable `left’ holds the single digit from CH1 as a

string. Its numerical value is stored as `trigger’. Similarly,

`centre’ holds the three digit string from CH2, and `max’ holds

its numerical value. `right’ holds the final 2 digit string from

CH3, and `gap’ holds its value. The `senddata’ function passes

the values of trigger, max and gap wirelessly to M1. The other

two blocks receive the data wirelessly from M1 and send them

by the serial cable to Excel where they are collected in

`DataIn’. Once there, you can highlight columns C and D, and

`Insert’ a `Scattergram’.

We have no need to edit the program for M1 which is already set to receive the line of data from

M2 carrying the values of trigger, max and gap wirelessly.

So with the micro:bit, Excel and Project Cordoba, we have a very simple, powerful and cheap data-

logging and modelling system which can be used for telemetry. But actually it is even more

powerful than that, because we have spare capacity in the micro:bit to carry out control operations.

Page 10: BBC Micro:bit some data-logging and control examples · BBC Micro:bit – some data-logging and control examples. Adrian Oldknow adrian@ccite.org 12th January 2018 A very basic experiment

As a very simple example, we can output a tone

to a buzzer attached to micro:bit M1’s P0 pin

which varies according to the size of the

acceleration. The Kitronik MI-power case for the

micro:bit not only provides a 3V power source

from a disc battery, but also has an on-board

buzzer. We can use the `ring tone’ command

from the `Music’ menu. The frequency is given

as a number of Hertz (Hz), cycles per second. In

the little amendment to the M1 program we

store the acceleration strength in the `accval’

variable, which is sent out by the 2nd `radio send’

block. We can do a little maths to make sure the frequency is a sensible value – say between 0

and 1000 Hz. At the end of the data transfer we can use a `rest’ command to turn the buzzer off.

This is a pretty useless example of control. Maybe you could connect light and temperature

sensors to pins P1 and P2 to gather and display data, e.g. in Excel, while also using the micro:bit to

control an LED and fan according to the sensed environment? Such decisions could be made locally

in the micro:bit program, or remotely by a PC App.

Now the principle of 2-way data communication by serial cable or Bluetooth Low Energy radio

transmission has been established, the world is indeed your oyster. I am constantly amazed by the

creative potential that lurks within such a mighty electronic mouse costing a mere £15.