micro:bit is a development board launched by the BBC for programming education for young people. It features a Bluetooth module, an accelerometer, a compass, three buttons, and a 5×5 LED matrix. With the help of the Python Foundation, micro:bit supports the MicroPython language. This article will introduce how to install the MicroPython editor mu on Raspberry Pi and upload programs written in MicroPython to run on micro:bit.
Required Hardware
micro:bit main board, microUSB data cable, 47Ω resistor, female-to-female jumper wires, crocodile clips, LED, Raspberry Pi and related accessories.
Installing Software
Enter the following command in the terminal to install the software:
sudo apt-get update && sudo apt-get install mu -y
Opening the mu Editor
MicroPython is a lightweight version based on Python 3, mainly used on microcontrollers (MCUs). mu is an open-source code editor designed specifically for children and can run on Raspberry Pi.
Open mu from the main menu of the Raspberry Pi system (desktop version).
Connect the Raspberry Pi and micro:bit together (using the microUSB data cable).
The system will pop up the following dialog: Just click cancel.
Using mu
The design of the mu software takes into account the age of the users, and the interface is very intuitive. 1. The New button opens a new file. 2. The Load button is used to open existing code you have written. 3. The Save button saves any work in the visible label. 4. The Flash button writes your code to micro:bit. 5. The Repl button opens an interactive shell. 6. The Zoom button changes the text size in the window. 7. The Theme button toggles between light and dark. You can choose your preference. 8. The Help button opens the Epiphany web browser and takes you to the help page. 9. The Quit button exits mu.
Using REPL
REPL is an interactive shell that runs on the micro:bit itself. Click the Repl button and wait for the interactive shell at the bottom to open:
You can click into REPL and immediately start writing code. Try the following two lines:
from microbit import *
display.scroll('Woop, woop')
Do you see the text scrolling on the micro:bit LED matrix? If not, you can type the second line again to scroll the message a second time:
display.scroll('Woop, woop')
REPL is a great place to write single lines of code to test them, but for larger scripts, you will need to use code files. Write and flash the code, then click the Repl button to close REPL. In the main window, you can now write a simple script to use the buttons on micro:bit:
from microbit import *
while True:
if button_a.is_pressed():
display.scroll('A PRESSED')
if button_b.is_pressed():
display.scroll('B PRESSED')
Now you can save the above code as what_pressed.py using the save button. Next, you need to use the mysterious Flash button. Press the button, and a dialog will appear: The amber LED at the bottom of the micro:bit should start blinking, indicating that the file is being loaded onto the micro:bit. Tap the button to see the text scrolling in the LED matrix.
More Fun
Additionally, there are GPIO pins, an accelerometer, and a compass on the micro:bit. If you connect an LED and a resistor using crocodile clips, as shown below.
We can make the external LED blink by shaking the micro:bit while displaying a specific pattern on the onboard matrix.
from microbit import *
shake = False
while True:
if shake:
pin0.write_digital(1)
display.show(Image.SQUARE)
else:
pin0.write_digital(0)
display.clear()
if accelerometer.was_gesture('shake'):
shake = not shake
sleep(500)
Flash the above code to the micro:bit and shake it to see the effect!
The links in the article can be clicked to read the original text at the end.
More Exciting Content
Make a Polaroid Camera with Raspberry Pi
DIY a Sound-Controlled Switch with Raspberry Pi
Make a Laser Cat Toy with ESP8266
Build a Fully Automatic Color Sorting Machine with Arduino
Complete Record of Making a Programmable Lava Effect Light (Part 1)
Build a Rechargeable 18650 Lithium Battery Pack with Unlimited Expansion
Make an LED Scrolling Display with Arduino