Nov 022012
 

Download Python Gertboard Programs

I’ve been having a go at producing Python 2.7 versions of some of the Gertboard test suite. It’s partly as a learning experience for me and one which I hope will help others too.

Which GPIO? Why not both?

There’s two different ways to handle GPIO (General Purpose Input Output) in Python at the moment…

  • Ben Croston’s RPi.GPIO
  • Gordon Henderson’s WiringPi (Python Wrapper by Gadgetoid)

The ultimate aim is to have versions of the Python programs using both systems. As a file naming convention, we’ll take the name from the original Gertboard program and add -rp.py for RPi.GPIO and -wp.py for WiringPi.

I had a bit of trouble getting WiringPi for Python up and running, so when I wrote the motor program, I used RPi.GPIO, which I’ve used before. It’s easy to use and is now included in the Raspbian image. So if you’ve got a recent image, you’ve already got RPi.GPIO installed.

When I’d got that working reasonably well, I thought I’d have a go at installing WiringPi for Python again. This time the installation worked (but not as easily as would be nice). So I decided to test out the use of WiringPi for Python with a slightly easier program. I’ve replicated the leds program as faithfully as I can.

The idea with both of these Python programs, was to make the user’s experience as identical as possible to what would happen if they ran the original Gertboard test program. I’ve tested both programs against the originals and they are as close to identical as I can make them.

Fair Comment

I’m sure people will have comments about programming style, etc. (people always do) and I’m also sure that there is plenty of room for improvement. I welcome that, as long as you’re nice and constructive. :)

I’ve put lots of my own explanatory comments in the script to try to explain what each section does. This is with a view to other people learning too.

It’s very interesting to me that the Python leds program can be written in ~40 lines, whereas the leds.c C source code takes a lot more lines than that. I don’t know if that’s the way it is between C and Python, or if the C program was written to be friendlier for newcomers to understand, but it looks the same at the other end, from the point of view of the user.

Fully functional

I’ve used functions, which is something fairly new to me. I’m starting to see the power of them. When I first wrote the scripts, I used several consecutive loops, but then thought “these loops are basically the same. What about if I had one common function and used ‘function arguments’ to handle the basic differences in the loops?” It seemed to work out OK, but obviously it makes the code slightly harder to read and understand.

leds-wp.py

leds-wp.py is as close a copy as I can make it, uses wiringpi for python. To make use of it, you will need to have wiringpi for python installed. You can install it with…

sudo apt-get update

sudo apt-get install python-dev python-pip

sudo pip install wiringpi

If you don’t have WiringPi installed, or have trouble with it, use leds-rg.py, which is the RPi.GPIO version.

motor.py

motor-rg.py uses RPi.GPIO and software PWM, which is not quite as smooth as the hardware PWM used by the original, but it works. It works better in the August 2012 Raspbian than the September version. Possibly to do with the flexible overclocking messing around with the timings. I hope to do another version with hardware PWM once I work out how to do it.

Update 5 November 2012: I have now implemented the hardware PWM in motor-wp.py

You can download these files from here…
Download Python Gertboard Programs

The main download page with instructions is here…
https://raspi.tv/downloads

And if you want to discuss these programs or porting others in the Gertboard test suite into Python, then either leave a comment below or you might like to visit this thread on the Raspberry Pi forums.

Update 3 November 2012 ~15:00: Just added leds-rg.py – leds program using RPi.GPIO
Update 3 November 2012 22:02: Just added ocol-rg.py and ocol-wp.py – open collector darlington array relay triggering program for both GPIO methods.
Update 4 November 2012 15:30: Just added buttons-rg.py – buttons program with RPi.GPIO
Update 5 November 2012: I have now implemented the hardware PWM in motor-wp.py

  13 Responses to “Gertboard software in Python 2.7 – part 1 – motor and leds programs”

  1. Thank you very much for producing the LEDS program. (I found the C offerings from Gert impossible to follow.) I have now been able to test my newly built board – the LEDS all light up correctly – so I will be able to start writing a few programs of my own to control them.

    One small problem. I’m using RPi.GPIO-0.4.1a and at the end of the run I get an error message:
    Line 48
    GPIO.cleanup()
    AttributeError: ‘module’ object has no attribute ‘cleanup’

    What is causing this message.

    I can only run the program from the terminal with sudo python leds-rg.py rather than from IDLE. Is this right?

    • Firstly you’re welcome :)

      The cleanup is a useful feature that resets all the ports to input at the end of the run. What this means is that they are effectively “reset” so you won’t get a warning next time you run a program that the ports are in use by another program. It’s just a clean way of ending it. It’s not essential. You could try commenting it out.

      I wonder if the latest version uses a different cleanup process? I think I have the next most recent version.

      If you sudo chmod +x leds-rg.py you should be able to run the code anywhere. But using the GPIO requires sudo priviliges, so you’ll need to start idle using sudo. I don’t know how idle starts (I don’t use it), but just stick a sudo in front of whatever you type to start idle and it will probably work.

      • I can see the reason to use cleanup() but not understand why I get the error message. Have I got the same library as you are using?

        • According to dpkg -l | grep -i 'gpio'

          I’m using 0.4.1a-1, but if you look here…

          http://pypi.python.org/pypi/RPi.GPIO

          …the cleanup function is still documented

          # to reset every channel that has been set up by this program to INPUT with no pullup/pulldown and no event detection.
          GPIO.cleanup()

          So I’m a little puzzled by this.

  2. […] was looking for a Rev 2 Pi, as I wanted one to see if the revision checker function of the Python Gertboard programs worked […]

  3. I’m trying to use this to teach my son python. He’s made the gertboard and is really excited now. This resource is fantastic as I don’t want to try to teach him c as he’s only 9.
    We have the leds program working and are now working on our own lie detector which needs the atod program.
    This is where we need a little more help. We have followed the installation instructions above to the letter and everything worked. We have a new copy of the raspian O/S, but when we run any of the wiring pi based programs we get the following error.
    File “atod.py” line 9, in
    import spidev sys
    ImportError: No module named spidev
    Can you help please? Google has failed me.

  4. Sorry I’ve found all the answers in the readme that came with the software!

    • No problem Olly, there will be some fuller documentation at some point very soon. The ADC and DAC chips need SPI interface and the Python SPI driver (spidev) needs to be installed before you can use them. As you say, the details for this are in the Readme, but perhaps not very prominent. :)

  5. Just what I’ve been looking for!

    Could I take your hardware pwm motor script and use tis with the L298N board I’ve just bought?

    • Give it a shot. If it uses the same kind of PWM it will work. I looked up the datasheet, but it didn’t make me much the wiser to be honest. I don’t think there’s any harm in trying though. It could just work :)

  6. […] it came to modifying it I am not very familiar with C. So I downloaded the Python code available here from raspi.tv and it worked great! I was able to modify it and made some of my own […]

  7. Thank you for the code! I tried the python script and also the C code. When running both scripts I noticed that the 4th led lights up on the start of the scripts and keeps shining bright till the end of script. Could it be that I have wired my board incorrectly, or could it be that my board has a defect?

Leave a Reply