ev3brick – The EV3 Programmable Brick

Buttons

ev3brick.buttons()

Check which buttons on the EV3 Brick are currently pressed.

Returns

List of pressed buttons.

Return type

List of Button

Examples:

# Do something if the left button is pressed
if Button.LEFT in brick.buttons():
    print("The left button is pressed.")
# Wait until any of the buttons are pressed
while not any(brick.buttons()):
    wait(10)

# Wait until all buttons are released
while any(brick.buttons()):
    wait(10)

Light

ev3brick.light(color)

Play a beep/tone

Parameters

color (Color) – Color of the light. Choose Color.BLACK or None to turn the light off.

Example:

# Make the light red
brick.light(Color.RED)

# Turn the light off
brick.light(None)

Sound

static sound.beep(frequency=500, duration=100, volume=30)

Play a beep/tone

Parameters
  • frequency (frequency: Hz) – Frequency of the beep (Default: 500).

  • duration (time: ms) – Duration of the beep (Default: 100).

  • volume (percentage: %) – Volume of the beep (Default: 30).

Example:

# A simple beep
brick.sound.beep()

# A high pitch (1500 Hz) for one second (1000 ms) at 50% volume
brick.sound.beep(1500, 1000, 50)
static sound.beeps(number)

Play a number of default beeps with a brief pause in between.

Parameters

number (int) – Number of beeps.

Example:

# Make 5 simple beeps
brick.sound.beeps(5)
static sound.file(file_name, volume=100)

Play a sound file.

Parameters
  • file_name (str) – Path to the sound file, including extension.

  • volume (percentage: %) – Volume of the sound (Default: 100).

Example:

# Play one of the built-in sounds
brick.sound.file(SoundFile.HELLO)

# Play a sound file from your project folder
brick.sound.file('mysound.wav')

Display

                x
            ------------>
(0, 0)   __________________
        |                  |
    |   |                  |
y   |   |      Hello       |
    |   |      World       |
    v   |                  |
        |__________________|
                            (177, 127)
static display.clear()

Clear everything on the display.

static display.text(text, coordinate=None)

Play a sound file.

Parameters
  • text (str) – The text to display.

  • coordinate (tuple) – (x, y) coordinate tuple. It is the top-left corner of the first character. If no coordinate is specified, it is printed on the next line.

Example:

# Clear the display
brick.display.clear()

# Print ``Hello`` near the middle of the screen
brick.display.text("Hello", (60, 50))

# Print ``World`` directly underneath it
brick.display.text("World")
static display.image(file_name, alignment=<Align.CENTER: 'c'>, coordinate=None, clear=True)

Show an image file.

You can specify its placement either using alignment or by specifying a coordinate, but not both.

Parameters
  • file_name (str) – Path to the image file. Paths may be absolute or relative from the project folder.

  • alignment (Align) – Where to place the image (Default: Align.CENTER).

  • coordinate (tuple) – (x, y) coordinate tuple. It is the top-left corner of the image (Default: None).

  • clear (bool) – Whether to clear the screen before showing the image (Default: True).

Example:

# Show a built-in image of two eyes looking upward
brick.display.image(ImageFile.UP)

# Display a custom image from your project folder
brick.display.image('pybricks.png')

# Display a custom image at the top right of the screen, without clearing
# the screen first
brick.display.image('arrow.png', Align.TOP_RIGHT, clear=False)

Battery

static battery.voltage()

Get the voltage of the battery.

Returns

Battery voltage.

Return type

voltage: mV

Example:

# Play a warning sound when the battery voltage
# is below 7 Volt (7000 mV = 7V)
if brick.battery.voltage() < 7000:
    brick.sound.beep()
static battery.current()

Get the current supplied by the battery.

Returns

Battery current.

Return type

current: mA