Thursday, September 24, 2015

Python/RPi.GPIO Control Raspberry Pi 2 B on-board ACT LED

I have a old example "Control the on-board LED using Python" to toggle Raspberry Pi 1 ACT LED. It's updated to toggle the ACT LED on Raspberry Pi 2.


On Raspberry Pi 2 B, the ACT LED is assigned pin 47.

pyGPIO.py
import sys
import RPi.GPIO as GPIO
import time

print(sys.version)
print("GPIO.VERSION: " + GPIO.VERSION)
print("GPIO.RPI_REVISION (deprecated): " + str(GPIO.RPI_REVISION))

print("")
print("GPIO.RPI_INFO:")
print(GPIO.RPI_INFO)

print("")
for keys,values in GPIO.RPI_INFO.items():
 print(keys + " : " + str(values))
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#GPIO.setup(16, GPIO.OUT)
GPIO.setup(47, GPIO.OUT)

for num in range(1, 5):
    print num
    #GPIO.output(16, True)   ##Turn OFF LED
    GPIO.output(47, False)   ##Turn OFF LED for RPi 2 B
    print "LED OFF"
    time.sleep(2)
    #GPIO.output(16, False) ##Turn ON LED
    GPIO.output(47, True)   ##Turn ON LED for RPi 2 B
    print "LED ON"
    time.sleep(3)

#GPIO.output(16, True)   ##Turn OFF LED
GPIO.output(47, False)   ##Turn OFF LED for RPi 2 B


In my experience, it cannot be perform using sudo, you have to login as root. To login as root, refer to the post "Set password of root".

Then login as root.

by default, the ACT LED is set triggered by mmc0. It can be checked with the command:

# cat /sys/class/leds/led0/trigger
none [mmc0] timer oneshot heartbeat backlight gpio cpu0 default-on

In order to program it as GPIO, remove the trigger with the command:
# echo none >/sys/class/leds/led0/trigger

Run our Python example, pyGPIO.py
# python pyGPIO.py

After test, resume the trigger by mmc0 with command (or reboot):
# echo mmc0 >/sys/class/leds/led0/trigger


Next:
- Python control Raspberry Pi 2 PWR/ACT LED, using RPi.GPIO or system's shell.

No comments: