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@

Once you have logged in:

cd /sys/class/leds/led0

Look what’s in there:


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!


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.

  • Kevin

    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!

    • Johan Karlsson

      You’re welcome! I’m happy to spread some Raspberry Pi joy =)