Pybricks provides a programmable interface directly from a browser code.pybricks.com, which requires neither installation nor registration. The interface assists in creating a starter program, but we won’t delve into that now.

Changing the core

To use Pybricks you need to change the core hub program, called firmware that sits between and connects the hardware pieces to your user program.

This is an easy 2-3 minute process even at your first go, so there is nothing to worry about, just make sure you pay attention to the instructions.

Firmware is programming that’s written to a hub hardware device’s permanent memory. This memory is where the content is saved when the hub is turned off or the battery is removed or depleted. Pybricks serves as firmware or even as an operating system much like Windows, Linux, or MacOS on your laptop. You can find awesome tutorials on firmware and operating systems over the internet like this or that.

For changing the firmware on your hub follow the install-pybricks tutorial (2-3 mins).
Note: You will need to use the Device Manager on Windows.
Note: You can easily get back to the official LEGO firmware later on.

Creating your first program

Step-by-step:

  1. Navigate to the code.pybricks.com site
  2. Create a new file using the plus sign
  3. Select “python” as program type
  4. Keep the “use template” switch on
  5. Select your hub - SPIKE and Mindstorms Robot Inventor hubs are practically the same
from pybricks.hubs import InventorHub
from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch

hub = InventorHub()

How to use Python

Python is suitable for writing quite complex programs, but our focus has been on the simplest usage. In the Pybricks environment, we program in Python, and the microcontroller at the heart of the hub communicates with us primarily in Python (MicroPython).

To make the hub understand our intentions the creators of Pybricks (Laurens Valk and David Lechner) categorized the available features into groups, much like organizing our drawings by theme into boxes. These groups are structured around physically tangible devices: hub, motor, color sensor, etc. Within each group, there are further subdivisions for easier management, separated by a dot (e.g., hub.display). Finally, we typically invoke a specific function, also separated by a dot and always ending with parentheses.

  1. Find the PrimeHub .display.icon() in the documentation
  2. Use Icon.HAPPY as a parameter of the call
  3. Replace the icon with another one
  4. Using the wait() function display 3 different images with a 3 second delay.\
    NOTE: wait(1000) delays 1000 milliseconds, that is 1 second.
  5. Clear the display after showing the last image
hub.display.icon(Icon.HAPPY)
wait(3000)
hub.display.icon(Icon.DOWN)
wait(3000)
hub.display.icon(Icon.ARROW_UP)
wait(3000)
hub.display.off()

tutorial_display

Playing with the hub

You can further play around with the key functions of the hub below hub.buttons., hub.lights., hub.speaker..

hub.system.beep()                           # hub beeps once.

Function calls can be more complex when we aim to achieve similar but not identical functionality. Here, we provide parameters within the parentheses to specify our intentions.

hub.display.number(42)                      # hub displays number 42
hub.display.number(99)                      # hub displays number 99
hub.display.text("Hello FLL, Hello WRO")    # hub displays this text
hub.display.icon(Icon.HAPPY)                # display a happy icon

Debugging

Sometimes you want to know what your program is doing, where its internal status is. At the moment you cannot stop the program and do a step-by-step execution that we would call debugging, yet you can print information as your code runs.

You can either write a character or a text to the pixel display.

hub.display.char("A")                       # single character
hub.display.text("Hello world")             # NOTE: this will take some time
hub.display.number(42)                      # numbers from -99 to +99

You can also print any information to the lower terminal area in your Pybricks IDE. Using print helps to track your current values and execution details.

print("A")                                  # prints a single character in a line
print("Hello world")                        # this will print instantaneously
print(42)                                   # you can print numbers as well
print("A", "Hello world", 42)               # combine multiple items to print
print(str(42)+"Hello")                      # combine strings
print(chr(42), "Hello star", ord("*"))      # ASCII code of a number

NEXT: Simple motors »


Copyright © 2024 Attila Farago.

Pybricks Competition Tutorial - using Pybricks framework in FIRST LEGO League and World Robot Olympiad competitions.