Skip to content
BetaDE
Lesson 1 of 8Robotics with LEGO SPIKE

Robotics with LEGO SPIKE · Lesson 1 · 20 minutes

Your first Pybricks program

What you'll learn

  • Import Pybricks building blocks
  • Control the hub with scrolling text, light and icons
  • Add pauses with wait()

The code in this lesson comes from the book “Python for Robots”, chapters 2.1 and 2.3, and runs unchanged in the simulator. German edition on Amazon ↗

Python on a robot

From now on your Python code doesn’t just run on the screen – it controls a robot. On the LEGO SPIKE Prime hub you use Pybricks for that: real Python with ready-made building blocks for motors and sensors.

You try everything in the simulator first: on the right you see the virtual hub with its 5×5 light matrix, on the left the robot from above. Press Run:

# First we import the building blocks we need.
# Pybricks is split into individual modules - this keeps the code tidy.

from pybricks.hubs import PrimeHub          # The SPIKE hub itself
from pybricks.parameters import Color      # Ready-made color values, e.g. Color.GREEN
from pybricks.tools import wait            # For pausing the program

# The hub is created ONCE - after that we control it via "hub"
hub = PrimeHub()

# Text scrolls across the hub's 5x5 LED matrix
hub.display.text("Hi!")

# Briefly light the hub up green
hub.light.on(Color.GREEN)

# Wait 2000 milliseconds = 2 seconds
wait(2000)

print("Program finished.")   # Appears in the Web IDE's console window

The first three lines import building blocks: PrimeHub is the hub itself, Color contains ready-made colours and wait makes the program pause. hub = PrimeHub() creates the hub object once – after that you control everything through hub. (remember classes and objects?).

Remember: wait(2000) waits 2000 milliseconds, so 2 seconds. Pybricks always counts time in milliseconds.

The 5×5 light matrix

The display can scroll text, show ready-made icons or your own patterns:

from pybricks.hubs import PrimeHub
from pybricks.parameters import Icon
from pybricks.tools import wait

hub = PrimeHub()

# Send text scrolling across the matrix
hub.display.text("Hi!")
wait(1500)

# Show ready-made icons from the Pybricks library
hub.display.icon(Icon.HAPPY)
wait(1500)

hub.display.icon(Icon.HEART)
wait(1500)

# Clear the display again (turn off all points of light)
hub.display.off()

Other ready-made icons are, for example, Icon.SAD, Icon.SQUARE, Icon.UP or Icon.ARROW_RIGHT_UP.

Tip: The centre button of the hub is the stop button in the simulator too – just like on the real robot.

Your task

Your turn

Exercise 2.1 from the book: change the program so that the hub first scrolls your dream robot name, then lights up in your favourite colour (try Color.RED, Color.BLUE, Color.YELLOW, Color.ORANGE) and finally prints Ready!.

# First we import the building blocks we need.
# Pybricks is split into individual modules - this keeps the code tidy.

from pybricks.hubs import PrimeHub          # The SPIKE hub itself
from pybricks.parameters import Color      # Ready-made color values, e.g. Color.GREEN
from pybricks.tools import wait            # For pausing the program

# The hub is created ONCE - after that we control it via "hub"
hub = PrimeHub()

# Text scrolls across the hub's 5x5 LED matrix
hub.display.text("Hi!")

# Briefly light the hub up green
hub.light.on(Color.GREEN)

# Wait 2000 milliseconds = 2 seconds
wait(2000)

print("Program finished.")   # Appears in the Web IDE's console window

Solve it without hints to earn a ⭐ on your progress page.

Show solution

Try it yourself first – mistakes teach you the most. Here is one possible solution:

from pybricks.hubs import PrimeHub
from pybricks.parameters import Color
from pybricks.tools import wait

hub = PrimeHub()

hub.display.text("Compass")
hub.light.on(Color.BLUE)
wait(2000)
print("Ready!")