Software

We can write the program to control the simulator in a variety of languages. Using the gpio program we can do it in a simple shell script (or even call it from languages such as php, perl, etc.) and using the wiringPi library then we can call it from C or C++.

As a bit of an exercise, I have written 2 versions – one using the standard BASH and the other in C. You can get the source code here:

The bash code (tuxx.sh) will run directly, but to make the C program run, we first need to compile it, then run it as root via the sudo command.

Setup:

mkdir ~/tuxx
cd ~/tuxx

To get and run the Bash version:

wget http://project-downloads.drogon.net/files/gpioExamples/tuxx.sh
chmod +x tuxx.sh
./tuxx.sh

To get and run the C version:

wget http://project-downloads.drogon.net/files/gpioExamples/tuxx.c
cc -o tuxx -Wall -I/usr/local/include -L/usr/local/lib tuxx.c -lwiringPi
sudo ./tuxx

to get the latest via GIT:

git clone git://git.drogon.net/gpioExamples
cd gpioExamples/tuxx
make
sudo ./tuxx

Do examine the code (both the C and Bash versions) to understand how they work and how to adapt them for your own projects

Enjoy!

-Gordon

Comments

Software — 47 Comments

  1. Hi George, thanks a lot for sharing your project! I expect to get my Pi this week, and was looking for newbie projects like this. Just a question I have, this ‘gpio’ command, does that come shipped with the standard Pi distributions based on Arch Linux and Debian?

  2. Hi Gordon

    Had fun putting this together as my first little project. There’s a bug in the Bash version of the code though. Was that deliberate to make sure people read it and understand it?

    In the waitButton() subroutine, the code needs to be reading 0 to keep in the while loop rather than 1, otherwise it’ll instantly break out unless you keep your finger on the button!

    Great work though – and thank you for helping me get my RPi doing something useful. ๐Ÿ™‚

    • The way I wired it up was to use one of the I2C pins which has an on-board pull-up resistor. So that pin will always read 1 when the button is not pushed. the button shorts the signal to ground, reading 0 when it’s pushed.

      So I think the code is right…

      At least it works OK on my setup!

      How have you wired the button in?

      -Gordon

  3. In the distribution Arch works perfectly. You must install the basic development tools with pacman -S base-devel to compile the library.

    • That’s good to know, thanks. I’ve been using Debian for 18 years, so I’m a bit out of touch with other distros!

      -Gordon

  4. really enjoyed doing this as my first GPIO project, no problems with the bash code.
    thanx! for sharing… now i’m gonna move on to the ladder game ๐Ÿ™‚

  5. Hi, great tutorial, I’ve wanted a use for the RPi. Only issue is when I stop the program, the LED’s are still on!

    • Yes – the GPIOs will stay the last you you left them! You may want to write a 2nd program that turns all outputs off, or resets them all to inputs.

      Or… Read up about signals so that if you stop your program with Control-C, it can be caught and in that function that catches it, you can reset all GPIOs to inputs, etc.

      -Gordon

  6. Great tutorial.
    I have tuxx program problem. Both bash an C hangs on Wait for Button. I chcecked that gpio 8 always has 1 even if I push button. Are you sure that button is conected to right pin?
    I have RPi B board version 1.0B1.1

    • Yes, pretty sure! The button ought to be connected to 0v on one side, and on the other side (normally open), it gets connected to wiringPi pin 8 which is pin 3 on the edge connector.

      Try connecting a wire from that pin to 0v (bypass the switch) and read the input – it ought to read 1 with nothing connected, and 0 when connected to 0v.

      -Gordon

        • I’m sort of out of ideas… Could you have a Rev 2 board? How old was your Pi? If it’s a Rev 2. then you’ll need a very new version of wiringPi as some of the pin numbers changed…

          -Gordon

          • Those little push button switches are push to make – I don’t like them very much, but they’re cheap and cheerfull!

            -Gordon

  7. Great job!
    What I looked for, to begin working on RaspBerry.
    Look easier to understand very deep how works the beats.

    -Launix

  8. Really impressive, coming form arduino, I can now unleash the monster programmer inside me into this approximately 800x more powerful machine. I was the third in this big city that i live in to get the raspberry pi.

  9. I am getting a pair of full sized traffic light heads would it be possible to modify the control gear to switch 24v lamps on and off using relays?

    • No reason why not. You’ll need some sort of buffers to drive the relays from the Pi – e.g. uln2803’s. Sounds like a fun version of the Tuxx crossing ๐Ÿ˜‰

      -Gordon

  10. Got it working no problem but the cycle is all wrong for New Zealand traffic lights. Ours go green / yellow / red, green man, red man flashing, then red man solid and directly red to green for traffic. Took a little fiddling but I got the script customised just how I want it now.

    • Having lived in NZ for quite some time, I too thought the phasing was strange but then I do remember the flashing ambers back in Blighty! Will see if the kids notice when (if) I let them try it.

      Great work Gordon.

  11. Why will this not work? Your TUXX.SH program works. I can type gpio write 1 0 and that works. WirinPi has been installed but …….. this
    import os
    import sys

    import wiringpi
    io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_PINS)
    wiringpi.wiringPiSetupGpio
    wiringpi.pinMode(18,1)
    wiringpi.digitalWrite(18,1)
    #wiringpi.digitalWrite(1,0)
    #wiringpi.digitalRead(1)

    won’t work? What am I doing wrong with Python> All help appreciated!

    • Hi,

      I’ve no idea why it’s not working. I know nothing about Python. Only C here.

      However – are you running it with sudo? You’ll need to if you’re using the wiringPiSetupGpio call.

      -Gordon

      • if you do
        io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_PINS)
        you create an object called “io” with the properties of GPIO as defined within wiringpi.
        that means every subsequent call should be like:
        io.wiringPiSetupGpio() and so on
        that way you set up the object you just created

        – anatol

  12. Hi Gordon,
    i have compiled tuxx after your instructions, but there will be no message in the terminal that the program runs.

    root@raspberrypi:/home/pi/Desktop# cc -o tuxx -Wall -I/usr/local/include -I/usr/local/lib tuxx.c -lwiringPi
    root@raspberrypi:/home/pi/Desktop# ./tuxx
    root@raspberrypi:/home/pi/Desktop#

    The terminal should have output Setup … or?
    Have you an idea?

    • Yes, it should have printed Setup… followed by Waiting…

      I have just tried it myself – (I fetched the code via wget and via git) and it worked OK for me:

      # cc -o tuxx -Wall -I/usr/local/include -I/usr/local/lib tuxx.c -lwiringPi
      # ./tuxx
      Setup ... OK
      Waiting for button ... ^C

      I’d not normally be root to do this (other than sudo ./tuxx) but I was root to make sure I did the same as you…

      So I’m somewhat confused that it didn’t print anything. At this point, I’d stick a printf in the code in the main() section just to see if it starts up at all, but it’s really a simple program – hard to see why it would fail to start at all…

      -Gordon

  13. Hi, thanks this was a great tutorial and all worked right to the end the only thing that has left me stumped is say you turn off your pi then back on again, how do you run the script again, maybe i’m just stupid but i’ve tried from the command promt and it just says no file or directory and if I use X screen and go to the exucutable file, the dialog box comes up and says execute or execute in terminal, either one i pick i get no error message the diolog box just disapers and nothing happens, sorry its probaly something simple, just not as much as me apparently, the only way around it is to delete everything and repeat the last step all over again???
    thanks,,

    • Is it the tuxx.sh script you’re running? If so, you should just be able to type it in a command window the same way to did in the first place..

      It’s not really designed to be “clicked” from a GUI though – start a terminal and type ./tuxx.sh

      If this isn’t what you’re after, then you’ll need to give me some more details though.

      -Gordon

  14. Gordon, excellent work! Thanks for sharing and to get me started. It all works well with me in C.

    While I try to rewrite the same idea in C++, I am having hard time to get my files to compile; when it cannot find the “operator new” in dynamic memory allocation. Could you please explain the contents of your Makefile so that I know how to redirect the compiler to the correct header files ? Appreciate it.

  15. Never mind, I have figured out myself from “google”. I need to include the -lstdc++ linker in my gcc command line. Thanks anyway.

  16. Well, great tutorial for my first project on the board.

    I tried your example and it works fine except.

    However, trying to go a little furhter, i wired a Led to each GPIO pin on connector P1. There I discovered something odd.
    I put all the GPIO pins ( 0 to 31 )in output mode
    I am able to write 0 or 1 to all these GPIO.
    However, P1 – GPIO 4 / CP Clk 0 seems to be stuck high – i.e. led always stays on.
    I have a PI rev2
    Is this normal?

    • Just make sure you have the right pin. wiringPi pin 4 is BCM_GPIO 23 and is on physical connector pin 16 on P1.

      The pin you might be refering to as CP Clk 0 is BCM_GPIO 4 and wiringPi pin 7 (physical pin 7 on P1)

      The Pi revision is not an issue for either of these pins. Just double check your connections and pin assignments. If using the gpio command, then use the -g flag to force it to use BCM_GPIO pin numbers, otherwise it will be using wiringPi pin numbers.

      See this page for the pin designations: http://wiringpi.com/pins/

      -Gordon

  17. Hi Gordon,

    Would it be possible to write a version of this code in Python?
    I would like to deliver this project to year 6’s.
    Many Thanks,
    Michelle

    • Sure – However I’m not a Python programmer. I only do BASIC and C. There are many examples out there though.

      -Gordon

  18. Lovely little project – very easy to follow and I enjoyed every minute of it! Thank you for taking the time to write it up ๐Ÿ™‚

    A few notes:
    – Like one of the previous commenters mentioned I had to cut a bit of wire off a resistor to adapt one of the male-to-female wires to a male-to-male wire, but that was an easy hack. The skpang raspberry pi starter kit A was excellent for this.
    – I ran into a weird bug that made two of the lights blink but not light up. Turns out it was because I was also running the LEDBorg module – a quick “sudo rmmod ledborg” sorted that quickly. It is an edge case but as it had me scratching my head I thought I would share ๐Ÿ™‚

    Thank you again – it is tutorials like this that make electronics approachable to a complete noob like me ๐Ÿ™‚

    • Not sure I know what you’re after here, but look at the ladder board stuff and the tuxx.sh program – that’s a simulator for a UK pelican crossing using the ladder board.

      -Gordon

  19. Gordon,
    Just ran across your tutorial. Wanted to say thanks for sharing this. I enjoyed every step. I’ll be going over the same steps with my boys (13, 9) this weekend.

    Thanks again!