NOTE: Every post ends with "END OF POST". If you don't see it then open the full post in a separate page!

Blinking LEDs on Raspberry Pi

Here is how you can blink some LEDs with Raspberry Pi.

First of all, this is what you will end up with:

LEDs_Raspberry_Pi

You will have three LEDs being controlled programmatically from the Pi.
We will control the LEDs through the GPIO PINs by using Linux’s built-in GPIO sysfs.

Ingredients

I used the pieces that came with the Raspberry Pi Model B+ Starter Pack (http://www.adafruit.com/product/2125):

  • Raspberry Pi Model B+
  • SD card with Raspbian Wheezy (Linux)
  • USB to TTL serial cable: for communication with the Pi
  • For the circuit
    • Breadboard
    • Adafruit Pi Cobbler Plus Kit- Breakout Cable
    • 3 pieces of 560 ohm resistors
    • 3 LEDs: red, green, blue
    • 4 pieces of male/male jumper wires

Step 1: Boot up and establish serial connection

Connect the Pi to the breadboard with the breakout cable and the cobbler.
Connect the USB to TTL cable like this: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-5-using-a-console-cable/connect-the-lead

  • Brown to RXD PIN (receive)
  • White to TXD PIN (transmit)
  • Black to GND PIN (ground)
  • Leave the Red cable unconnected. It is for power, but we will use the power adapter of the Pi for that.
  • Plug the USB plug to your computer.

Put your SD card with Raspbian into the Pi.
Plug the power cable to your Pi.
Plug the Pi’s power adapter to the wall socket.
Now the Pi is booting. You should see its red led lit up.

Run screen on your laptop:

sudo screen /dev/ttyUSB0 115200

Press ENTER and you should see something like this:

[    2.641363] usb 1-1: New USB device found, idVendor=0424, idProduct=9514
[    2.651493] usb 1-1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[    2.661640] hub 1-1:1.0: USB hub found
[    2.667076] hub 1-1:1.0: 5 ports detected
[    2.951168] usb 1-1.1: new high-speed USB device number 3 using dwc_otg
[    3.071404] usb 1-1.1: New USB device found, idVendor=0424, idProduct=ec00
[    3.079917] usb 1-1.1: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[    3.108390] smsc95xx v1.0.4
[    3.177061] smsc95xx 1-1.1:1.0 eth0: register 'smsc95xx' at usb-bcm2708_usb-1.1, smsc95xx USB 2.0 Ethernet, b8:27:eb:9d:7e:2c
[    3.958510] udevd[156]: starting version 175
[    6.232318] bcm2708-i2s bcm2708-i2s.0: Failed to create debugfs directory
[    9.452151] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
[    9.943347] EXT4-fs (mmcblk0p2): re-mounted. Opts: (null)
[   15.772181] FAT-fs (mmcblk0p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
��
Raspbian GNU/Linux 7 raspberrypi ttyAMA0

raspberrypi login: 

OK. Now you can shutdown the Pi like this:

sudo shutdown -h now

Unplug the power adapter from the wall.
Unplug the USB cable from your computer.

Step 2: Wire up the circuit

Here is the schematic I used:

RPi LEDs schematic

  • GND PIN: One of the GPIO GND PIN of the Pi
  • R1, R2, R3: 560 ohm resistors
  • D1, D2, D3: Red, green and blue LEDs
  • PIN 23, 24, 25: These GPIO PINs will provide 3.3 Volts to each LED circuit

Step 3: Basic scripts for GPIO control

Script for initializing a LED PIN:

pi@raspberrypi:~$ cat gpio-init.sh 
#!/bin/sh

die() {
        echo "Error: $1"
        echo "Usage: gpio-init.sh pin-number in|out"
        exit 255
}

if [ $# -ne 2 ]; then
        die "Missing argument"
fi

PIN=$1
DIR=$2

echo -n "Exporting GPIO pin $PIN... "
echo $PIN > /sys/class/gpio/export || die "Cannot export"
echo "done"
sleep 1

echo -n "Setting direction to $DIR... "
echo $DIR > /sys/class/gpio/gpio$PIN/direction || die "Cannot set direction"
echo "done"
sleep 1

Script for putting PINs HIGH or LOW:

pi@raspberrypi:~$ cat gpio-write.sh 
#!/bin/sh

die() {
        echo "Error: $1"
        echo "Usage: gpio-write.sh pin-number value"
        exit 255
}

if [ $# -ne 2 ]; then
        die "Missing argument"
fi

PIN=$1
VAL=$2

echo -n "Writing $VAL to pin $PIN... "
echo $VAL > /sys/class/gpio/gpio$PIN/value || die "Cannot write"
echo "done"

Script for de-initializing LEDs:


pi@raspberrypi:~$ cat gpio-done.sh 
#!/bin/sh

die() {
        echo "Error: $1"
        echo "Usage: gpio-done.sh pin-number"
        exit 255
}

if [ $# -ne 1 ]; then
        die "Bad argument"
fi

PIN=$1

echo -n "Un-exporting GPIO pin $PIN... "
echo $PIN > /sys/class/gpio/unexport || die "Cannot un-export"
echo "done"
sleep 1

Step 4: Blink some LEDs

Script for defining common variables (e.g. PIN numbers) used by other scripts.

pi@raspberrypi:~$ cat blinker-def.sh 
# This is meant to be sourced from other scripts.

RED=23
GREEN=24
BLUE=25

Script for initializing the LEDs:

pi@raspberrypi:~$ cat blinker-init.sh 
#!/bin/sh

. ./blinker-def.sh

./gpio-init.sh $RED out
./gpio-init.sh $GREEN out
./gpio-init.sh $BLUE out

Script for blinking the LEDs:
We loop through some hardcoded LED patterns.

pi@raspberrypi:~$ cat blinker-blink.sh 
#!/bin/sh

. ./blinker-def.sh

while true; do
        for J in 100 010 001 101 011 100 110 001; do
                VAL_RED=`expr substr $J 1 1`
                VAL_GREEN=`expr substr $J 2 1`
                VAL_BLUE=`expr substr $J 3 1`
                ./gpio-write.sh $RED $VAL_RED
                ./gpio-write.sh $GREEN $VAL_GREEN
                ./gpio-write.sh $BLUE $VAL_BLUE
                sleep 1
        done
done

Script for de-initialization:

pi@raspberrypi:~$ cat blinker-cleanup.sh 
#!/bin/sh

. ./blinker-def.sh

./gpio-done.sh $RED
./gpio-done.sh $GREEN
./gpio-done.sh $BLUE

So, if you then run these in this sequence, you should get LEDs blinking in the predefined patterns:

$ sudo ./blinker-init.sh
$ sudo ./blinker-blink.sh # Press Ctrl-C when you got bored of it.
$ sudo ./blinker-cleanup.sh

END OF POST

Advertisements

One Comment on “Blinking LEDs on Raspberry Pi”

  1. […] post ends with "END OF POST". If you don't see it then open the full post in a separate page! Blinking LEDs on Raspberry Pi […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s