I'm trying to power the onboard LEDs of my Raspberry Pi 3 Model B+ using this tutorial as follows:

Firstly you need to disable the usual triggers for the built-in LEDs. This can be done from the terminal with the following commands:

$ echo none | sudo tee /sys/class/leds/led0/trigger
$ echo gpio | sudo tee /sys/class/leds/led1/trigger

Now you can control the LEDs with gpiozero like so:

from gpiozero import LED
from signal import pause

power = LED(35) # /sys/class/leds/led1
activity = LED(47) # /sys/class/leds/led0

activity.blink()
power.blink()
pause()

when I run the above code stored in onboard_led.py, I get this error: /usr/lib/python2.7/dist-packages/gpiozero/pins/rpigpio.py:95: PinNonPhysical: no physical pins exist for GPIO35 'no physical pins exist for GPIO%d' % number)) /usr/lib/python2.7/dist-packages/gpiozero/pins/rpigpio.py:95: PinNonPhysical: no physical pins exist for GPIO47 'no physical pins exist for GPIO%d' % number))

Is the documentation out of date, my pins don't exist, or the pins have changed?

up vote 3 down vote accepted

From a note near the very bottom of the documentation you linked to:

On the Pi 3B the LEDs are controlled by a GPIO expander which is not accessible from gpiozero (yet).

  • Thanks. Didn't get to that. Perhaps that message should come first before the steps? Well, thanks. @stevieb So any other means to control the onboard LED lights? – Rexford Jul 19 '17 at 14:22
  • 1
    @Rexford I honestly don't know if there's another way. I've never needed that functionality. You could email the maintainers of that site and request they put the notice at the top of the page as opposed to the bottom... – stevieb Jul 19 '17 at 14:25
  • Sorry, this does not hold since 2016, as the LEDs are accessible to the best of my knowledge, see my answer below. – TheDiveO Jul 20 '17 at 9:31

This answer worked for me on Raspberry Pi 3B running Raspbian Lite Stretch to blink the green activity light. (I did not attempt to control the red power light.) It involves an obscure command to write to the mailbox interface for the light.

alias led_on="/opt/vc/bin/vcmailbox 0x00038041 8 8 130 1 >/dev/null"
alias led_off="/opt/vc/bin/vcmailbox 0x00038041 8 8 130 0 >/dev/null"
while [ 1 ]; do led_on; sleep 1; led_off; sleep 1; done

To disable the automatic blinking on SD card activity, you must write to the LED's trigger file.

sudo sh -c "echo none > /sys/class/leds/led0/trigger"

The trigger is reset on reboot. You can add the above command to /etc/rc.local before the exit 0 line to disable the SD card activity indicator at startup.

I have not seen any evidence that the pi3-act-led overlay can be used.

The following Python code seems to work well for me to control the onboard LEDs on a Pi 3B running Raspbian GNU/Linux 9 (stretch). Save it to led.py, then you can call it from your main program with import led then led.control("red", "flash"), for example.

If you don't want to use Python, you can issue the commands directly in a shell. For example, to turn the red LED off, echo 0 | sudo tee /sys/class/leds/led1/brightness should do the trick.

import os

green_led = "/sys/class/leds/led0"
red_led = "/sys/class/leds/led1"
green_led_reset_trigger = "mmc0"
red_led_reset_trigger = "input"

def control(led_colour, state):
    if led_colour == "green":
        device = green_led

    elif led_colour == "red":
        device = red_led

    else:
        return

    if  state == "on":
        os.system("echo none | tee " + device + "/trigger >> /dev/null")
        os.system("echo 1 | tee " + device + "/brightness >> /dev/null") 

    elif state == "off":
        os.system("echo none | tee " + device + "/trigger >> /dev/null")
        os.system("echo 0 | tee " + device + "/brightness >> /dev/null") 

    elif state == "flash":
        os.system("echo timer | tee " + device + "/trigger >> /dev/null")
        os.system("echo 1 | tee " + device + "/brightness >> /dev/null")

    elif state == "reset":
        if device == green_led:
            trigger = green_led_reset_trigger
        elif device == red_led:
            trigger = red_led_reset_trigger

        os.system("echo " + trigger + " | tee " + device + "/trigger >> /dev/null")
        os.system("echo 1 | tee " + device + "/brightness >> /dev/null") 

    else:
        return

Issues:

  • The Pi can be very slow to change the LED state. If you call this function more than once a second, it sometimes misses the state change.
  • To avoid having to run the code as root, you'll need to change the default permissions on the LEDs. You can automate this by placing the following script in /etc/init.d/led-control:

    #! /bin/sh
    # /etc/init.d/led-control
    
    ### BEGIN INIT INFO
    # Provides:          led-control
    # Required-Start:    $remote_fs $syslog
    # Required-Stop:     $remote_fs $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Change permissions on Raspberry Pi LEDs to allow them to be switched on and off
    # Description:       Raspberry Pi status LEDs are normally only controlled by root. This script enables them to be controlled by regular users.
    ### END INIT INFO
    
    # If you want a command to always run, put it here
    
    # Carry out specific functions when asked to by the system
    case "$1" in
      start)
        echo "Enabling led-control"
        # Raspberry Pi status LED
        /bin/chmod 666 /sys/class/leds/led0/trigger
        /bin/chmod 666 /sys/class/leds/led0/brightness
        # Raspberry Pi power LED
        /bin/chmod 666 /sys/class/leds/led1/trigger
        /bin/chmod 666 /sys/class/leds/led1/brightness
        ;;
      stop)
        echo "Disabling led-control"
        # kill application you want to stop
        /bin/chmod 644 /sys/class/leds/led0/trigger
        /bin/chmod 644 /sys/class/leds/led1/trigger
        /bin/chmod 644 /sys/class/leds/led0/brightness    
        /bin/chmod 644 /sys/class/leds/led1/brightness
        ;;
      *)
        echo "Usage: /etc/init.d/led-control {start|stop}"
        exit 1
        ;;
    esac
    
    exit 0
    

    Install the startup script with sudo update-rc.d led-control defaults so it runs on boot. Activate it with sudo /etc/init.d/led-control start

While it is correct that the onboard LEDs are on an expander, they are already accessible for quite some time. See this github issue 1363 from 2016. According to its documentation, the pi3-act-led overlay "changes the GPIO controller back to the standard one and restores the dtparams".

  • 1
    So how would I turn it on and off? I'm sorry, but the link didn't provide any easy to understand way to do the on or off. – Rexford Jul 21 '17 at 9:39
  • After you have configured the boot to load the overlay, the existing tutorials on how to control the LEDs should apply again. – TheDiveO Jul 21 '17 at 10:38
  • @TheDiveO I've attempted to add dtoverlay=pi3-act-led,gpio=18 as in the linked answer, but then I still could not control the activity LED. I also tried gpio=47. I also cannot find any reference to anyone who has gotten this to work. – rgov Mar 28 at 4:57

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.