Difference between revisions of "BBC:Microbit / Calliope"

From phyphox
Jump to navigation Jump to search
(Created page with "The BBC:Microbit is a Microcontroller that is designed to be easily programmable by young students. The Calliope is a very similar device, designed to be compatible to the Mic...")
 
Line 1: Line 1:
The BBC:Microbit is a Microcontroller that is designed to be easily programmable by young students. The Calliope is a very similar device, designed to be compatible to the Microbit. Hence, we discuss both on the same page with a focus on the Microbit. You should be able to load the same programs onto the Calliope.
+
The BBC micro:bit is a Microcontroller that is designed to be easily programmable by young students. The Calliope is a very similar device, designed to be compatible to the Microbit. Hence, we discuss both on the same page with a focus on the Microbit. You should be able to load the same programs onto the Calliope.
  
Which experiment configuration you can use with the Microbit depends on the program loaded onto the microcontroller. You will need to enable the Bluetooth functions with such a program first. Examples will follow...
+
Which experiment configuration you can use with the Microbit depends on the program loaded onto the microcontroller. You will need to enable the Bluetooth functions with such a program first, so there are no experiments included with phyphox, but example combination of Microbit code and phyphox configurations can be found below.
 +
 
 +
'''Important: Unlike most other Bluetooth Low Energy devices, the microbit needs to be paired with the phone to be used. You should use the Microbit app to do that (or the Bluetooth settings on Android - was not able to do this without the Microbit app on iOS).'''
 +
 
 +
== Read IO Pins ==
 +
 
 +
To make the IO pins available via Bluetooth, you just need to write a minimal program for the Microbit that starts the IO Bluetooth Service once:
 +
 
 +
[[File:Microbit io.png]]
 +
 
 +
That's it. Upload it to your Microbit and it is ready to be used (don't forget to pair it).
 +
 
 +
Now, at the phyphox end, you need to figure out, on which characteristic the data can be received and how it is formatted. According to the Microbit documentation, this is E95D8D00-251D-470A-A062-FA1922DFA9A8, but the response will be a series of byte pairs and each pair gives the pin number and its value. In principle, we could even match and parse these in phyphox, but for this simple example, we can make our life a bit easier by only using a single pin. If there is only one pin that is read, the io data will be a single pair of bytes. The first will always be the pin number and the second will be the value. Therefore, if set up correctly, we only need to read the second byte of E95D8D00-251D-470A-A062-FA1922DFA9A8:
 +
 
 +
<code>
 +
<output char="E95D8D00-251D-470A-A062-FA1922DFA9A8" offset="1" conversion="singleByte">v0</output>
 +
</code>
 +
 
 +
Now, how do we tell the Microbit that we only want a single pin? Well, there are the characteristic E95DB9FE-251D-470A-A062-FA1922DFA9A8, which is a bit mask (a series of 1s and 0s, where each digit represents an option), that defines which pin is an input and which is an output. We only set one of those to "1" and the others to "0", which means that only one pin is an input. Also, there is characteristic E95D5899-251D-470A-A062-FA1922DFA9A8 with a similar bit mask, with which we can tell the Microbit that we want to do an analog read. So, we do the following config:
 +
 
 +
<nowiki>
 +
<config char="E95D5899-251D-470A-A062-FA1922DFA9A8" conversion="hexadecimal">01000000</config>
 +
<config char="E95DB9FE-251D-470A-A062-FA1922DFA9A8" conversion="hexadecimal">01000000</config>
 +
</nowiki>
 +
 
 +
(A little detail as this might be confusing: The Microbit looks at the 20 least significant bits, so we want to set the "x" in the pattern xxxxxxxxxxxxxxxxxxxx000000000000 (20 "x" and 12 "0" to get a total of 32 bits). phyphox requires an input in hexadecimal, so groups of 8 bits are expressed as a byte in hexadecimal: xxxxxxxx xxxxxxxx xxxx0000 00000000. Setting the first bit leads to the hexadecimal number 01 00 00 00. Why not 80 00 00 00? Because we want to set the least significant bit, not the first one in the first one written in our notation.)
 +
 
 +
Finally, we also want to have a time axis, so we tell phyphox to also save the time when a new value arrives:
 +
 
 +
<code>
 +
<output char="E95D8D00-251D-470A-A062-FA1922DFA9A8" extra="time">t</output>
 +
</code>
 +
 
 +
So, in total we have
 +
<nowiki>
 +
<bluetooth name="BBC micro:bit">
 +
    <config char="E95D5899-251D-470A-A062-FA1922DFA9A8" conversion="hexadecimal">01000000</config>
 +
    <config char="E95DB9FE-251D-470A-A062-FA1922DFA9A8" conversion="hexadecimal">01000000</config>
 +
    <output char="E95D8D00-251D-470A-A062-FA1922DFA9A8" offset="1" conversion="singleByte">v0</output>
 +
    <output char="E95D8D00-251D-470A-A062-FA1922DFA9A8" extra="time">t</output>
 +
</bluetooth>
 +
</nowiki>
 +
 
 +
The following experiment configuration does all the above and also adds a little calibration. This calibration assumes that the range is 3V and that we have a single byte (a number from 0 to 255), so it is just 3V/255. Note, that the Microbit is designed to measure voltages. This is only an estimate and you will encounter effects that would be unusual for a real voltmeter, but that are typical for analog inputs of a microprocessor (like floating voltages and a battery dependent logic level as reference).
 +
 
 +
So, just try this configuration in phyphox:
 +
 
 +
[[Media:BBC Microbit Voltage.phyphox]]
 +
 
 +
 
 +
== Write number to LED service ==
 +
 
 +
This example works in the other direction. We will use the accelerometer of the phone to detect a shake. For each shake, we count upwards and write the numer of shakes to the LEDs of the Microbit.
 +
 
 +
On the Microbit, you only have to enable the LED service:
 +
 
 +
[[File:Microbit led.png]]
 +
 
 +
The phyphox experiment can now simply write numbers to the characteristic E95D93EE-251D-470A-A062-FA1922DFA9A8, so any number we want to show on the Microbit, can be written as follows:
 +
<nowiki>
 +
<bluetooth name="BBC micro:bit">
 +
    <input char="E95D93EE-251D-470A-A062-FA1922DFA9A8" conversion="singleByte">char</input>
 +
</bluetooth>
 +
</nowiki>
 +
 
 +
The rest of the experiment simply reads the accelerometer, checks if it is above a threshold and than adds one to the counter. You can try it below:
 +
 
 +
[[Media:BBC Microbit Counter.phyphox]]
 +
 
 +
 
 +
== Move point on LED service ==
 +
 
 +
This is just another example. As above, it only requires the LED service:
 +
 
 +
[[File:Microbit led.png]]
 +
 
 +
The experiment configuration is rather complex because it needs to compose the bits that have to be written to the LED service. It is probably only relevant if you want to do something similar and you should really know how the phyphox experiment configurations work:
 +
 
 +
[[Media:BBC Microbit Matrix.phyphox]]

Revision as of 23:03, 16 June 2019

The BBC micro:bit is a Microcontroller that is designed to be easily programmable by young students. The Calliope is a very similar device, designed to be compatible to the Microbit. Hence, we discuss both on the same page with a focus on the Microbit. You should be able to load the same programs onto the Calliope.

Which experiment configuration you can use with the Microbit depends on the program loaded onto the microcontroller. You will need to enable the Bluetooth functions with such a program first, so there are no experiments included with phyphox, but example combination of Microbit code and phyphox configurations can be found below.

Important: Unlike most other Bluetooth Low Energy devices, the microbit needs to be paired with the phone to be used. You should use the Microbit app to do that (or the Bluetooth settings on Android - was not able to do this without the Microbit app on iOS).

Read IO Pins

To make the IO pins available via Bluetooth, you just need to write a minimal program for the Microbit that starts the IO Bluetooth Service once:

Microbit io.png

That's it. Upload it to your Microbit and it is ready to be used (don't forget to pair it).

Now, at the phyphox end, you need to figure out, on which characteristic the data can be received and how it is formatted. According to the Microbit documentation, this is E95D8D00-251D-470A-A062-FA1922DFA9A8, but the response will be a series of byte pairs and each pair gives the pin number and its value. In principle, we could even match and parse these in phyphox, but for this simple example, we can make our life a bit easier by only using a single pin. If there is only one pin that is read, the io data will be a single pair of bytes. The first will always be the pin number and the second will be the value. Therefore, if set up correctly, we only need to read the second byte of E95D8D00-251D-470A-A062-FA1922DFA9A8:

<output char="E95D8D00-251D-470A-A062-FA1922DFA9A8" offset="1" conversion="singleByte">v0</output>

Now, how do we tell the Microbit that we only want a single pin? Well, there are the characteristic E95DB9FE-251D-470A-A062-FA1922DFA9A8, which is a bit mask (a series of 1s and 0s, where each digit represents an option), that defines which pin is an input and which is an output. We only set one of those to "1" and the others to "0", which means that only one pin is an input. Also, there is characteristic E95D5899-251D-470A-A062-FA1922DFA9A8 with a similar bit mask, with which we can tell the Microbit that we want to do an analog read. So, we do the following config:

<config char="E95D5899-251D-470A-A062-FA1922DFA9A8" conversion="hexadecimal">01000000</config>
<config char="E95DB9FE-251D-470A-A062-FA1922DFA9A8" conversion="hexadecimal">01000000</config>

(A little detail as this might be confusing: The Microbit looks at the 20 least significant bits, so we want to set the "x" in the pattern xxxxxxxxxxxxxxxxxxxx000000000000 (20 "x" and 12 "0" to get a total of 32 bits). phyphox requires an input in hexadecimal, so groups of 8 bits are expressed as a byte in hexadecimal: xxxxxxxx xxxxxxxx xxxx0000 00000000. Setting the first bit leads to the hexadecimal number 01 00 00 00. Why not 80 00 00 00? Because we want to set the least significant bit, not the first one in the first one written in our notation.)

Finally, we also want to have a time axis, so we tell phyphox to also save the time when a new value arrives:

<output char="E95D8D00-251D-470A-A062-FA1922DFA9A8" extra="time">t</output>

So, in total we have

<bluetooth name="BBC micro:bit">
    <config char="E95D5899-251D-470A-A062-FA1922DFA9A8" conversion="hexadecimal">01000000</config>
    <config char="E95DB9FE-251D-470A-A062-FA1922DFA9A8" conversion="hexadecimal">01000000</config>
    <output char="E95D8D00-251D-470A-A062-FA1922DFA9A8" offset="1" conversion="singleByte">v0</output>
    <output char="E95D8D00-251D-470A-A062-FA1922DFA9A8" extra="time">t</output>
</bluetooth>

The following experiment configuration does all the above and also adds a little calibration. This calibration assumes that the range is 3V and that we have a single byte (a number from 0 to 255), so it is just 3V/255. Note, that the Microbit is designed to measure voltages. This is only an estimate and you will encounter effects that would be unusual for a real voltmeter, but that are typical for analog inputs of a microprocessor (like floating voltages and a battery dependent logic level as reference).

So, just try this configuration in phyphox:

Media:BBC Microbit Voltage.phyphox


Write number to LED service

This example works in the other direction. We will use the accelerometer of the phone to detect a shake. For each shake, we count upwards and write the numer of shakes to the LEDs of the Microbit.

On the Microbit, you only have to enable the LED service:

Microbit led.png

The phyphox experiment can now simply write numbers to the characteristic E95D93EE-251D-470A-A062-FA1922DFA9A8, so any number we want to show on the Microbit, can be written as follows:

<bluetooth name="BBC micro:bit">
    <input char="E95D93EE-251D-470A-A062-FA1922DFA9A8" conversion="singleByte">char</input>
</bluetooth>

The rest of the experiment simply reads the accelerometer, checks if it is above a threshold and than adds one to the counter. You can try it below:

Media:BBC Microbit Counter.phyphox


Move point on LED service

This is just another example. As above, it only requires the LED service:

Microbit led.png

The experiment configuration is rather complex because it needs to compose the bits that have to be written to the LED service. It is probably only relevant if you want to do something similar and you should really know how the phyphox experiment configurations work:

Media:BBC Microbit Matrix.phyphox