Learn EV3 Python‎ > ‎

LEDs

Controlling the two LEDs located under the buttons of the EV3 brick must be one of the simplest things we can do in EV3 Python so let's start with that.

The official documentation is HERE. Note that the Leds class is a child of the Led class so you should look at the documentation for that too.

I think that all you really need to know as a beginner is how to set the color of each LED and how to make the LEDs blink. For other functions such as setting the brightness of each LED, see the official documentation.

Set the color of an LED

Use Leds.set_color(group, color)
Group can be Leds.LEFT or Leds.RIGHT.
Color can be Leds.RED, Leds.GREEN, Leds.YELLOW, Leds.ORANGE or Leds.AMBER.

Example
This example turns the left led red when the touch sensor button is pressed. When you run this or any other program, be aware that the program will take a few seconds to start. Press Ctrl-C to interrupt the infinite loop.

#!/usr/bin/env python3
# Above line is needed so program can be run from Brickman

# Plug a touch sensor into any sensor port
from ev3dev.ev3 import *
ts = TouchSensor()

while True:
    if ts.value()==1:   #touch sensor pressed
        Leds.set_color(Leds.LEFT, Leds.RED)
    else:
        Leds.set_color(Leds.LEFT, Leds.GREEN)

# Press Ctrl-C to exit

Here is the same example in a neater but more obscure form. Highlighted is a tuple with two elements. If ts.value() is zero then element zero of the tuple (green) will be used, etc.

#!/usr/bin/env python3
# So program can be run from Brickman

# Plug a touch sensor into any sensor port
from ev3dev.ev3 import *
ts = TouchSensor()

while True:
    Leds.set_color(Leds.LEFT, (Leds.GREEN, Leds.RED)[ts.value()])

# Press Ctrl-C to exit

Here is a similar example which will switch both LEDs at the same time:
Coming...

Make the LEDs blink
Here’s an LED blink demo.  Warning:  this currently does not work correctly for me if I choose colors other then RED or GREEN which are the true colors of the LEDs. The other colors, ORANGE, YELLOW and AMBER, are made by using the RED and GREEN LEDs together, and for me this sometimes causes the wrong color to appear or error messages to be triggered.
 
#!/usr/bin/env python3
from ev3dev.ev3 import *
Leds.set(Leds.LEFT, brightness_pct=0.5, trigger='timer')
Leds.set(Leds.LEFT, delay_on=3000, delay_off=500)
Leds.set_color(Leds.LEFT, Leds.GREEN)
 
This example uses the “Leds.set” method to set some properties on the left (“LEFT”) LED group. It sets the brightness to 50%, then it sets the trigger to “timer”, which means that it will be “triggered” on a set timer. “delay_off” and “delay_on” dictate the amount of time (in milliseconds) that the LED is off or on, respectively. With the numbers given here, it will stay off for 3 seconds and then turn on for half a second, then repeat. The trigger must be set to 'timer' before delay_off and delay_on are set, and in practice this means they must be set within separate commands.

Note that the settings will keep their values after the script stops running, so the LEDs will continue to blink until some other script stops them.

Turn off the LEDs
Turn off both LEDs with Leds.all_off()
Comments