EV3 Brick

This section shows how to program the buttons, lights, sounds and display.

Buttons

This program associates 3 button presses with 3 different colors:

  • LEFT - green
  • CENTER - yellow
  • RIGHT - red
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import Button, Color

while True:
    b = brick.buttons()
    if Button.LEFT in b:
        brick.light(Color.GREEN)

    elif Button.CENTER in b:
        brick.light(Color.YELLOW)

    elif Button.RIGHT in b:
        brick.light(Color.RED)

    else:
        brick.light(None)

Sound

This program uses the 4 buttons to change volume and frequency.

  • LEFT/RIGHT - change volume from 0 to 100 in increments of 10
  • UP/DOWN - change frequency in increments of 10
#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import Button

# up/down buttons to change frequency
freq = 500
volume = 30

while True:
    buttons = brick.buttons()
    if Button.UP in buttons:
        freq += 10

    elif Button.DOWN in buttons:
        freq -= 10
    
    elif Button.LEFT in buttons:
        volume = max(0, volume-10)
    
    elif Button.RIGHT in buttons:
        volume = min(100, volume+10)

    brick.sound.beep(freq, 300, volume)

This program places a couple of sound files into two lists:

  • emotions
  • numbers

Inside a loop they are played in sequence.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import SoundFile

emotions = ['SHOUTING', 'CHEERING', 'CRYING']
numbers = 'ZERO ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE'.split()

for sound in emotions:
    file = eval('SoundFile.'+sound)
    brick.sound.file(file, 100)

for sound in numbers:
    file = eval('SoundFile.'+sound)
    brick.sound.file(file, 100)

Display

This program displays image files and their name on the screen, during 1 second.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import ImageFile
from pybricks.tools import print, wait

images = 'RIGHT FORWARD ACCEPT QUESTION_MARK STOP_1 LEFT DECLINE \
    THUMBS_DOWN BACKWARD NO_GO WARNING STOP_2 THUMBS_UP'.split()

for image in images:
    brick.display.clear()
    brick.display.text(image, (10, 10))
    file = eval('ImageFile.'+image)
    brick.display.image(file, clear=False)
    wait(1000)

This program writes a new line of text to screen, every second.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.parameters import (Port, Stop, Direction, Button, Color,
                                 SoundFile, ImageFile, Align)
from pybricks.tools import print, wait

images = 'RIGHT FORWARD ACCEPT QUESTION_MARK STOP_1 LEFT DECLINE \
    THUMBS_DOWN BACKWARD NO_GO WARNING STOP_2 THUMBS_UP'.split()

for image in images:
    brick.display.text(image.lower())
    wait(1000)

Battery

This program displays the battery voltage and current during 5 seconds.

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.tools import print, wait

voltage = brick.battery.voltage()
current = brick.battery.current()

brick.display.text('voltage = {} mV'.format(voltage))
brick.display.text('current= {} mA'.format(current))

wait(5000)