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. The BBC Micro:bit 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.
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:
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 methods/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 methods/functions (methods and functions are the same thing I come from a Java background so I call them methods. In Python they’re usually referred to as 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 methods 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.