EV3 Brick

../_images/ev3device-ev3.png
ev3
class EV3Brick

LEGO® MINDSTORMS® EV3 Brick.

Using the buttons

ev3centeris pressed
buttons.pressed() → set[Button]

Checks which buttons are currently pressed.

Returns:

Set of pressed buttons.

Using the brick status light

redev3lighton
light.on(color)

Turns on the light at the specified color.

Parameters:

color (Color) – Color of the light.

ev3lightoff
light.off()

Turns off the light.

Blinks the light at a given color by turning it on and off for given durations.

The light keeps blinking indefinitely while the rest of your program keeps running.

This method provides a simple way to make basic but useful patterns. For more generic and multi-color patterns, use animate() instead.

Parameters:
  • color (Color) – Color of the light.

  • durations (list) – Sequence of time values of the form [on_1, off_1, on_2, off_2, ...].

light.animate(colors, interval)

Animates the light with a sequence of colors, shown one by one for the given interval.

The animation runs in the background while the rest of your program keeps running. When the animation completes, it repeats.

Parameters:
  • colors (list) – Sequence of Color values.

  • interval (Number, ms) – Time between color updates.

Using the speaker

speaker.volume(volume)
speaker.volume() → int: %

Gets or sets the speaker volume.

If no volume is given, this method returns the current volume.

Parameters:

volume (Number, %) – Volume of the speaker in the 0-100 range.

ev3500Hz100msbeep atfor
await speaker.beep(frequency=500, duration=100)

Play a beep/tone.

Parameters:
  • frequency (Number, Hz) – Frequency of the beep in the 64-24000 Hz range.

  • duration (Number, ms) – Duration of the beep. If the duration is less than 0, then the method returns immediately and the frequency play continues to play indefinitely.

await speaker.play_notes(notes, tempo=120)

Plays a sequence of musical notes. For example: ["C4/4", "C4/4", "G4/4", "G4/4"].

Each note is a string with the following format:

  • The first character is the name of the note, A to G or R for a rest.

  • Note names can also include an accidental # (sharp) or b (flat). B#/Cb and E#/Fb are not allowed.

  • The note name is followed by the octave number 2 to 8. For example C4 is middle C. The octave changes to the next number at the note C, for example, B3 is the note below middle C (C4).

  • The octave is followed by / and a number that indicates the size of the note. For example /4 is a quarter note, /8 is an eighth note and so on.

  • This can optionally followed by a . to make a dotted note. Dotted notes are 1-1/2 times as long as notes without a dot.

  • The note can optionally end with a _ which is a tie or a slur. This causes there to be no pause between this note and the next note.

Parameters:
  • notes (iter) – A sequence of notes to be played.

  • tempo (int) – Beats per minute. A quarter note is one beat.

Using the screen

screen.clear()

Clears the screen. All pixels on the screen will be set to Color.WHITE.

screen.draw_text(x, y, text, text_color=Color.BLACK, background_color=None)

Draws text on the screen.

The most recent font set using set_font() will be used or Font.DEFAULT if no font has been set yet.

Parameters:
  • x (int) – The x-axis value where the left side of the text will start.

  • y (int) – The y-axis value where the top of the text will start.

  • text (str) – The text to draw.

  • text_color (Color) – The color used for drawing the text.

  • background_color (Color) – The color used to fill the rectangle behind the text or None for transparent background.

screen.print(*args, sep=' ', end='\n')

Prints a line of text on the screen.

This method works like the builtin print() function, but it writes on the screen instead.

You can set the font using set_font(). If no font has been set, Font.DEFAULT will be used. The text is always printed used black text with a white background.

Unlike the builtin print(), the text does not wrap if it is too wide to fit on the screen. It just gets cut off. But if the text would go off of the bottom of the screen, the entire image is scrolled up and the text is printed in the new blank area at the bottom of the screen.

Parameters:
  • args (Any) – Zero or more objects to print.

  • sep (str) – Separator that will be placed between each object that is printed.

  • end (str) – End of line that will be printed after the last object.

screen.set_font(font)

Sets the font used for writing on the screen.

The font is used for both draw_text() and print().

Parameters:

font (Font) – The font to use.

screen.load_image(source)

Clears this image, then draws the source image centered in the screen.

Parameters:

source (Image) – The source Image.

screen.draw_image(x, y, source, transparent=None)

Draws the source image on the screen.

Parameters:
  • x (int) – The x-axis value where the left side of the image will start.

  • y (int) – The y-axis value where the top of the image will start.

  • source (Image) – The source Image.

  • transparent (Color) – The color of image to treat as transparent or None for no transparency.

screen.draw_pixel(x, y, color=Color.BLACK)

Draws a single pixel on the screen.

Parameters:
  • x (int) – The x coordinate of the pixel.

  • y (int) – The y coordinate of the pixel.

  • color (Color) – The color of the pixel.

screen.draw_line(x1, y1, x2, y2, width=1, color=Color.BLACK)

Draws a line on the screen.

Parameters:
  • x1 (int) – The x coordinate of the starting point of the line.

  • y1 (int) – The y coordinate of the starting point of the line.

  • x2 (int) – The x coordinate of the ending point of the line.

  • y2 (int) – The y coordinate of the ending point of the line.

  • width (int) – The width of the line in pixels.

  • color (Color) – The color of the line.

screen.draw_box(x1, y1, x2, y2, r=0, fill=False, color=Color.BLACK)

Draws a box on the screen.

Parameters:
  • x1 (int) – The x coordinate of the left side of the box.

  • y1 (int) – The y coordinate of the top of the box.

  • x2 (int) – The x coordinate of the right side of the box.

  • y2 (int) – The y coordinate of the bottom of the box.

  • r (int) – The radius of the corners of the box.

  • fill (bool) – If True, the box will be filled with color, otherwise only the outline of the box will be drawn.

  • color (Color) – The color of the box.

screen.draw_circle(x, y, r, fill=False, color=Color.BLACK)

Draws a circle on the screen.

Parameters:
  • x (int) – The x coordinate of the center of the circle.

  • y (int) – The y coordinate of the center of the circle.

  • r (int) – The radius of the circle.

  • fill (bool) – If True, the circle will be filled with color, otherwise only the circumference will be drawn.

  • color (Color) – The color of the circle.

screen.width = 178
screen.height = 128

Using the battery

ev3batteryvoltage
battery.voltage() → int: mV

Gets the voltage of the battery.

Returns:

Battery voltage.

ev3batterycurrent
battery.current() → int: mA

Gets the current supplied by the battery.

Returns:

Battery current.

System control

system.info() → dict

Gets information about the hub as a dictionary with the following keys:

  • "name": The hub name. This is the name you see when connecting via Bluetooth.

  • "reset_reason": Why the hub (re)booted. It is 0 if the hub was previously powered off normally. It is 1 if the hub rebooted automatically, like after a firmware update. It is 2 if the hub previously crashed due to a watchdog timeout, which indicates a firmware issue.

  • "host_connected_ble": True if the hub is connected to a computer, tablet, or phone via Bluetooth, and False otherwise.

  • "host_connected_usb": True if the hub is connected to a computer via USB and activated in the app. False otherwise.

  • "program_start_type": It is 1 if the program started automatically when the hub was powered on. It is 2 if the program was started with the hub buttons. It is 3 if the program was started from your connected computer.

  • “program_id”: Program (slot) number of the currently running program.

Returns:

A dictionary with system info.

Changed in version 3.6: The name and reset reason where previously available as separate methods. Now they are included in the info dictionary. The methods are still available for backwards compatibility.

ev3centerstop button is
ev3nonestop button is
system.set_stop_button(button)

Sets the button or button combination that stops a running script.

Normally, the center button is used to stop a running script. You can change or disable this behavior in order to use the button for other purposes.

Parameters:

button (Button) – A button such as Button.CENTER, or a tuple of multiple buttons. Choose None to disable the stop button altogether. If you do, you can still turn the hub off by holding the center button for three seconds.

system.storage(offset, write)
system.storage(offset, read) → bytes

Reads or writes binary data to persistent storage.

This lets you store data that can be used the next time you run the program.

The data will be saved to flash memory when you turn the hub off normally. It will not be saved if the batteries are removed while the hub is still running.

Once saved, the data will remain available even after you remove the batteries.

Parameters:
  • offset (int) – The offset from the start of the user storage memory, in bytes.

  • read (int) – The number of bytes to read. Omit this argument when writing.

  • write (bytes) – The bytes to write. Omit this argument when reading.

Returns:

The bytes read if reading, otherwise None.

Raises:

ValueError – If you try to read or write data outside of the allowed range.

system.reset_storage()

Resets all user settings to default values and erases user programs.

ev3shut down
system.shutdown()

Stops your program and shuts the hub down.

Status light examples

Turn the light on and change the color

from pybricks.hubs import EV3Brick
from pybricks.parameters import Color
from pybricks.tools import wait

# Initialize the EV3
ev3 = EV3Brick()

# Turn on a red light
ev3.light.on(Color.RED)

# Wait
wait(1000)

# Turn the light off
ev3.light.off()

Screen examples

Drawing shapes on the screen

from pybricks.hubs import EV3Brick
from pybricks.tools import wait

# Initialize the EV3
ev3 = EV3Brick()


# Draw a rectangle
ev3.screen.draw_box(10, 10, 40, 40)

# Draw a solid rectangle
ev3.screen.draw_box(20, 20, 30, 30, fill=True)

# Draw a rectangle with rounded corners
ev3.screen.draw_box(50, 10, 80, 40, 5)

# Draw a circle
ev3.screen.draw_circle(25, 75, 20)

# Draw a triangle using lines
x1, y1 = 65, 55
x2, y2 = 50, 95
x3, y3 = 80, 95
ev3.screen.draw_line(x1, y1, x2, y2)
ev3.screen.draw_line(x2, y2, x3, y3)
ev3.screen.draw_line(x3, y3, x1, y1)

# Wait some time to look at the shapes
wait(5000)