77

There are 5 LEDs on the RPi: OK, PWR, FDX, LNK, 10M.

I'd like to know if it's possible to control any of these from software i.e. turn them on, or change intensity (or even change colour gasp).

And if so, where can I read up about it? The LEDs could be quite handy way of signalling user application status when they are not required for the original use.

darryn.ten
  • 1,486
  • 2
  • 14
  • 18
Maria Zverina
  • 5,168
  • 5
  • 30
  • 42
  • 2
    And yes - I know I can add LEDs easily onto the GPIO outputs - but I'm curious what can be done without external hardware. – Maria Zverina Jun 25 '12 at 20:10
  • 2
    LEDs are generally single colour (especially when required for one purpose), so you can forget about that part :) – Jivings Jun 26 '12 at 00:11
  • 2
    @Jivings Agree that they're probably single colour given RPi's cost ... but it never hurts to ask :) – Maria Zverina Jun 26 '12 at 08:19

7 Answers7

64

The OK LED can be controlled from user space software. Details here: Re: Can we control the on-board leds

Summarised from the above (all credit to BrianW):

The OK LED is available as /sys/class/leds/led0/.

The kernel LED driver has "triggers" which let some other part of the kernel control the LED. The default trigger for the LED is 'mmc0', which makes it come on when the SD card is accessed.

root@raspberrypi:~# cat /sys/class/leds/led0/trigger
none [mmc0]

You can deactivate the mmc0 trigger as follows:

echo none >/sys/class/leds/led0/trigger

The LED can be turned on and off using the 'brightness' file. The minimum brightness is 0, and the maximum is 255. As there is no variable brightness support, any value greater than 0 will turn the LED on.

echo 1 >/sys/class/leds/led0/brightness
echo 0 >/sys/class/leds/led0/brightness

Setting the brightness to 0 automatically sets the trigger to "none".

If you want the LED to go back to its default function:

echo mmc0 >/sys/class/leds/led0/trigger

There are a couple of kernel modules you can load up (ledtrig_timer and ledtrig_heartbeat) which will flash the LED for you.

modprobe ledtrig_heartbeat
echo heartbeat >/sys/class/leds/led0/trigger

Once you have turned off the mmc0 trigger, you can use GPIO16 to control the LED. It's active-low, so you need to set the pin low to turn the LED on, and high to turn it off.

From Python, you can use the module RPi.GPIO to control pin 16. There is also a C# driver.

Sample code

#!/usr/bin/python

import RPi.GPIO as GPIO from time import sleep

Needs to be BCM. GPIO.BOARD lets you address GPIO ports by periperal

connector pin number, and the LED GPIO isn't on the connector

GPIO.setmode(GPIO.BCM)

set up GPIO output channel

GPIO.setup(16, GPIO.OUT)

On

GPIO.output(16, GPIO.LOW)

Wait a bit

sleep(10)

Off

GPIO.output(16, GPIO.HIGH)

Guy
  • 1,657
  • 1
  • 16
  • 18
  • 5
    FYI on the Raspberry Pi 2, you can finally control the PWR LED as well! Switch it to GPIO mode with echo gpio | sudo tee /sys/class/leds/led1/trigger, then on or off with echo [0|1] | sudo tee /sys/class/leds/led1/brightness. – geerlingguy Mar 12 '15 at 02:59
  • 2
    For anybody wondering on how to do this from command line (without super user privileges required to write to /sys/class...), first set the LED to trigger on gpio, and then install WiringPi https://learn.sparkfun.com/tutorials/raspberry-gpio/c-wiringpi-setup after that you can just "$ gpio -g mode 16 output && gpio -g write 16 0" to turn the led ON (and 1 for off) – joonas.fi Feb 17 '17 at 11:37
  • Great answer! Want to add this: Effectively the LED has brightness support by LED multiplexing. SSH to your Raspi in two separate Terminal windows, one for changing config on the fly and one to create certain load (=trigger events). In the config terminal set the CPU as the trigger with echo cpu >/sys/class/leds/led0/trigger. When the Raspi is idle the LED will be very dim. When you launch top the LED will shine stronger shortly (max. 1 sec). In top you see that your Raspi is 99% idle. Then run yes which constantly prints "y" hence stresses CPU to the max, LED getting fully bright! – porg Oct 28 '20 at 01:04
  • The leds name have changed recently on my pi4 now I use /sys/class/leds/PWR/trigger and /sys/class/leds/ACT/trigger. – Emmanuel Jun 03 '23 at 14:35
20

None of the on board LEDs can be controlled in software - they are all used for other things.

  • OK - indicates SD card access
  • PWR - indicates power to the micro USB connection
  • FDX - Full Duplex LAN
  • LNK - LAN Activity
  • 10M - 10M/100M connection speed - if it is lit, the RPi is connected at 100M.

Now, that's the official answer... Let's study the schematic.

Page 4 The PWR LED is connected directly to the power supply, so we can't control that in software.

Page 3 FDX, LNK and 10M are all connected to the Ethernet chip, so again we can't control them in software (without some funky traffic hack...).

Page 4 BUT OK is actually controlled by GPIO 16, so there is the possibility of a hack...

Alex Chamberlain
  • 15,530
  • 14
  • 67
  • 113
  • 1
    @AlexChamberlain Nice definition of the LEDs. But are we really sure they can't be controlled by software? Are network LEDs under firmware control or the actual hardware? And if they are firmware controlled, can we reverse engineer this somehow? – Maria Zverina Jun 26 '12 at 09:38
  • 6
    Check out the schematic - PWR is directly connected to the power, and FDX, LNK & 10M to the ethernet chip. As I said, you might be able to hack the OK LED. – Alex Chamberlain Jun 26 '12 at 09:43
  • Alex is correct. the BCM chip has no control (excpet for GPIO16) over those LED's; so no software can access them, not even low level kernel code. You would have to hack the LAN chip or create your own short cut circuit to these LED's- BUt that is really not a great idea. Why not just add your own? – Piotr Kula Aug 09 '12 at 10:13
  • 4
    It seems to me that this other answer shows at least part of this answer (and some comments) to be false. Am I reading things correctly? It may be accurate that only OK (labeled "ACT" on my Rev B. board) can be controlled, but it seems like that one can be. (I haven't tried to actually do it yet, though.) – lindes Oct 28 '13 at 19:01
  • 1
    Just confirming that the statement "None of the on board LEDs can be controlled in software - they are all used for other things" is false. I tested the accepted response on raspberry pi 3 and 4s.

    Here are the commands I used while verifying above...

    Turn on both LEDs echo 1 | sudo tee /sys/class/leds/led{0,1}/brightness

    Turn off both LEDs echo 0 | sudo tee /sys/class/leds/led{0,1}/brightness

    Attach the "heartbeat" trigger to both LEDs echo heartbeat | sudo tee /sys/class/leds/led{0,1}/trigger

    – Andrew Allbright Jan 13 '20 at 00:01
  • By the way, in newer versions of Raspberry Pi, STATUS OK LED is called STATUS "ACT" LED and POWER ON LED is called POWER OK "PWR" LED. This revision is reasonable since the new names are more clear (because "observed an access to SD" doesn't necessarily equal "status is okay"). – ynn Jun 03 '20 at 12:47
9

You can control all leds (except PWR in older Pi models, as said in other answers).

But for ethernet leds you will need to patch the driver and recompile the kernel.

Information of how recompile can get here: http://elinux.org/RPi_Kernel_Compilation

Patch and more information here(google translate if need): http://everpi.tsar.in/2013/11/patch-para-controlar-os-leds-ethernet-do-raspberrypi.html

After done, you be able to control over: /sys/class/smsc95xx_leds and eth_fdx, eth_lnk e eth_spd.

Example: echo 0 > /sys/class/smsc95xx_leds/eth_fdx echo 1 > /sys/class/smsc95xx_leds/eth_fdx

Bex
  • 2,929
  • 3
  • 25
  • 34
czar
  • 91
  • 1
  • 2
8

I can confirm that with the Raspberry Pi 2 it's possible to control the PWD LED as well!

The power LED is controlled by the files in:

/sys/class/leds/led1

You can turn it off just like the Status LED using:

echo 0 > /sys/class/leds/led1/brightness # Power LED
echo 0 > /sys/class/leds/led0/brightness # Status LED

See Guy's answer for more ways to control the LEDs

Bex
  • 2,929
  • 3
  • 25
  • 34
Patrick
  • 181
  • 1
  • 1
8

I wrote a userspace program which let you control the Ethernet LEDs.

The program requires the more recent libusb-1.0 (NOT the older 0.1). It works with LAN9512 (e.g. on the older Raspberry B) as well as LAN9514 chips (e.g. on Raspberry B+ or Raspberry 2)

Details can be found here: LAN951x LED control

Jackfritt
  • 3
  • 3
blip
  • 81
  • 1
  • 1
  • As the content of your link is quite small, it would be better to include it here in your answer, in blockquotes, as the link may die in the future. If it does then your answer will not be much use. Also, provide the github link here as well. – Greenonline Aug 17 '18 at 20:47
7

There is a simpler way to control the LAN LEDs from userland. Source code is available from

http://www.raspberrypi.org/forums/viewtopic.php?f=63&t=72070

The program needs to be run with sudo.

joan
  • 71,024
  • 5
  • 73
  • 106
5

With the Pi 2, you can control both the red and green onboard LED's.

The Windows 10 IoT documentation lists the red power LED and the green Ok LED on GPIO 35 and 47, respectively.

https://ms-iot.github.io/content/en-US/win10/samples/PinMappingsRPi2.htm

I tried with the Windows 10 IoT and with Python on Raspbian. Both can control the LED's, although Raspbian does override the LED whenever something accesses the SD card. Presumably, clearing the trigger would remove this behavior.)

Here's a sample. (Note that this does not override the trigger as mentioned in prior posts)

print ("Program Start")
import RPi.GPIO as GPIO
import time

channels = [35, 47]

print ("Turning off LED's")
GPIO.setmode(GPIO.BCM)
GPIO.setup(channels, GPIO.OUT)
GPIO.output(channels, GPIO.LOW)
time.sleep(5)

print ("Turning on LED's")
GPIO.output(channels, GPIO.HIGH)
time.sleep(5)

GPIO.cleanup()

print ("Program End")
TomXP411
  • 151
  • 1
  • 2
  • On a Raspberry Pi 3 this sample program seems to consistently crash/hang the operating system. – centic Jun 21 '20 at 16:37