Learn EV3 Python‎ > ‎

LEDs

UPDATE SEPTEMBER 2017: Although controlling the LEDs (plural, since EV3 Python controls the left and right LEDs separately) should be simple and straightforward, I have actually experienced significant frustration with this. My experiments lead me to think that errors may occur if an LED operation does not have time to complete before another is attempted, and I also get the impression that trying to make the LEDs flash can often give unexpected results, especially for the colors orange, amber and yellow. These are 'composite' colors made by blending the red and green colors which are the colors of the actual LEDs. If you try to flash red and green you are more likely to be successful. If you experience problems then my advice is to include sleep(0.01) after each LED command and avoid trying to make the LEDs flash. Here is an example of code that follows my advice- it sets both LEDs to orange:

from ev3dev.ev3 import *
from time import sleep

Leds.set_color(Leds.LEFT,  Leds.ORANGE)
sleep(0.01)
Leds.set_color(Leds.RIGHT, Leds.ORANGE)
sleep(0.01)

However, since I am not sure that other people are experiencing these problems I will assume that this is not the case for the rest of this page.
-----------------------------------------------------------------------------------------------------------------------

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 perhaps 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 pair
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. It's possible that when the left and right LEDs blink they may not be synchronised.

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