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

Robotics with LEGO SPIKE · Lesson 8 · 30 minutes

Moving to the real SPIKE

What you'll learn

  • Put Pybricks onto the SPIKE hub
  • Transfer programs from the simulator to the real robot
  • Measure sensor values before the first run instead of guessing

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

From the simulator to the table

Everything you’ve programmed here in the simulator also runs on the real LEGO SPIKE Prime hub – unchanged, because the simulator uses the same Pybricks commands as the book. But there are a few things you need to prepare once.

1. Put Pybricks on the hub (once)

Just like in the book (chapter 2.1):

  1. Open code.pybricks.com in Chrome or Edge – that’s the Pybricks web IDE.
  2. Connect the hub to the computer with a USB cable.
  3. Follow the web IDE’s instructions to install the Pybricks firmware. Your projects in the LEGO app are kept, and you can switch back at any time.
  4. Done – when you switch the hub on, it now shows the Pybricks logo.

Tip for schools: in Chrome/Edge you can also install Pybricks as an app (icon in the address bar). It then starts in its own window with a desktop icon.

2. Transfer a program

The routine is always the same (book, chapter 2.2):

  1. Connect the hub via USB or pair it via Bluetooth.
  2. In the web IDE, click the connect icon and choose the hub.
  3. Paste the code: click into the Codelix editor, press Ctrl+A (select all) and Ctrl+C (copy) – on a Mac ⌘ instead of Ctrl – then paste it into Pybricks with Ctrl+V.
  4. Press Play – the code is transferred and starts straight away.
  5. Watch the result and keep an eye on the console.
  6. Stop with the stop button in the web IDE or the centre button on the hub.

3. Checklist before the first run

  • 🔋 Battery charged? A weak battery makes motors slower – distances won’t be right any more.
  • 🔌 Ports correct? The code says Port.A, Port.B, Port.C – are the cables plugged in the same way?
  • ↔️ Direction? If the robot drives backwards instead of forwards, swap Direction.CLOCKWISE and COUNTERCLOCKWISE.
  • 📏 Calibrated? Real wheels are never exactly 56 mm – do the calibration from lesson 7.
  • 🧹 Space? A clear area, nothing breakable within reach, cable unplugged.

4. Measure instead of guessing

The biggest difference between the simulator and reality: light and surface. The line follower from lesson 4 assumes the sensor measures 50 at the edge of the line. On your table that might be 35 or 70! That’s why you measure first – and that’s exactly your task.

Try the book’s hello-hub program here once more – on the real hub the text then scrolls across the light matrix:

# 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

How to use the readings: measure on the white surface, on the black line and exactly at the edge. The value at the edge is your TARGET_BRIGHTNESS for the line follower. In the simulator you can even see the values as a curve – in the live chart below the field.

Your task

Your turn

Before the line follower drives on a real table, you should know which values your colour sensor measures there. Write a measuring program: it prints the reflection color_sensor.reflection() five times with print() and waits 500 milliseconds in between. The robot should stay where it is.

from pybricks.hubs import PrimeHub
from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port
from pybricks.tools import wait

hub = PrimeHub()

color_sensor = ColorSensor(Port.C)

# Measure the reflection five times here and print it

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.pupdevices import ColorSensor
from pybricks.parameters import Port
from pybricks.tools import wait

hub = PrimeHub()

color_sensor = ColorSensor(Port.C)

for measurement in range(5):
    print(color_sensor.reflection())
    wait(500)