Code to support the Product Design Electronic Things course using the Arduino and BBC micro:bit
There are a number of ways you can interact with the micro:bit depending what you are comfortable with.
MakeCode Block is a Scratch-like environment for coding with the micro:bit.
You can write blocks code on the make code website
Blocks is a simplification of programming with the micro:bit and a great place to start if you are new to programming. Some operations that would usually be complex have already been encapsulated into function blocks, making things like radio and bluetooth communication much easier.
MakeCode Blocks are essentially just javascript code, if you press the Javascript switch you can start editing the code directly
Use blocks to access filesystem
To stream data over bluetooth via make code, do the following using Google Chrome
- create a project
- Click settings cog
- project settings set "No Pairing Required: Anyone can connect via Bluetooth."
- start the ble uart service and write data over uart
bluetooth.startUartService();
basic.forever(function() {
bluetooth.uartWriteValue("x", input.acceleration(Dimension.X));
bluetooth.uartWriteValue("y", input.acceleration(Dimension.Y));
bluetooth.uartWriteValue("z", input.acceleration(Dimension.Z));
basic.pause(200);
});
- Click settings cog
- click about
- select Experiments
- activate bluetooth
- go back
- Click settings cog
- pair bluetooth
You should now see a show device console and you can export data from the session.
Write python code for the micro:bit in the browser or download the Mu editor.
Python makes operations like parsing large amounts of data and writing data to files quite trivial. If your project involves saving a lot of data, think about writing in python
You can write for the micro:bit from the Arduino IDE. If you already know C++ or would like to learn more about using the Arduino platform, this may be a good place to start.
There is a fairly comprehensive guide to setting up the micro:bit for use with the Arduino IDE at Adafruit's Website. This is still being developed so some functions may not be available with the Arduino IDE that are available with other programming languages.
The microbit is capable of broadcasting bluetooth low energy data. The following tools will come in handy if you wish to monitor information over bluetooth
To setup bluetooth in make code, you must first add the bluetooth. In the blocks menu, select extensions and add bluetooth.
Logging data via bluetooth is currently still experimental. You will need to select About
then experiments and choose bluetooth console.
At the moment, the console only seems to like data in the format of a key
/ value
pair. So, use the bluetooth.uartWriteValue
block for now.
These examples relate to programming the microbit with the Arduino IDE. make sure to follow the setup instructions at Adafruit
Simple demonstration of sending data over BLE.
Broadcast accelerometer data over a BLE UART service. Remember to install the correct libraries. You should also install a BLE tool.
Drag and drop the contents of the libraries folder into your Arduino/libraries
otherwise some examples will not compile.
For the new version microb:bit there is a Combined Accelerometer / Magnetometer. Use the LSM303AGR library.
For using the accelerometer and using the magnetometer, first Check Your Board Version
It should be the Combined Accelerometer / Magnetometer (LSM303AGR)
Select syntax highlighting in the following file
/Applications/Arduino.app/Contents/Resources/Java/lib/theme/theme.txt
Sometimes everything can just stop working. If that is the case, a good first step is to perform a factory reset.
From there, try and build your program back up gradually to identify where something went wrong.