Learn EV3 Python‎ > ‎

LEDs

The EV3 has two pairs of LEDs underneath the buttons. On both the left side and the right side there is both a red LED and green LED. By turning on both these colors in different ratios, other colors can obtained such as yellow, amber and orange. Allowable named colors are 'RED', 'GREEN', 'YELLOW', 'ORANGE, 'AMBER', 'BLACK'. Setting an LED to black is the same as turning it off, of course, but there is a special command for turning off all the LEDs.

Set the color of an LED pair

It is also supposed to be possible to create custom colors by specifying a percentage of the maximum brightness for the red and green LEDs in that order. For example, 
leds.set_color('LEFT', (1, 0.01)) # red: 100% brightness, green: 1%
should set the left pair of LEDs to be almost pure bright red, but very slightly orange. But as of September 2018  this is not working correctly and non-zero values are always treated as 1. 

It is important to turn off all the LEDs before starting to set colors because otherwise the flashing from the standard flashing green will interfere with the LEDs.

#!/usr/bin/env python3
from ev3dev2.led import Leds
from time import sleep

leds = Leds()

leds.all_off() # Turn all LEDs off
sleep(1)

# Set both pairs of LEDs to amber
leds.set_color('LEFT', 'AMBER')
leds.set_color('RIGHT', 'AMBER')
sleep(4)

# With custom colors:
leds.set_color('LEFT', (1, 0)) # Bright Red.
leds.set_color('RIGHT', (0, 1)) # Bright green.
sleep(4)

leds.set_color('LEFT', (1, 0.01)) # Does not work correctly
leds.set_color('RIGHT', (0.01, 1)) # Does not work correctly
sleep(4)

Another Example
This example turns the left led pair red when the touch sensor button is pressed. 

#!/usr/bin/env python3
from ev3dev2.led import Leds
from ev3dev2.sensor.lego import TouchSensor
from time import sleep

leds = Leds()
ts = TouchSensor()

while True:
    if ts.is_pressed:   #touch sensor pressed
        leds.set_color('LEFT', 'RED')
    else:
        leds.set_color('LEFT', 'GREEN')
    sleep(0.1)  # Give the CPU a rest


Turn off the LEDs
Turn off all the LEDs with all_off()This also turns off the flashing.

Make the LEDs blink
It is theoretically possible to make the LEDs blink but it is not trivial so will not be explained here. See the official documentation for more information.

Official documentation for the LED functions is HERE.
Comments