Today’s mission 5 min
Put Pybricks on your SPIKE Prime hub and make it light up, smile, beep and count down from a program you typed yourself.
You have programmed robots with blocks for four levels. From today you write Python, the language professional engineers use. You are not starting again. Every block you know has a line of Python that does the same job. This level shows you both, side by side, until you no longer need the blocks.
Why switch? Pybricks programs are short, quick to change and easy to read on one screen. They drive more accurately than blocks. A competition program that fills ten screens of blocks often fits on one page of Python.
The idea — new firmware, same hub 5 min
Firmware is the program that runs the hub itself. It starts the hub, talks to the motors and runs your code. The hub arrives with LEGO’s firmware, which runs SPIKE App programs. Pybricks is a different firmware, and it runs Python.
You write Pybricks programs in your web browser, at code.pybricks.com, and send them to the hub over Bluetooth. There is nothing to install on the computer.
Nothing is broken for ever. To go back to the SPIKE App, open Pybricks Code’s settings (the gear) and choose Restore official LEGO firmware. You can switch back and forth as often as you like.
Set up your hub 15 min
You need a charged SPIKE Prime hub, its USB cable, and Chrome or Edge. Firefox cannot talk to Bluetooth devices, and an iPad or iPhone will not work.
Has your teacher already installed Pybricks? If the hub’s name sticker says your team name, skip to Connect.
1. Open the install wizard
Go to code.pybricks.com. Click the gear button at the top left to open settings. Under Firmware, click Install Pybricks firmware.
2. Choose the hub, accept the licence, name it
- Select hub type: click the SPIKE Prime hub, then Next.
- Accept licenses: Pybricks is free, open-source software. Read the licence, tick the box, then Next.
- Configure options: type the hub’s name in Hub name. Keep it short. If you see “The name is too long”, shorten it. Then Next.
3. Put the hub in update mode
The last step shows a short video. These are the same steps, in words:
- Unplug the USB cable and make sure the hub is turned off.
- Press and hold the Bluetooth button on the hub.
- Still holding it, plug in the USB cable.
- Keep holding until the Bluetooth light blinks pink, green, blue, off, over and over.
- Let go of the Bluetooth button.
4. Install
- Click Install.
- A browser pop-up asks which device to use. Choose LEGO Technic Large Hub in DFU Mode and click Connect.
- Wait for the progress bar to finish. Do not unplug the cable until it does.
No “DFU Mode” device in the list? The hub is not in update mode. Unplug, switch off, and do step 3 again. Hold the button until the light really blinks. Letting go too early is the most common mistake. On Windows, also check the USB driver steps.
Windows only: the wizard asks for a USB driver
If you are using Pybricks on a Windows computer for the first time, the wizard may say you need to install a USB driver. Do this once, with the hub in update mode (“Put the hub in update mode” above) and plugged in:
- Right-click the Windows Start menu, then click Device Manager.
- Look for the hub under Other devices.
- Open it, and in its Properties click Update Driver…
- Click Browse my computer for drivers.
- Click Let me pick from a list of available drivers on my computer.
- Select Universal Serial Bus devices, then Next.
- In Manufacturer choose WinUsb Device. In Model also choose WinUsb Device. Click Next.
- Windows warns that the driver is not recommended. Click Yes. The driver works.
- Click Close. Now go back to the install and click Install.
Connect over Bluetooth
- Unplug the cable. Turn the hub on with the centre button. The light blinks blue: it is waiting for you.
- In Pybricks Code, click the Bluetooth button in the toolbar (“Connect using Bluetooth”).
- A pop-up lists nearby hubs. Pick your hub’s name and click Pair.
Never pair the hub in your computer’s Bluetooth settings. Always connect from Pybricks Code. If someone already paired it in Windows or macOS settings, remove it there first, then connect from Pybricks Code.
Make your first file
Click the page button at the top left to open your files, then the + button to make a new file.
Click Create. The template writes the import lines for you, and the program starts with hub = PrimeHub().
print() appears and errors are shown, is at the bottom.The buttons along the top, left to right:
- Page — show or hide your files.
- Gear — settings, firmware and help.
- USB plug and Bluetooth — connect to the hub. The lessons use Bluetooth.
- Play — run this program (shortcut F5). It is sent to the hub and starts.
- Square — stop the program.
- >>> — type Python one line at a time in the terminal (the REPL). Useful for testing one sensor quickly.
On the hub itself:
- The centre button starts the last program, even with no computer connected. Press it again to stop.
- The hub has 5 program slots. Use the left and right buttons to choose one. A program you run from Pybricks Code is stored in that slot.
- To switch the hub off, hold the centre button for three seconds.
Anything else — backing up your programs, going back to the LEGO firmware, or a hub that will not connect — is in the Pybricks setup guide.
Blocks vs Python 10 min
Here is today’s program twice. On the left, the SPIKE word blocks you could build in the SPIKE App. On the right, the same program in Pybricks. Read them line by line, left then right.
SPIKE word blocks
when program starts :: events hat set Power Button light to [green v] :: light turn on [smile v] for (2) seconds :: light play beep (72) for (0.2) seconds :: sound write (3) :: light wait (1) seconds :: control write (2) :: light wait (1) seconds :: control write (1) :: light wait (1) seconds :: control write [GO] :: light
Pybricks Python
from pybricks.hubs import PrimeHub from pybricks.parameters import Color, Icon from pybricks.tools import wait hub = PrimeHub() hub.light.on(Color.GREEN) hub.display.icon(Icon.HAPPY) hub.speaker.beep(523, 200) wait(2000) hub.display.number(3) wait(1000) hub.display.number(2) wait(1000) hub.display.number(1) wait(1000) hub.display.text("GO")
| Block | Python | Watch out |
|---|---|---|
| when program starts | (the top of the file) | Python starts at the first line. No hat block is needed. |
| (nothing) | from … import … | Python only knows the words you bring in. Blocks bring them all in for you. |
| (nothing) | hub = PrimeHub() | Gives the hub a name, so you can talk to it. |
| set Power Button light to green | hub.light.on(Color.GREEN) | The Power Button is the hub's centre button. |
| turn on smile | hub.display.icon(Icon.HAPPY) | |
| play beep 72 for 0.2 seconds | hub.speaker.beep(523, 200) | SPIKE uses a note number. Pybricks uses hertz, and milliseconds. |
| wait 1 seconds | wait(1000) | Pybricks counts time in milliseconds. 1 second = 1000. |
| write 3 | hub.display.number(3) | Use text("GO") for words. |
Notice the dots. hub.light.on reads as “the hub’s light, turn on”. Every Pybricks line works like that: the thing, then what it does.
Write it from scratch 20 min
Make a new file in Pybricks Code called hello. Type it, do not paste it. Your fingers learn the shape of Python faster than your eyes do.
Build it in three steps, running after each one:
- The three import lines and
hub = PrimeHub(). Run it. Nothing happens, and that is correct: you have only said hello. - Add the light, the smile and the beep. Run it. Press the centre button on the hub to stop a program at any time.
- Add the countdown. Run it and watch the timing.
Then add one more line at the end and look at the terminal at the bottom of Pybricks Code:
Add at the end
print("Countdown done")
print sends words from the hub back to your screen. It does nothing on the robot, but it is the most useful tool in this level. From lesson 2 you will print sensor numbers, and from lesson 19 you will use it to hunt bugs.
Test and measure 10 min
Change one number at a time and write down what happened:
- Change
523to262, then to1047. What happens to the sound each time the number doubles? - Change the countdown waits from
1000to250. How fast can you make it before you cannot read the numbers? - Try
Color.RED,Color.BLUEandColor.ORANGE. TryIcon.SAD,Icon.HEARTandIcon.ARROW_UP. - Type
wait(1)instead ofwait(1000). What did the robot do? This is the most common Level 5 bug. Now you have seen it on purpose.
Break it, fix it 10 min
Each program below has one mistake. Type it in, run it and read the red message in the terminal. The message names the line and the kind of mistake. Fix it without asking anyone.
Bug 1
from pybricks.hubs import PrimeHub from pybricks.parameters import Color hub = primehub() hub.light.on(Color.GREEN)
Bug 2
from pybricks.hubs import PrimeHub from pybricks.parameters import Color hub = PrimeHub() hub.display.icon(Icon.HAPPY)
Bug 3
from pybricks.hubs import PrimeHub from pybricks.parameters import Color hub = PrimeHub() hub.light.on(Color.green)
Read the last line of the error first. NameError means Python has never heard of that word: a capital letter is wrong, or it was never imported. AttributeError means the thing exists, but not the part after the dot. Python cares about capital letters: GREEN and green are different words.
Solo challenge — no coach 10 min
No coach, no help. At WRO your coach may not touch the robot or the code. Close this page. You may use your own earlier programs and the Pybricks documentation, nothing else.
Launch sequence. Write a new program, launch, that:
- turns the light red and shows a sad face;
- counts down 5, 4, 3, 2, 1 with a beep on every number;
- makes the last beep higher than the others;
- turns the light green, shows a happy face and prints
Lift offin the terminal.
Done means: it runs first time in front of another team, and you can explain every line.
Engineering journal 5 min
Today’s entry:
- your hub’s name, and how to restore LEGO firmware
- the two units that differ from SPIKE: time in milliseconds, beeps in hertz
- each error you met, and what it meant
- one line of Python you can now write without looking