ev3brick
– The EV3 Programmable Brick¶
LEGO® MINDSTORMS® EV3 Brick.
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¶
Sound¶
-
classmethod
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)
-
classmethod
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)
-
classmethod
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)
-
classmethod
display.
clear
()¶ Clear everything on the display.
-
classmethod
display.
text
(text, coordinate=None)¶ Display text.
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")
-
classmethod
display.
image
(file_name, alignment=Align.CENTER, coordinate=None, clear=True)¶ Show an image file.
You can specify its placement either using
alignment
or by specifying acoordinate
, 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¶
-
classmethod
battery.
voltage
()¶ Get the voltage of the battery.
Returns: Battery voltage. Return type: voltage: mV Examples:
# Play a warning sound when the battery voltage # is below 7 Volt (7000 mV = 7V) if brick.battery.voltage() < 7000: brick.sound.beep()
-
classmethod
battery.
current
()¶ Get the current supplied by the battery.
Returns: Battery current. Return type: current: mA