GPIO Traffic Light demo on the Raspberry Pi with Python or C

27/08/2012

This is a simple GPIO traffic light demo on the Raspberry Pi. The software is written in C and Python.

Using the Starter Kit-A or Starter Kit-C wire up the breadboard as shown. Note, the cathode of the LED is the short lead which connects to ground.

 

Demo Software in C

Now download the software by:

For old rev 1 board

wget http://skpang.co.uk/dl/traffic_light.tar 

Unpack the tar file and change into directory by:

tar xf traffic_light.tar 
cd traffic_light

 

For new Rev 2 board

wget http://skpang.co.uk/dl/traffic_light_rev_2.tar 

Unpack the tar file and change into directory by:

tar xf traffic_light_rev_2.tar 
cd traffic_light_rev_2

 

Compile the software by:

make all

Start the example by:

sudo ./traffic_light

To stop the program running press CTL-C

The main program is traffic_light.c to edit this program use:

sudo nano traffic_light.c

You can try and change the value of the delay.  Press CTL-X Y to save and exit.

To re-compile the program:

make all

Now you can run it again.

Demo Software in Python

If you have not installed Python and GPIO module, do the following:

sudo apt-get install python python-dev python-pip
sudo easy_install -U distribute
sudo pip install rpi.gpio

Download the python software by:

For old rev 1 board

wget http://skpang.co.uk/dl/traffic_light.py

Make it executable by:

chmod +x traffic_light.py

To run the program:

sudo ./traffic_light.py



 

 

For new rev 2 board

wget http://skpang.co.uk/dl/traffic_light_rev_2.py

Make it executable by:

chmod +x traffic_light_rev_2.py

To run the program:

sudo ./traffic_light_rev_2.py

Press CTL-C to stop

 

 

Parts List

 

Short URL:

Tags: , ,

22 Responses to GPIO Traffic Light demo on the Raspberry Pi with Python or C

  1. andreas on 21/09/2012 at 18:10

    gcc -o traffic_light gb_common.o traffic_light.o
    /usr/bin/ld: error: gb_common.o uses VFP register arguments, traffic_light does not
    /usr/bin/ld: failed to merge target specific data of file gb_common.o
    collect2: ld returned 1 exit status
    make: *** [traffic_light] Fehler 1

    • Paul on 24/01/2013 at 12:03

      Have successfully loade and run the Demo Software in C but now want to use the Python version so I can make adjustments to the program (one programming language is more than enough).
      Seems GPIO something not loaded so ran this ok
      “sudo apt-get install python python-dev python-pip”
      But this
      “sudo easy_install -U distribute”
      caused a “do not know Easy_install command”
      And this
      “sudo pip install rpi.gpio”
      also came up with an error.
      My board is B rev 1 and running Debian.
      Can someone please help a newbie.

  2. j on 29/09/2012 at 11:52

    before ‘make all’ try ‘make clean’

  3. Graham on 22/10/2012 at 13:51

    Using a Raspberry Pi Revision 2 board, to get the North Side yellow LED to light I had to change 21 to 27 in the line #define YEL_NORTH (1<<21) in traffic_light.c, as the GPIO number for pin 13 has changed from GPIO21 to GPIO27. Presently my East Side LEDs are not lighting at all, though that may well turn out to be for some mundane reason unconnected with any GPIO number changes.

    • Geoff on 17/01/2013 at 01:13

      Were you ever able to get your East lights working? Mine aren’t working either.

      • Geoff on 17/01/2013 at 01:32

        Needed to connect positive on left and right side of board. Duh.

  4. Craig on 05/12/2012 at 09:57

    The links to files appear to be offline, any chance of bringing them back?

  5. admin on 05/12/2012 at 10:04

    The blue text are not links. You need to type them in on your Pi.

    • adrian on 10/12/2012 at 18:55

      same issue as craig..

      when type
      wget http://skpang.co.uk/dl/traffic_light_rev_2.py
      get error
      pi@raspberrypi ~ $ wget http://skpang.co.uk/dl/traffic_light_rev_2.py
      bash: wget http://skpang.co.uk/dl/traffic_light_rev_2.py: No such file or directory

      • admin on 10/12/2012 at 19:16

        Instead of typing the text, use cut and paste.

  6. adrian on 10/12/2012 at 19:21

    That is exactly what i did

    • admin on 10/12/2012 at 19:25

      Make sure your Pi is connected to the internet and at the prompt cut and paste this line:

      wget http://skpang.co.uk/dl/traffic_light_rev_2.py

      You should get this reply:

      wget http://skpang.co.uk/dl/traffic_light_rev_2.py

      –2012-12-10 19:25:29– http://skpang.co.uk/dl/traffic_light_rev_2.py
      Resolving skpang.co.uk (skpang.co.uk)… 77.235.53.95
      Connecting to skpang.co.uk (skpang.co.uk)|77.235.53.95|:80… connected.
      HTTP request sent, awaiting response… 200 OK
      Length: 1395 (1.4K) [text/plain]
      Saving to: `traffic_light_rev_2.py.3′

      100%[======================================>] 1,395 –.-K/s in 0s

      2012-12-10 19:25:29 (3.87 MB/s) – `traffic_light_rev_2.py.3′ saved [1395/1395]

  7. adrian on 10/12/2012 at 19:41

    Thank you

    the line you entered allows me to copy and paste fine, however if i type or cut and paste from the web page i get errors. even on the chmod +x traffic_light_rev_2.py

    so sorry for being completly useless, but this is my very first go.. Great customer service by the way.

    • admin on 10/12/2012 at 20:00

      Please email me a screen shot ( or take a photo of your screen) of the error. Email : sales@skpang.co.uk

  8. Andy on 20/12/2012 at 02:14

    I’m having issues with the yellow north.

    I wasn’t sure which board I had so i checked with “cat /proc/cpuinfo” and got back 000f which is the 512mb version 2 I believe.

    I ran the rev2 python code and it wouldn’t work and got stuck at line 23:
    GPIO.setup(27, GPIO.OUT)

    I ran the rev1 code and it worked except for the yellow north.

    I’ve tried to get GPIO pin 21/27(now not sure what to call it!) independently but I can’t.
    I’m now starting to think that this pin is fried.

    • Robert on 02/01/2013 at 17:18

      I had the same problem with the yellow north i don’t know why the problem is there but i used this diagram (http://www.raspberrypi-spy.co.uk/wp-content/uploads/2012/09/Raspberry-Pi-GPIO-Layout-Revision-1-e1347664808358.png) and chose another three ports and updated the code both pyphon and c versions and it worked perfectly

      The only other thing i noticed was that the c version had the CPU stuck at 100% whilst the pyphon version barely registered as expected.

    • Paul on 24/01/2013 at 12:06

      Sorry to repeat this message but think I replied in the wrong place.
      Have successfully loaded and run the Demo Software in C but now want to use the Python version so I can make adjustments to the program (one programming language is more than enough).
      Seems GPIO something not loaded so ran this ok
      “sudo apt-get install python python-dev python-pip”
      But this
      “sudo easy_install -U distribute”
      caused a “do not know Easy_install command”
      And this
      “sudo pip install rpi.gpio”
      also came up with an error.
      My board is B rev 1 and running Debian.
      Can someone please help a newbie.

      • andy on 20/03/2013 at 22:45

        You should try doing “sudo apt-get update” first, then follow the instructions again from the beginning

  9. William Cook on 15/03/2013 at 22:15

    Dear Sir,

    I had problems with the yellow north led not working on “traffic_light_rev_2″ written in c. The #define YEL_NORTH (1<<21) had been changed to (1<<27) but two more changes are required:-

    // LEDs test GPIO mapping:
    "GPIO21 = LED" must be changed to "GPIO27 = LED"

    and in

    void setup_gpio(void)
    "INP_GPIO(21); OUT_GPIO(21)" must be changed to
    "INP GPIO(27); OUT_GPIO(27)

    Then the Yellow North led operates correctly.

    Best Regards
    Bill Cook

  10. pauljoanss on 04/04/2013 at 17:30

    Have run Python version successfully using instructions direct from Linix prompt (using wheezy).
    Have then opened User interface (startx)and then file manager. Opened file from here and tried to run but message as follows:
    “RPi.GPIO. SetupException: No access to /dev/men. Try running as root!”
    Again using file manager there does not seem to be a /dev/men folder.
    Very new to this stuff and trying very hard to understand the basics. Do not want to just do everything by blindly following instructions.
    Regards

  11. RB on 08/10/2013 at 22:27

    It would be nice if the Python sample code ran GPIO.cleanup() on exit (e.g. by catching the KeyboardInterrupt exception) – otherwise when you hit Ctrl-C to stop you invariably have some LEDs stuck on.

    Also the code doesn’t use GPIO.setwarnings(False) which may be confusing for beginners if they attempt to re-run the sample.

  12. Cameron Tawn on 05/01/2014 at 10:23

    how are you supposed to connect the east side yellow led?