Flash an LED

Here is a quick tutorial for beginners to GPIO.  Making an LED flash on and off might seem quite boring, but if you don’t know the basics then how will you be able to progress with other projects! This tutorial will show you howto install wiringPi (to talk to the GPIO pins) and the code needed to make the LED work.


  • Raspberry Pi
  • Breadboard (to connect it all)
  • LED(s) (to look pretty)
  • Resistor
  • Jumper wires (to connect all the bits)
LED for Beginners

Breadboard setup

The first thing to do is to lay-out your breadboard. Place an LED onto the board so that the two pins are occupying two parallel rows (but not crossing the central divide).

Connect a jumper wire to GND (actual PIN 6) on the Pi board and the other end to one of the connectors on the long red strip down the side of the breadboard (This is our ground (GND) channel).

Next place a transistor between one of the rows that our LED is on and the long red strip down the side (GND).

Lastly connect a jumper wire to GPIO 0 (actual PIN 3) on the Pi board and the other end to the other row on the breadboard that is not connected to GND.

If the LED doesn’t light up using the code below you will probably need to swap the legs of the LED on the breadboard.

Right!  That’s the hardware set-up.  Now for the software.

Firstly we need to download wiringPi.  WiringPi is a set of libraries by Gordon at https://projects.drogon.net/.  His work makes it very easy to control the GPIO pins.

To install wiringPi type the following into the terminal:

git clone git://git.drogon.net/wiringPi

Once downloaded then type:

cd wiringPi

Lovely! Now thats installed we can make some light!

In the terminal type the following:
This tells GPIO 0 to be used as an output to provide power to the LED.

gpio mode 0 out

Ok! You should now have an LED that is turned on… This code tells GPIO 0 to provide current (1).

gpio write 0 1

To turn it off simply type:

gpio write 0 0

Easy peasy eh?

Here is a small script that you can use to flash the LED on and off given user input. Play around with the values and see what happens.  Save the following into a file called led.sh and run by typing ./led.sh into a terminal:

echo -e "\n Would you like the LED to flash? [y=yes/n=no] \n"
read input
if [ $input == "y" ]; then
# ask the user how many times to flash the LEDs
echo -e "\n How many times would you like the LED to flash? \n"
read flash
# Set the LED pin to output
gpio mode 0 out
while [ $i -lt $flash ]
echo -n -e ">>>>> The LEDs have flashed $i times \r"
# Turn the LED on
gpio write 0 1
# wait for 1/2 a second
sleep 0.5
# turn the LED off
gpio write 0 0
# wait for 1/2 a second
sleep 0.5
echo "This script will now exit"

Happy flashing 😀

This entry was posted in bash, Child Friendly, GPIO, LED, raspberry pi, Tutorial and tagged , , , , . Bookmark the permalink.

2 Responses to Flash an LED

  1. mike says:

    It’s a resistor not a transistor

Leave a Reply

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