1

I want to control the PWR and ACT LEDs on the Raspberry Pi 2 B using a Python script.

I found these lines but I don't know how to implement it in Python, can anyone help me please?

# Set the PWR LED to GPIO mode (set 'off' by default).
echo gpio | sudo tee /sys/class/leds/led1/trigger

# (Optional) Turn on (1) or off (0) the PWR LED.
echo 1 | sudo tee /sys/class/leds/led1/brightness
echo 0 | sudo tee /sys/class/leds/led1/brightness

# Revert the PWR LED back to 'under-voltage detect' mode.
echo input | sudo tee /sys/class/leds/led1/trigger

# Set the ACT LED to trigger on cpu0 instead of mmc0 (SD card access).
echo cpu0 | sudo tee /sys/class/leds/led0/trigger

Source: Controlling PWR and ACT LEDs on the Raspberry Pi - Jeff Geerling

2 Answers 2

2

At the beginning of your program add:

import os

Then, to execute those commands in your question just use:

# Set the PWR LED to GPIO mode (set 'off' by default).
os.system("echo gpio | sudo tee /sys/class/leds/led1/trigger")

# (Optional) Turn on (1) or off (0) the PWR LED.
os.system("echo 1 | sudo tee /sys/class/leds/led1/brightness")
os.system("echo 0 | sudo tee /sys/class/leds/led1/brightness")

# Revert the PWR LED back to 'under-voltage detect' mode.
os.system("echo input | sudo tee /sys/class/leds/led1/trigger")

# Set the ACT LED to trigger on cpu0 instead of mmc0 (SD card access).
os.system("echo cpu0 | sudo tee /sys/class/leds/led0/trigger")
0
1

With my pigpio Python module.

#!/usr/bin/env python

# Pi2B_leds.py
# Public Domain

import time
import pigpio

# The following values are unique to the Pi2B.

POWER_LED=35
ACTIVITY_LED=47
USB_POWER_GPIO=38

pi = pigpio.pi()
if not pi.connected:
   exit(0)

stop = time.time() + 30

while time.time() < stop:

   pi.write(POWER_LED, 0)
   pi.write(ACTIVITY_LED, 1)

   time.sleep(3)

   pi.write(POWER_LED, 1)
   pi.write(ACTIVITY_LED, 0)

   time.sleep(1)

pi.stop()

Note that the green activity LED probably will not act as you expect. The system will still be updating its value.

2
  • What about this line echo gpio | sudo tee /sys/class/leds/led0/trigger? It will take the control of the act led, am I wrong?
    – ozat
    Commented Mar 13, 2016 at 14:44
  • No, you are right. You will need to do that as Patrick Cook suggests or perhaps as a device tree entry (I'm not sure if it's possible, check /boot/overlays/README for leds).
    – joan
    Commented Mar 13, 2016 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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