EV3 Brick
- class EV3Brick
LEGO® MINDSTORMS® EV3 Brick.
Using the buttons
- buttons.pressed() set[Button]
Checks which buttons are currently pressed.
- Returns:
Set of pressed buttons.
Using the brick status light
- light.on(color)
Turns on the light at the specified color.
- Parameters:
color (Color) – Color of the light.
- light.off()
Turns off the light.
- light.blink(color, durations)
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.
- 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.
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.
- 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,
AtoGorRfor a rest.Note names can also include an accidental
#(sharp) orb(flat).B#/CbandE#/Fbare not allowed.The note name is followed by the octave number
2to8. For exampleC4is middle C. The octave changes to the next number at the note C, for example,B3is the note below middle C (C4).The octave is followed by
/and a number that indicates the size of the note. For example/4is a quarter note,/8is 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 orFont.DEFAULTif 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
Nonefor 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.DEFAULTwill 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.
- screen.set_font(font)
Sets the font used for writing on the screen.
The font is used for both
draw_text()andprint().- Parameters:
font (Font) – The font to use.
- screen.load_image(source)
Clears this image, then draws the
sourceimage centered in the screen.
- screen.draw_image(x, y, source, transparent=None)
Draws the
sourceimage on the screen.
- screen.draw_pixel(x, y, color=Color.BLACK)
Draws a single pixel on the screen.
- 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 withcolor, 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:
- screen.width = 178
- screen.height = 128
Using the battery
- battery.voltage() int: mV
Gets the voltage of the battery.
- Returns:
Battery voltage.
- 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 is0if the hub was previously powered off normally. It is1if the hub rebooted automatically, like after a firmware update. It is2if the hub previously crashed due to a watchdog timeout, which indicates a firmware issue."host_connected_ble":Trueif the hub is connected to a computer, tablet, or phone via Bluetooth, andFalseotherwise."host_connected_usb":Trueif the hub is connected to a computer via USB and activated in the app.Falseotherwise."program_start_type": It is1if the program started automatically when the hub was powered on. It is2if the program was started with the hub buttons. It is3if 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.
- 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. ChooseNoneto 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:
- 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.
- 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)