* First 'science experiments' lesson * Add package tag for 'radio' * Add attributions, links, and license logos
10 KiB
Setup and procedure
micro:bit setup and coding concepts
This document describes different methods using micro:bits to collect and record data for science experiments. There are several ways to collect data from an experiment. The simplest is having the data display on the LED screen and manually record the data on a paper. Data can also be collected using the Window’s 10 MakeCode app. The third way is using 2 micro:bits with one observing the data and then radioing the results to a second micro:bit so it can allow the remote collection of data.
Data collection options
Option 1 - LED display
Data collection can be setup to display data collected from different micro:bit sensors on the LEDs by using a |||basic:show number||
block from the ||basic:Basic||
toolbox:
basic.showNumber(input.temperature())
As data is displayed it can then be recorded on a paper for further analysis.
Data can also be displayed graphically on the micro:bit using the ||led:plot bar graph o||
block from the ||led:LED||
toolbox.
led.plotBarGraph(
input.temperature(), 0
)
Option 2 - micro:bit Windows 10 MakeCode app and a USB connection
The Windows 10 MakeCode app allows data to be directly read from the micro:bit when it is attached using USB cable. Data can be sent from the micro:bit to the Windows 10 MakeCode app using serial data connection. The data collected over the serial connection can be graphed and the data can be downloaded. A limit of only about the last 20 seconds of data can be downloaded as a "data.csv" file. This allows the collection of data in real time. This file can be opened in a spreadsheet for further analysis. Many different kinds of experiments can be performed using this data logging technique.
You can download the Windows 10 MakeCode app.
With the program downloaded from the MakeCode app to the micro:bit and the USB cable left connected and using the ||serial:serial write value||
block from the ||serial:Serial||
toolbox in the Advanced tool section.
serial.writeValue("Celsius", input.temperature())
When the program is running, a purple Show data Device button shows up under the Simulator. By clicking on the button the data being observed can be monitored and graphed in the Show data Device window.
The graph is highlighted with the blue box. The Download button in the red highlighted box allows the downloading of the recorded data as a CSV file which can be opened in a spreadsheet and analyzed.
Option 3 - Remote radio collecting to receiving radio displaying
Two micro:bits can be used to collect and record data using the radio commands. One micro:bit can be setup remotely and the other micro:bit can be used to observe the data. The first micro:bit can send the data it observes to the second micro:bit for the observer to record. To set up 2 micro:bits so they can communicate over the radio they need to be on the same radio group.
micro:bit radio sending code
In the starting of the code the title is displayed, radio group 99 is setup, and the initial temperature
variable is set to 0
.
In the forever loop the temperature is collected from the micro:bit sensor and stored in the temperature
variable. The temperature is displayed on the LED display. A radio signal is sent to all micro:bit radios in group 99
. The program pauses for 1000 milliseconds and then loops again.
// Temperature Radio Sender
// by C Lyman
// January 2018
temperature = 0
basic.showString("TEMPERATURE RADIO REMOTE")
radio.setGroup(99)
let temperature = 0
// Forever Loop event
basic.forever(() => {
temperature = input.temperature()
basic.showNumber(temperature)
radio.sendNumber(temperature)
basic.pause(1000)
})
Shared "Temperature Radio Sender" program: https://makecode.micro:bit.org/_DLmi641H5CP0
Radio receiver code
In the starting of the code the title is displayed, radio group 99 is setup, and the initial temperature
variable is set to 0
.
In the radio received event, the temperature is received from sending the micro:bit radio. The received temperature is then displayed on the LED display. This is repeated whenever a radio signal is received.
// Temperature Radio Receiver
// by C Lyman
// January 2018
temperature = 0
basic.showString("TEMPERATURE RADIO RECEIVER")
radio.setGroup(99)
let temperature = 0
// Radio Packet Received event
radio.onDataPacketReceived( ({ receivedNumber: temperature }) => {
basic.showNumber(temperature)
})
micro:bit radio receiver code with serial write
This code is the same as above but one additional line of code is added to write to the word "Celisus"
and the temperature to the MakeCode app to the USB serial connection. This is the same as described peviously in Option 2.
// Temperature Radio Receiver
// by C Lyman
// January 2018
temperature = 0
basic.showString("TEMPERATURE RADIO RECEIVER SERIAL")
radio.setGroup(99)
let temperature = 0
// Radio Data Packet Received event
radio.onDataPacketReceived( ({ receivedNumber: temperature }) => {
basic.showNumber(temperature)
serial.writeValue("Celisus", temperature)
})
Shared "Temperature Radio Receiver" program: https://makecode.micro:bit.org/_44HRETXpC2ib
Sample Project to Collect Accelerator Data
This sample project demonstrates the coding of the 2 micro:bits to collect data from the micro:bit accelerator sensor. It will also show the data collection and downloaded data to be further analyzed in a spreadsheet.
"Sender" micro:bit code
Code the first micro:bit using MakeCode for micro:bit. Name the project, "Gravity Sender".
The ||basic:on start||
event will display the title and function of the micro:bit in all caps, "GRAVITY SENDER"
. Add some comments to the ||basic:on start||
event:
- Name of the project
- Creator
- Date created
Set up a radio group using the ||radio:radio set group||
. Both micro:bits need the same radio group.
// Gravity Sender
// by C Lyman
// Jan 2018
basic.showString("GRAVITY SENDER")
radio.setGroup(99)
The ||basic:forever||
event will constantly monitor the strength of the acceleration and send the value to any other micro:bits that might be receiving radio signals in the same radio group.
Open the pull down menu in the ||input:acceleration||
block and and change the x
value to the strength
value. This maximizes the x, y, and z dimensions of the acceleration into one value.
basic.forever(() => {
radio.sendNumber(input.acceleration(Dimension.Strength))
})
Shared "Gravity Sender" program: https://makecode.micro:bit.org/_83oRcxDfyPpa
"Receiver" micro:bit code
Using the Windows 10 MakeCode app, setup and code the second micro:bit. This micro:bit will remain connected to the computer through the USB cable and the Windows 10 MakeCode app to monitor the data being received.
Name the project, "Gravity Receiver". The ||basic:on start||
event will display the title and function of the micro:bit in all caps, "GRAVITY RECEIVER"
. Add comments to the ||basic:on start||
event like before: Name the project, creator, and date created. Set up a radio group using the ||radio:radio set group||
block. Both micro:bits need the same radio group.
// Gravity Receiver
// by C Lyman
// Jan 2018
basic.showString("GRAVITY RECEIVER")
radio.setGroup(99)
The ||radio:on radio received||
event will constantly monitor radio signals from the radio group.
When a value is received from the group it is stored in the gravity
variable.
The ||serial:serial write value||
sends 2 pieces of data back to the MakeCode app through the USB cable. First it sends a label "gravity"
and then the value received as gravity from the ||input:acceleration||
method from the first micro:bit.
radio.onDataPacketReceived( ({ receivedNumber: gravity }) => {
serial.writeValue("gravity", gravity)
})
Shared "Gravity Receiver" program: https://makecode.micro:bit.org/_gXjhkyb3PWzh
Monitoring, downloading, and analyzing the data
With the micro:bit code downloaded from the MakeCode app to the micro:bit and the USB cable connected it will start receiving data from the first micro:bit. Under the simulator in the app a purple outlined button shows up Show data Device.
By clicking on the Show data Device button a window opens up to the right showing values and graph of the gravity data being received (The dips in the graph are 3 tosses of the micro:bit in the air).
The Download button in the red highlighted box allows the downloading of about the last 20 seconds of recorded data as a CSV file.
When the data recorded is downloaded as a CSV spreadsheet file, it is named "data.csv".
~hint
The CSV file usually opens in directly into a spreadsheet but sometimes it doesn’t which makes it hard to find. A search of the C:\ drive might be necessary to find it.
~
Here is a link to sample downloaded data from the "data.csv" file that's already imported into Google Sheets:
https://docs.google.com/spreadsheets/d/1L_3Gq61UFEEjVZYQk31yTwzrTF3nlv55Ic2CjQdbIW8/edit?usp=sharing
Additional analysis and graphing can be done in a spreadsheet.
Adapted from "Microbit Data Collection Methods" by C Lyman |
radio