Remote Control

../_images/pupdevice-remote.png
class Remote(name=None, timeout=10000)

LEGO® Powered Up Bluetooth Remote Control.

When you instantiate this class, the hub will search for a remote and connect automatically.

The remote must be on and ready for a connection, as indicated by a white blinking light.

Parameters
  • name (str) – Bluetooth name of the remote. If no name is given, the hub connects to the first remote that it finds.

  • timeout (Number, ms) – How long to search for the remote.

name(name)
name() str

Sets or gets the Bluetooth name of the remote.

Parameters

name (str) – New Bluetooth name of the remote. If no name is given, this method returns the current name.

light.on(color)

Turns on the light at the specified color.

Parameters

color (Color) – Color of the light.

light.off()

Turns off the light.

buttons.pressed() Collection[Button]

Checks which buttons are currently pressed.

Returns

Set of pressed buttons.

Examples

Checking which buttons are pressed

from pybricks.pupdevices import Remote
from pybricks.parameters import Button
from pybricks.tools import wait

# Connect to the remote.
my_remote = Remote()

while True:
    # Check which buttons are pressed.
    pressed = my_remote.buttons.pressed()

    # Show the result.
    print("pressed:", pressed)

    # Check a specific button.
    if Button.CENTER in pressed:
        print("You pressed the center button!")

    # Wait so we can see the result.
    wait(100)

Changing the remote light color

from pybricks.pupdevices import Remote
from pybricks.parameters import Color
from pybricks.tools import wait

# Connect to the remote.
remote = Remote()

while True:
    # Set the color to red.
    remote.light.on(Color.RED)
    wait(1000)

    # Set the color to blue.
    remote.light.on(Color.BLUE)
    wait(1000)

Changing the light color using the buttons

from pybricks.pupdevices import Remote
from pybricks.parameters import Button, Color


def button_to_color(buttons):

    # Return a color depending on the button.
    if Button.LEFT_PLUS in buttons:
        return Color.RED
    if Button.LEFT_MINUS in buttons:
        return Color.GREEN
    if Button.LEFT in buttons:
        return Color.ORANGE
    if Button.RIGHT_PLUS in buttons:
        return Color.BLUE
    if Button.RIGHT_MINUS in buttons:
        return Color.YELLOW
    if Button.RIGHT in buttons:
        return Color.CYAN
    if Button.CENTER in buttons:
        return Color.VIOLET

    # Return no color by default.
    return Color.NONE


# Connect to the remote.
remote = Remote()

while True:
    # Wait until a button is pressed.
    pressed = ()
    while not pressed:
        pressed = remote.buttons.pressed()

    # Convert button code to color.
    color = button_to_color(pressed)

    # Set the remote light color.
    remote.light.on(color)

    # Wait until all buttons are released.
    while pressed:
        pressed = remote.buttons.pressed()

Using the timeout setting

You can use the timeout argument to change for how long the hub searches for the remote. If you choose None, it will search forever.

from pybricks.pupdevices import Remote

# Connect to any remote. Search forever until we find one.
my_remote = Remote(timeout=None)

print("Connected!")

If the remote was not found within the specified timeout, an OSError is raised. You can catch this exception to run other code if the remote is not available.

from pybricks.pupdevices import Remote

try:
    # Search for a remote for 5 seconds.
    my_remote = Remote(timeout=5000)

    print("Connected!")

    # Here you can write code that uses the remote.

except OSError:

    print("Could not find the remote.")

    # Here you can make your robot do something
    # without the remote.

Changing the name of the remote

You can change the Bluetooth name of the remote. The factory default name is Handset.

from pybricks.pupdevices import Remote

# Connect to any remote.
my_remote = Remote()

# Print the current name of the remote.
print(my_remote.name())

# Choose a new name.
my_remote.name("truck2")

print("Done!")

You can specify this name when connecting to the remote. This lets you pick the right one if multiple remotes are nearby.

from pybricks.pupdevices import Remote
from pybricks.tools import wait

# Connect to a remote called truck2.
truck_remote = Remote("truck2", timeout=None)

print("Connected!")

wait(2000)