Raspberry Pi LEDs

Blinking the Raspberry Pi ACT LED with a bash script

Let’s use a Bash script to blink the ACT/OK LED. (Called ACT on revision 2 both model A and B, called OK on model B revision 1.)

I am using Raspbian Linux on my Pi. If you need any help setting that up, please follow this guide.

According to the Unix tradition ‘Everything is a file’, and so is the ACT LED on the Pi.

Start with remote connecting to your Pi with SSH:

ssh pi@192.168.0.149

Once you have logged in:

cd /sys/class/leds/led0

Look what’s in there:

ls

You should see something like this:

brightness  device  max_brightness  power  subsystem  trigger  uevent

The brightness file controls if the LED is on or off (1 or 0). Print the contents of the file:

cat brightness

The command should display 0 (off).

The trigger file decides what controls the LED. Print the contents of the trigger file:

cat trigger

The command should display something like:

none [mmc0] timer oneshot heartbeat backlight gpio cpu0 default-on rfkill0 phy0rx phy0tx phy0assoc phy0radio

[mmc0] means that the LED will blink whenever the SD card is accessed (mmc is short for MultiMediaCard which was the predecessor to SD).

Change to root user (always be careful with what you do when logged in as root):

sudo su

First we need to take control over the LED:

echo none > trigger

Turn on the LED by putting a 1 into the brightness file:

echo 1 > brightness

Turn off the LED by putting a 0 into the brightness file:

echo 0 > brightness

Restore the trigger:

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

Don’t forget to switch back to the pi user!

exit

Now we know how it works. Goto home directory:

cd ~

Create a bash script:

nano blink.sh

Copy and paste the following code:

This will blink the LED with 1 second intervals five times.
Save the file by: Ctrl + X, Y, Enter.

Look at the file details

ls -al

As we can see the file is not executable. Read more about file permissions in Unix on Wikipedia.

drwxr-xr-x  2 pi pi 4096 Jan 18 16:11 .
drwxr-xr-x 11 pi pi 4096 Jan 18 15:45 ..
-rw-r--r--  1 pi pi  138 Jan 18 17:09 blink.sh

Make it executable by the owner – the pi user.

chmod o+x blink.sh

You need to be root to run the script.

sudo ./blink.sh

Mission accomplished! It blinks!

Thanks to BrianW for his post on the Raspberry Pi Forum.

Share on Google+Share on LinkedInTweet about this on TwitterEmail this to someoneShare on FacebookPin on Pinterest

3 thoughts on “Blinking the Raspberry Pi ACT LED with a bash script

  1. Thank so you much for posting this!

    I just got my raspi last week, ordered a breadboard and Adafruit Cobbler…but no LEDs, resistors, etc.

    Been itching to get some physical computing done, and this small step has got me going.

    To Radio Shack!

  2. My Pi did not like some of the commands, so i adjusted some of them and added that the led lights for longer each loop pass.

    my script:

    #!/bin/bash
    echo none > /sys/class/leds/led0/trigger
    for i in 1 2 3 4 5; do
    echo 1 > /sys/class/leds/led0/brightness
    sleep $i
    echo 0 > /sys/class/leds/led0/brightness
    sleep 1
    done

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">