Microcontroller programming with BBC micro:bit
MicroPython Commands
Table 2 lists the most important MicroPython commands for communicating with the micro:bit's sensors. There are commands for the matrix display (Figure 4), the buttons, and the digital input and output.
Table 2
micro:bit Commands
Function | Effect |
---|---|
microbit.display.show(Image.HAPPY) |
Displays a smiley face on the LED matrix |
microbit.display.show(Image.SAD) |
Displays a frowning face on the LED matrix |
microbit.display.set_pixel(1, 2, 9) |
Switches the LED at position (1,2) to maximum brightness |
microbit.button_a.is_pressed() |
Checks whether button a is pressed |
microbit.button_a.was_pressed() |
Checks whether the button was pressed after switching on, or the last request |
microbit.button_a.get_presses() |
Counts how many times the button was pressed after the last request |
microbit.accelerometer.get_x() |
Reads the x-coordinates of the position sensor |
microbit.pin0.read_digital() |
Reads the digital input at pin 0 |
microbit.pin0.write_digital(1) |
Sets the digital input at pin 0 |
music.play(music.RINGTONE) |
Plays a tune if a speaker is connected to pin 0 |
The command is always preceded by the name of the matching MicroPython library. |
The acceleration sensor also serves as a position sensor when the module is at rest. In order not to have to deal with the orientation angles of its three axes, there is the microbit.accelerometer.current_gesture()
command. It understands the orientations up
, down
, left
, right
, face up
, face down
, and shake
.
A magnetometer with three axes is also available. In theory, it is sensitive enough to detect the direction of the Earth's magnetic field – in practice, it is difficult to do so. The temperature sensor is similar: It also measures the processor temperature. However, under no circumstances should you heat up the board with a hair dryer – be content with measuring the ambient temperature.
The microbin.pin0.is_touched()
command connects the digital input via a 10m ohm pull-up resistor. The micro:bit uses this to detect if there is too high of a resistance to ground. Typically, one finger is sufficient for this, while another finger simultaneously touches the micro:bit's ground cable (GND, the large contact at bottom right on the board).
Interface
Once flashed, the microcontroller no longer accepts commands. Instead, it switches to communication and outputs its information serially via the USB connection. In Figure 5, the connected micro:bit passes the brightness value to Mu's REPL command window via the print()
command. If you close the program, you get direct access to the data.
You already know the device name for the serial interface, in our case ttyACM3
, as shown in Listing 1. The commands in Listing 4 redirect the micro:bit's output to the terminal window. The cat
command returns the brightness value as a decimal number; od -x
returns it as a hexadecimal number (Figure 6).
Listing 4
Redirecting Output to the Terminal
$ cat /dev/ttyACM3 $ od -x < /dev/ttyACM3
Hex Files
The Mu editor converts the program code into a hex file. If you disconnect the micro:bit before flashing, the Mu editor enables access to the file and asks where to store it.
The hex file consists of an 8KB MicroPython interpreter. MicroPython is a reduced Python instruction set, which feels like real Python in its basic functions. Additionally, the file contains the program's byte sequence, which the MicroPython interpreter then executes. Finally, the hex file contains a compressed version of the actual program text including comments.
Contrary to what the name might suggest, the hex file is initially a text file: The address and bytes are written out as text (Listing 5). The file therefore shrinks as soon as the system converts the ASCII text into binary numbers. This explains why a hex file of more than 500KB fits into the micro:bit's memory, which has a capacity of 256KB – including the Micropython interpreter. In the end there are about 8KB of physical memory for your own python scripts.
Listing 5
Sample Hex File
:020000040000FA :1000000000400020218E01005D8E01005F8E010006 :1000100000000000000000000000000000000000E0 [...]
Flashing the micro:bit via a connected computer is a more elegant solution. The micro:bit logs on to the computer's filesystem as a USB stick, so you just need to copy the hex file to it. If the micro:bit recognizes the format, it converts the content into a byte sequence and immediately writes it to its flash memory. Otherwise, the file remains in USB memory, just like on a normal memory stick.
Although flashing always overwrites the old data, the data is retained when the power supply is disconnected. The micro:bit executes the program once it has been loaded, as soon as you resupply power.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.