Hub Outputs

Pybrick Documentation

To explore all Pybricks’ features check the Pybricks documentaion. This can also be seen in the right-hand panel of the Pybricks IDE.

The Spike Prime Hub has three features that can be used to produce output for the robot:

  • Hub status light

  • The light matrix

  • A speaker

Output and Input

Imagine that you want the robot to explore a room all by itself. When the robot uses its sensors to see if there are obstacles ahead and decides to turn left, that’s the input. It’s like the robot figuring out what’s around it.

Then, when the robot actually turns and goes in the new direction, that’s the output. It’s the robot taking action based on what it learned from its sensors.

So, input is when the robot gathers information, and output is the robot using that information to make decisions and move around. It’s like the robot’s way of thinking and moving on its own!

The Hub Status Light

The Hub Status Light is around the Power button and it is blue by default.

Hub Status Light

Status Light Functions

Pybricks has four functions for the status light:

Status Light Example

You will now use all four status light functions:

  1. Create a new file called hub_light.py

  2. Type the code below into the file

  3. Predict what you think will happen.

  4. Run your code

 1# hub_light.py
 2
 3from pybricks.hubs import PrimeHub
 4from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
 5from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
 6from pybricks.robotics import DriveBase
 7from pybricks.tools import wait, StopWatch
 8
 9# --- SETUP
10hub = PrimeHub()
11
12
13# --- MAIN LOOP
14while True:
15    hub.light.on(Color.MAGENTA)
16    wait(1000)
17    hub.light.blink(Color.RED, [500,250,500,250])
18    wait(1500)
19    hub.light.animate([Color.GREEN, Color.WHITE, Color.ORANGE],1000)
20    wait(3000)
21    hub.light.off()
22    wait(1000)

Investigate

  • lines 3 - 7 → imports all the Pybricks command for use with your robot

  • lines 9 - 10 → creates a PrimeHub and names it hub

  • line 14 → creates an endless loop

  • line 15 → changes the status light to MAGENTA

  • line 16 → waits for 1000 milliseconds (ie. 1 second)

  • line 17 → blinks RED in the pattern on for 500ms, off 250ms, on 500ms, off 250ms

  • line 18 → waits for 1500 milliseconds

  • line 19 → moves through the three colours GREEN, WHITE then ORANGE, showing each for 1000ms

  • line 20 → waits for 3000 milliseconds

  • line 21 → turns the light off

  • line 16 → waits for 1000 milliseconds

Modify

  • can you display different colours?

  • can you change the timing of the blink?

  • can you change the timing of the animation?

  • what happens when you comment out (put a # in front) all the wait functions?

Light Matrix

The Spike Prime Hub has a five by five light matrix that can be used to display text and images, or individual pixels (light) can be accessed using their coordinates (see below).

light matrix

Light Matrix Functions

Pybricks has eight functions for the Light Matrix

Light Matrix Example

The code below uses all eight functions for the light matrix.

  1. Create a new file called light_matrix.py

  2. Type the code below into the file

  3. Predict what you think will happen.

  4. Run your code

 1# light_matrix.py
 2
 3from pybricks.hubs import PrimeHub
 4from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
 5from pybricks.parameters import Button, Color, Direction, Port, Side, Stop, Icon
 6from pybricks.robotics import DriveBase
 7from pybricks.tools import wait, StopWatch
 8
 9# --- SETUP
10
11hub = PrimeHub()
12
13
14# --- MAIN LOOP
15while True:
16    hub.display.orientation(Side.RIGHT)
17    hub.display.icon(Icon.UP)
18    wait(500)
19    hub.display.orientation(Side.BOTTOM)
20    hub.display.icon(Icon.UP)
21    wait(500)
22    hub.display.orientation(Side.LEFT)
23    hub.display.icon(Icon.UP)
24    wait(500)
25    hub.display.orientation(Side.TOP)
26    hub.display.icon(Icon.UP)
27    wait(500)
28
29    hub.display.off()
30    wait(1000)
31
32    hub.display.pixel(1,1,100)
33    hub.display.pixel(1,3,100)
34    hub.display.pixel(3,1,100)
35    hub.display.pixel(3,2,100)
36    hub.display.pixel(3,3,100)
37    wait(1000)
38
39    hub.display.off()
40    wait(1000)
41
42    arrows = [Icon.RIGHT, Icon.DOWN, Icon.LEFT, Icon.UP]
43    hub.display.animate(arrows, 500)
44    wait(2000)
45
46    hub.display.off()
47    wait(1000)
48
49    hub.display.char("R")
50    wait(500)
51    hub.display.number(2)
52    wait(500)
53    hub.display.char("D")
54    wait(500)
55    hub.display.number(2)
56    wait(500)
57
58    hub.display.off()
59    wait(1000)
60
61    hub.display.text("C3PO", 500, 50)
62
63    hub.display.off()
64    wait(2000)

Investigate

  • lines 3 - 7 → imports all the Pybricks command for use with your robot

    • make sure to add Icon to the end of line 5

  • lines 9 - 10 → creates a PrimeHub and names it hub

  • line 15 → creates an endless loop

  • lines 16 - 27 → changes which side of the hub is considered up and then displays the up arrow.

  • lines 29 - 30 → turns the display off for 1 second

  • lines 32 - 37 → turns on a number of pixels to show the meh face

  • lines 29 - 40 → turns the display off for 1 second

  • line 42 → makes an Icon list

  • line 43 → displays each icon in the icon list for 500ms

  • line 44 → waits for a second

  • lines 46 - 47 → turns the display off for 1 second

  • lines 49 - 56 → displays numbers and characters

  • lines 58 - 59 → turns the display off for 1 second

  • line 61 → displays the text C3PO

  • lines 63 - 54 → turns the display off for 2 seconds

Modify

  • can you make the Icon.HAPPY spin around the display?

  • can you change the meh face so the mouth uses the entire screen and the eyes are four pixels big?

  • what happens if you comment out all the wait functions?

Speaker

The Prime Hub has a speaker built in, which can be used to produce sounds.

Speaker Functions

The hub’s speaker has three functions:

Speaker Example

The example below uses the three speaker functions:

  1. Create a new file called light_matrix.py

  2. Type the code below into the file

  3. Predict what you think will happen.

  4. Run your code

 1# speaker.py
 2
 3from pybricks.hubs import PrimeHub
 4from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
 5from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
 6from pybricks.robotics import DriveBase
 7from pybricks.tools import wait, StopWatch
 8
 9# --- SETUP
10
11hub = PrimeHub()
12
13
14# --- MAIN LOOP
15while True:
16    
17    hub.speaker.volume(100)
18    current_volume = str(hub.speaker.volume())
19    hub.display.text(current_volume)
20    hub.speaker.beep(440,500)
21    
22    hub.speaker.volume(50)
23    current_volume = str(hub.speaker.volume())
24    hub.display.text(current_volume)
25    hub.speaker.beep(440,500)
26    
27    oh_when_the_saints = [
28        "C4/4", "E4/4", "F4/4", "G4/1",
29        "C4/4", "E4/4", "F4/4", "G4/1",
30        "C4/4", "E4/4", "F4/4", "G4/2",
31        "E4/2", "C4/2", "E4/2", "D4/1",
32        "E4/4", "E4/4", "D4/4", "C4/2",
33        "C4/2", "E4/2", "G4/4", "G4/4", "G4/4", "F4/1",
34        "E4/4", "F4/4", "G4/2", "E4/2", "C4/2", "D4/2", "C4/1"
35    ]
36
37    hub.speaker.play_notes(oh_when_the_saints,180)

Investigate

  • lines 3 - 7 → imports all the Pybricks command for use with your robot

  • lines 9 - 10 → creates a PrimeHub and names it hub

  • line 15 → creates an endless loop

  • line 17 → sets the speaker volume to 100%

  • lines 18 - 19 → read the current volume level and assigns it to current_volume then display it to the light matrix

  • line 20 → play a tone of 440Hz for 500ms

  • line 22 → sets the speaker volume to 50%

  • lines 23 - 24 → read the current volume level and assigns it to current_volume then display it to the light matrix

  • line 25 → play a tone of 440Hz for 500ms

  • lines 27 - 35 → a list containing the notes for Oh When The Saints

  • line 37 → play the notes in the oh_when_the_saints list at a tempo of 180bpm

Modify

  • seeing what is the lowest volume beep you can hear

  • seeing what is the lowest pitch beep you can hear

  • seeing what is the highest pitch beep you can hear

  • make the hub play a different tune