Examples

Hub Outputs

Status Light

 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)

Light Matrix

 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)

Speaker

 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)

Motor Output

Motors Running Forever

 1# motors_run_forever.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()
11left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
12right_motor = Motor(Port.B, Direction.CLOCKWISE)
13
14# --- MAIN LOOP
15while True:
16    left_motor.run(1000)
17    right_motor.run(1000)
18    wait(1000)
19
20    left_motor.dc(50)
21    right_motor.dc(50)
22    wait(1000)

Motors Stopping

 1# motor_stop.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()
11left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
12right_motor = Motor(Port.B, Direction.CLOCKWISE)
13
14# --- MAIN LOOP
15while True:
16    hub.light.on(Color.GREEN)
17
18    left_motor.dc(100)
19    right_motor.dc(100)
20    wait(1000)
21    
22    left_motor.stop()
23    right_motor.stop()
24    wait(500)
25
26    hub.light.on(Color.ORANGE)
27
28    left_motor.dc(100)
29    right_motor.dc(100)
30    wait(1000)
31    
32    left_motor.brake()
33    right_motor.brake()
34    wait(500)
35
36    hub.light.on(Color.VIOLET)
37
38    left_motor.dc(100)
39    right_motor.dc(100)
40    wait(1000)
41    
42    left_motor.hold()
43    right_motor.hold()
44    wait(500)

Motor Fixed Amount

 1# motors_run_fixed.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()
11left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
12right_motor = Motor(Port.B, Direction.CLOCKWISE)
13
14# --- MAIN LOOP
15while True:
16    hub.light.on(Color.BROWN)    
17    
18    left_motor.run_time(1000, 1000, Stop.COAST)
19    right_motor.run_time(1000, 1000, Stop.COAST)
20
21    hub.light.on(Color.VIOLET)
22
23    left_motor.run_angle(1000, 180)
24    right_motor.run_angle(1000, 180)
25
26    hub.light.on(Color.CYAN)
27
28    left_motor.reset_angle(0)
29    right_motor.reset_angle(0)
30    left_motor.run_target(1000, 180)
31    right_motor.run_target(1000, 180)
32
33    hub.light.on(Color.GRAY)
34
35    left_motor.reset_angle(0)
36    right_motor.reset_angle(0)
37    left_motor.track_target(180)
38    right_motor.track_target(180)
39
40    wait(1000)

Drive Base

Drive Base Setup

 1# drive_base.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# create drive base
13left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
14right_motor = Motor(Port.B, Direction.CLOCKWISE)
15my_robot = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=80)

Driving Forever

 1# drive_base_forever.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# create drive base
13left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
14right_motor = Motor(Port.B, Direction.CLOCKWISE)
15my_robot = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=80)
16
17# --- MAINLOOP
18while True:
19    hub.light.on(Color.WHITE)
20    my_robot.drive(1000, 0)
21    wait(500)
22
23    hub.light.on(Color.RED)
24    my_robot.drive(1000, -300)
25    wait(500)
26
27    my_robot.stop()
28    wait(1000)
29
30    hub.light.on(Color.WHITE)
31    my_robot.drive(1000, 0)
32    wait(500)
33
34    hub.light.on(Color.GREEN)
35    my_robot.drive(1000, 300)
36    wait(500)
37
38    my_robot.stop()
39    wait(1000)

Driving Fixed

 1# drive_base_fixed.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# create drive base
13left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
14right_motor = Motor(Port.B, Direction.CLOCKWISE)
15my_robot = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=80)
16
17# - MAIN LOOP
18while True:
19    hub.light.on(Color.YELLOW)
20    my_robot.settings(200, 200, 200, 200)
21    print(my_robot.settings())
22    my_robot.straight(200)
23    my_robot.turn(90)
24    my_robot.curve(100,180)
25
26    hub.light.on(Color.MAGENTA)
27    my_robot.settings(500, 500, 500, 500)
28    print(my_robot.settings())
29    my_robot.straight(200)
30    my_robot.turn(90)
31    my_robot.curve(100,180)

Hub Inputs

Buttons

 1# buttons.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
10hub = PrimeHub()
11
12
13# --- MAIN LOOP
14while True:
15    pressed = hub.buttons.pressed()
16    
17    if Button.LEFT in pressed:
18        hub.display.icon(Icon.ARROW_LEFT)
19    elif Button.RIGHT in pressed:
20        hub.display.icon(Icon.ARROW_RIGHT)
21    elif Button.CENTER in pressed:
22        hub.display.icon(Icon.CIRCLE)
23    elif Button.BLUETOOTH in pressed:
24        hub.display.char("B")
25    else:
26        hub.display.off()

IMU Orientation

 1# imu_orientation
 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# --- MAIN LOOP
13while True:
14    up  = hub.imu.up()
15    pitch, roll = hub.imu.tilt()
16
17    print(up, "\t", pitch, "\t", roll)

Motor as Sensor

Motor Measurement Example

 1# motor_inputs.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()
11left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
12
13left_motor.reset_angle(0)
14
15# --- MAIN LOOP
16while True:
17    pressed = hub.buttons.pressed()
18
19    if Button.LEFT in pressed and Button.RIGHT in pressed:
20        print("Angel:", left_motor.angle())
21        left_motor.run_target(1000, 0)
22        print("Angel:", left_motor.angle())
23    
24    elif Button.LEFT in pressed:
25        left_motor.run(300)
26        wait(250)
27        print("Speed:", left_motor.speed())
28        wait(500)
29        left_motor.stop()
30    
31    elif Button.RIGHT in pressed:
32        left_motor.run(300)
33        if left_motor.stalled():
34            print("Stalled")
35        else:
36            print("Torque load:", left_motor.load())
37    
38    else:
39        left_motor.stop()

Drive Base as Sensor

Drive Base Measurement Example

 1# drive_base_inputs.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
12left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE)
13right_motor = Motor(Port.B, Direction.CLOCKWISE)
14my_robot = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=80)
15
16my_robot.reset()
17
18# --- MAIN LOOP
19while True:
20    pressed = hub.buttons.pressed()
21    
22    if Button.LEFT in pressed:
23        distance, speed, angle, rate_of_turn = my_robot.state()
24        print("Start - distance:", distance, "speed:", speed, "angle:", angle, "rate of turn:", rate_of_turn)
25        my_robot.curve(100, 180)
26        distance, speed, angle, rate_of_turn = my_robot.state()
27        print("Start - distance:", distance, "speed:", speed, "angle:", angle, "rate of turn:", rate_of_turn)
28
29    elif Button.RIGHT in pressed:
30        distance = my_robot.distance()
31        print("Start distance:", distance)
32        my_robot.straight(500)
33        distance = my_robot.distance()
34        print("End distance:", distance)
35
36    elif Button.BLUETOOTH in pressed:
37        angle = my_robot.angle()
38        print("Start angle:", angle)
39        my_robot.turn(-90)
40        angle = my_robot.angle()
41        print("End angle:", angle)
42
43    else:
44        my_robot.stop()

Force Sensor

Force Sensor Example

 1# force_sensor.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()
11force_sensor = ForceSensor(Port.F)
12
13# --- MAIN LOOP
14while True:
15    force_reading = round(force_sensor.force(), 1)
16    distance_reading = round(force_sensor.distance(), 1)
17    is_pressed = force_sensor.pressed(3)
18    is_touched = force_sensor.touched()
19
20    print(
21        "Force:", force_reading,
22        "\tDistance:", distance_reading,
23        "\tForce > 3:", is_pressed,
24        "\tTouched:", is_touched
25    )

Color Sensor

Color Sensor Example

 1# color_sensor
 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()
11color_sensor = ColorSensor(Port.D)
12modes = ["S", "L", "R", "A"]
13mode = 0
14
15# --- MAIN LOOP
16while True:
17    presses = hub.buttons.pressed()
18
19    if Button.LEFT in presses and mode > 0:
20        mode -= 1
21        wait(250)
22    elif Button.RIGHT in presses and mode < 3:
23        mode += 1
24        wait(250)
25
26    hub.display.char(modes[mode])
27    
28    if mode == 0:
29        print(color_sensor.color())
30    elif mode == 1:
31        print(color_sensor.color(False))
32    elif mode == 2:
33        print(color_sensor.reflection())
34    elif mode == 3:
35        print(color_sensor.ambient())

Distance Sensor

Distance Sensor Example

 1# distance_sensor.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()
11distance_sensor = UltrasonicSensor(Port.C)
12
13# --- MAIN LOOP
14while True:
15    distance = distance_sensor.distance()
16    present = distance_sensor.presence()
17
18    print("Distance:", distance, "\tDetected other sensor:", present)
19
20    distance_sensor.lights.off()
21    wait(100)
22    distance_sensor.lights.on(100)