2: Two more LEDs

No surprises here – add 2 more LEDs to the board as shown below:

Added two more LEDs

Added two more LEDs

Three LEDs on the breadboard

Three LEDs on the breadboard

They are wired up as before with the Yellow LED being connected to wiringPi pin 1 (GPIO-18) and the Green LED on wiringPi pin 2 (GPIO-21)

Test with the gpio program as before. e.g.

gpio mode 0 out
gpio mode 1 out
gpio mode 2 out
gpio write 0 1
gpio write 1 1
gpio write 2 1

to turn the all on.

If you want to do this somewhat easier, then we can use the shell programming system:

for i in 0 1 2 ; do gpio mode $i out; done
for i in 0 1 2 ; do gpio write $i 1; done
for i in 0 1 2 ; do gpio write $i 0; done

to do this using the GPIO pins:

for i in 17 18 21 ; do gpio -g write $i 1; done

If the above doesn’t make sense to you, then don’t worry for now.

Lets move on to finishing setting up the breadboard in part 3.


2: Two more LEDs — 22 Comments

  1. Ok I am complete newbie to this and got really excited as this is the first time I have got this to work. May I be stupid and ask what shell programming system and where do I find it.

    Thanks a million as I am dying to learn


    • The “shell” is the name of the thing (program) that you type commands into – when you write a shell script, it’s really a program written using the normal commands you type in (plus a few more for flow control, etc.). If used to ole DOS, then it’s similar to writing a DOS ‘batch’ file.

      There are several different shells avalable, but the common one these days is called “bash”.

      Have a look at the tuxx.sh code for an example of a program written using the bash shell.


  2. This is great. Just one thing though, the negative wire should be in the blue bus, not red. This might cause short circuit to the others and wasting their pi.

    I’m very cautios about this having fried 2 arduino mega in the past.

    • The fritzing diagram is correct, but the breadboard is the wrong way round compared to the photo I took. I’m slowly working my way through these again and re-doing them as needed.



  3. When doing the “for i in 17 18 21 ; do gpio -g write $i 1; done”
    GPIO 21 wont react for some reason. Using the wiringPi pin numbers it worked.
    Any idea what i might do wrong?

    • You have a Rev 2 board and GPIO pin 21 is GPIO pin 27. (and that was written before Rev 2 boards existed)

      Stick to wiringPi numbers and they’ll always work as it knows the difference between board revisions.


  4. Hey

    Just wanted to say thanks for your tutorial, this kind of thing is brand new to me and following your steps I was able to turn my external LED on and off!!

  5. Hey friend!

    first thanks for the good tutorial.
    and now, Are you gone done some like this using Python ?

    tks again,

  6. Thanks Gordon this tutorial was fun. I have to admit I sort of had to stop on this one and play with the lights. I ended up writing a looping shell script (ok stole yours and some one script) that did a red on for 5 second then off yellow on for 2 seconds then off then green on for 5 seconds and off and keeps looping until a key is pressed (LOL sort of like a traffic light). Sadly I won’t be able to do the next one until the stores open tomorrow as I don’t have the button 🙁

  7. Gordon, you’re awesome. Thanks for the clear explanations and the diagrams and photos. Most people don’t realize how much work it is to get this stuff spelled out correctly and simply, but we who do realize really appreciate it!


  8. Gordon, thanks a lot for making this tutorial!
    I just now got my Pi blinking three LEDs from the command line! Awesome!!
    Now I’m off to try to do it using a C program…

    Thanks again!

  9. Hey, I just wanted to thank you for the tutorial!! I have searched everywhere for something like this. But I just have two questions.
    First, can you do this code in python? If not (two) can you turn the lights on and off with two different commands? (Sorry if you already explained that, I’ll look through your post some more).

    • I’m not a Python programmer. Everything I do is in C or BASIC.

      for 2 different commands, you can use the gpio command – just look in the tuxx.sh program for details.


      • Thanks for the fast reply! I know how to run shell commands in Python, so I think I can do this in python.