11 May 2022
Datalogging with micro:bit V2 - part 2
In the previous post we looked at the new features of micro:bit V2, and how to set up the makecode environment to code it.
The code below sets up the microbit V2 to record the sound level snd store the data.
You can open this code here – https://makecode.microbit.org/_bAWXAuMVjK4c
First, you need to create an array in the micro bit, just like you did with a variable in previous posts. Think of an array as a table, with columns and rows, where data is stored. As we are only going to store the values of a single sensor, sound level in this example, we setup an array with a single column. Every time the sensor takes a reading, the value will be placed on a different row in that column.
In an On Start event code block, drag and drop a Set columns block. Reduce the number of columns by clicking the – (minus sign). Replace the letter “a” with the words Sound Level (something that describes the value). The next three commands show a tick icon for 2 secs. This is just to show that the code is running.
The next event block sets up by taking a reading every second (1000ms). This value could be changed to any time interval you like but must be in ms.
The next command block - log data, takes a reading from the sound sensor and stores it in the array. But, unlike previous examples, where we only used variables, when a new value is recorded it would overwrite the previous value, in an array the new value is recorded on a new row in the column of the array that was set up.
So every second a new value will be added to the array until the microbit can no longer store any more data. The microbit V2 can record a total of 11000 entries, divided amongst the number of columns.
The next block is optional, mirror to serial allows the data to be sent to a computer and the data viewed in real-time. - Visualizing logged data with a microbit
The show Leds is just used to indicate that the microbit is logging the data.
On log full - indicates when the log is full and can no longer store any data.
On Button pressed, calls the command delete Log, which deletes the data stored in the array. You need to do this as the array doesn't reset every time you use it, data is just added to the end of the last set of data collected. The pause is added to indicate to the user that they need to wait for this process to happen.
Download this code to your micro:bit and you are now ready to begin log changes in the sound levels.
In part 3 of this blog post, we will look at how to access and visualise the stored data.