Your first ChickBot Pi program

Start up your Raspberry Pi, launch Geany and you are ready to start. We’ll be using Python, a widely supported programming language with a whole variety of uses.

Like building a house, you can get in to all sorts of trouble trying to build a roof, paint the walls and lay the foundations all at the same time. Everything starts with the foundations and the more solid they are, the better the rest of what you build will be in the future, so don’t rush these early steps.

This program will turn the small light, the Light Emitting Diode or LED, that is on the driver board on and off.

We’ll then add in the buzzer so it controls the light and plays a sound.

And then we’ll control it from the push switch that is on the corner of the board.

We will explain what all the commands do once you’ve got something up and running, for now, just play along!

Please read these instructions so you have an overview of what you are doing, then watch the video below.

So, with Geany in front of you, it starts out with a blank program that you can add to.

First of all, we need to tell the program we want to use a programming ‘Lego’ block or module that will give access to the General Purpose Input Output pins:

import RPi.GPIO

Feel free to copy & paste from this web page – the commands have to be written exactly otherwise they won’t work, so if you do experience any problems, check that what you’ve typed is what is on this page.

We also need to tell the program how we want to reference the pins – there are different numbering schemes, for these tutorials we use the Broadcom (the CPU manufacturer) numbering scheme. So we need to add this line of code:

RPi.GPIO.setmode(RPi.GPIO.BCM)

We also need to tell the program we want to use pin 3 as an output with:

RPi.GPIO.setup(3, RPi.GPIO.OUT)

Finally, we can use the command to turn on the output on or off as we want. Because the Pi starts up with pin 3 turned on, you should see that the LED is currently on. So initially, we’ll set the command to turn the LED off:

To run this program, go under the Build menu and select Execute. You will be prompted to save your program, which you can name something like MyFirstProgram.py – you can use any file name as long as you put the .py at the end so that both Geany & your Pi know that it’s a Python program. Once you’ve saved it, a terminal window will open up to show the results of your program. As we haven’t told it to say anything on screen, you won’t see much there. But the LED on the ChickBot driver board should go out.

Here’s a video run through of what we’ve done so far:

The Pi will remember the last command on an output, so you can turn the LED back on by changing the last line to RPi.GPIO.output(3, 1). Feel free to try this out as much as you like. Each time you’ll need to Execute the program. You can close the terminal windows that get opened up each time to keep things tidy.

Let’s add some sound!

There is a piezo sounder on the driver board which needs just a few lines to use:

import time
RPi.GPIO.setup(7, RPi.GPIO.OUT)

tone = RPi.GPIO.PWM(7, 440)
tone.start(50)
time.sleep(1)
tone.stop()

First we enable access to the time module so we can have a delay to set the duration of the tone. We then create a variable, tone, that gives us access to the Pi output pin 7, setting the frequency to 440Hz (which is middle C). We then start the output which requires a value for the duty cycle. The duty cycle is the ratio of the time that the output is on to the time that it is off. For the purposes of creating a tone, 50% is fine. We then wait 1 second before stopping the output.

The ‘import time’ and the ‘setmode’ section should go with the other import at the beginning of the program. The rest can be added after the LED command, so your code looks like this:

Click the Execute button to try it out. Clearly this will drive everyone crazy, especially if you change it from 1 second to a longer time, so have fun with it!

It’s a simple step from here to turn on the LED whilst the tone is being played. We’ll actually wait a second before running it all so we can clearly see the LED coming on. Alter the second block to this:

Finally, let’s get the program to wait for the button to be pressed before it starts lighting up the LED and beeping.

We’ll add a line of code that constantly checks the pushbutton on the corner of the board (which is attached to pin 2) and waits for it to be pressed before continuing. Due to the current configuration of the driver board, we are actually waiting for the input to go low, so while it is 1 or high, we keep the loop going:

RPi.GPIO.setup(2, RPi.GPIO.IN)
while(RPi.GPIO.input(2)):pass

While the button is 1, we just cycle round doing nothing. The ‘pass’ keyword doesn’t do anything, but allows us to have a blank line in a control block. As soon as the button is pressed, the input goes to 0 so the while condition is no longer met and the program carries on with the next line.

You will notice that the command ‘pass’ is indented. This is an essential part of laying out the code so don’t leave it out.

We’ll put this at the start of the main block so when the program is run, you have to press the button before the LED will come on & the tone is played.