MicroPython is a scaled down version of Python 3 designed for micro controllers such as the ARM Cortex M0 that is on the BBC Micro:Bit V1. The BBC Micro:bit V1 has 16KB of RAM which limits what we can do with the board as MicroPython needs at least 8KB to actually do anything. Due to this limitation the Bluetooth module on the BBC Micro:Bit can not be used in conjunction with MicroPython; the Bluetooth drivers take up 12KB of RAM alone. However, if you require wireless communication you can use the Radio function provided by the Bluetooth module instead to communicate with other Micro:Bits. In one of the later tutorials I will show you how to control the Bit:Bot from a mobile phone over Bluetooth, because of the limitations stated above this code will be written using the Microsoft PXT toolset instead of MicroPython.
UPDATE: The Micro:Bit Version 2 has 128KB of RAM which means that the Bluetooth stack should be available to be used with MicroPython.
No programmer can remember all features offered by programming languages, so we use reference libraries. The below link will take you to the BBC Micro:Bit MicroPython Reference Library, have a look, it will probably confuse you at the moment but as you start to understand the language and write your own programs you will find it very useful:
https://microbit-micropython.readthedocs.io/en/0.9
MicroPython code on this website will have the following structure:
- Comments at the top of the page these are defined using the # character
- If libraries are imported these will be next
- All pin assignments
- All global variables
- All program functions
- Program loop
This might seem a little abstract at the moment , once you look at the first example it will start to make sense. I have explained what each part does below:
- Comments at the top of the page will give you a breakdown of what the code does.
- Libraries are bits of code written by other people that provide functionality. An example of this is the NeoPixel library that will allow us to control the multi-colour light emitting diodes (LED’s) on Bit:Bot.
- Pin assignments will let the Micro:Bit know what is connected to each pin.
- Global variables are locations where values can be stored and accessed by all functions.
- Program methods are bits of code that will do something. They’re defined using the def keyword.
- The program loop will loop forever when the Bit:Bot is switched on. This will most probably be used to call different program functions and contain most of your program logic.
I won’t go over the syntax of the language here; you will pick this up as you go through the tutorials. Please read the hints and tips section, I added this so you can avoid unnecessary frustration while coding in MicroPython (because it can be very frustrating at times).
Try to have the patients to work through the tutorials. They don’t just tell you how to program the Bit:Bot; they also demonstrate various programming techniques that can be used with any programming language and with any micro-controller/processor.
I’m no expert using MicroPython, I spent a few hours learning it so I could put these tutorials together and help you get the most out of your Bit:Bot. With this in mind I’m certain that after a bit a tweaking you can make huge improvements on the code provided here.
Happy coding!