- Pre-driving checks
If you are here you should have set up your Raspberry Pi and also understand how to connect your Pi-Car to your Raspberry Pi. If not don’t worry:
For more information on setting up your Raspberry Pi click here.
For more information on how to connect your Pi-Car to your Raspberry Pi click here.
- What’s in this driving lesson?
This lesson shows you how to write a program to control your Pi-Car. You should not need any experience of writing a program to do this lesson and you don’t even have to know what a program is. By the end of the lesson you should be able to write a program in something called Python that will move your Pi-Car.
- Let’s get going
In order to tell your Raspberry Pi to do things – including making your Pi-Car work – you need to write a program. A program is a series of instructions that tell the Raspberry Pi exactly what to do. The Raspberry Pi will try and perform these instructions when you run the program. The format and words used in these instructions is called the language of the program. There are lots of different types of program languages, the one we are using here is called Python which comes installed on the Raspberry Pi.
To create the program we need to write down the instructions. We can use any text editor to do this but if we use the one called IDLE3 that comes with the Raspberry Pi it will help make sure you use the right format and help you to use the correct words. The idea is this means you are less likely to make mistakes and get something to work.
You can run IDLE3 by double clicking on the icon on the Desktop but we need to run it slightly differently. This is because we want to make the Python program interact with the GPIO pins to move the Pi-Car around. To do this we have to run the IDLE3 editor with extra power. This can be done by clicking on the LX Terminal window as shown below:
Into the black window that pops up type the following command and press enter.
This should open up a window as shown below (if you are accessing your Raspberry Pi remotely through VNC click here for instructions on an extra command you will need):
You are now ready to start writing the instructions for your Python program! We will start off by using what is called the ‘shell’. This is shown above when you first start IDLE3. What the shell does is execute each instruction as soon as you press enter after you type it rather than save a series of instructions that can be run later. It is a useful way of finding out quickly if you are writing the instructions correctly.
- Writing your first Python program in the shell
The first thing you need to do is let the Python program know that you want to interact with the GPIO pins. Don’t worry too much about what these command means at the moment but make sure you type them in accurately and copy the case of the instructions i.e. if they are lower case below you need to type them in as lower case.
Into the shell window type the following commands pressing enter at the end of each line:
import time, RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False)
You should now have a window as shown below. If you have typed in something incorrectly it will give you an error. If you have an error check carefully that you have added the instructions exactly as above.
Next we have to instruct the Python shell to tell the Raspberry Pi that we want to use a certain GPIO pin as an output – it is possible to use the GPIO pins as inputs or outputs so we have to specify which way we want to use it. We use the GPIO pin as an output in order to send a command to the radio controller connected to that pin. Enter the following command into the shell and press enter:
GPIO pin 12 in Pi-Cars makes the car move forwards so that is the one we want at the moment.
All that is left now is to instruct the Pi-Car to move forwards. Make sure that your Pi-Car is connected – click here for instructions on how to – and type the following command into the shell and press enter:
Your Pi-Car should now be moving forwards!
You probably want to stop your Pi-Car from moving now so type the following into the shell and press enter:
Your IDLE3 shell should now look like the following:
- Saving a Python program to be run later
Whilst using the shell is great for doing things quickly and seeing if things work it is more useful to be able to save all of the instructions into a file that can then be run all at once at a later stage. That is what we’ll do next.
Firstly you need to open up a window as opposed to a shell that you can type your instructions into. You can do this by clicking on File->New Window as shown below:
This will open up a window that you can type in. The difference between the shell and the window is that in the window each instruction will not be actioned when you press the enter button whereas in the shell it was. Add the following lines to the window:
import time, RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(12,GPIO.OUT)
Your window should now look like below:
We now need to add to the window the instruction to make the Pi-Car move forwards:
We probably don’t want out program to just move the car forwards forever so we will also need to stop it. However, if we type in the command to turn the GPIO pin off as we did earlier in the shell, the instruction it will be actioned straight away. When the file with the instructions is run the Raspberry Pi will action the instructions very quickly.
To overcome this we can tell the Raspberry Pi to wait before performing the next instruction. We do this by typing the following into the window:
The ‘1’ in brackets stands for 1 second that it will wait for before moving onto the next instruction. You can change this to any value you want including a fraction of a number such as 0.1.
After the sleep we can now turn the GPIO pin off which will stop the Pi-Car moving. Add the following line to the file:
Your window should now look like the below:
So we now have a complete set of instructions that we need to run. We can do this by clicking on Run->Run Module, or from pressing F5 as shown below:
After you click on this you will be informed that you need to save the source as below:
Click on ok and choose somewhere to save the file to. As good as place as any is to click on the home folder and then the pi folder. Choose a sensible name such as PiCar_ForwardsAndStop. It is important you do not have any spaces in your name. Your screen should look like below:
When you click on Save, the instructions in your program will be run. This should result in your Pi-Car moving forwards for a second and then stopping. You may also notice that when the program is run the shell that you used earlier starts as shown below:
And that completes the first Python driving lesson. Next time out we will be able to start making our Pi-Car do some more interesting things. If you have any problems please leave a comment or email email@example.com .
PDF version of this article – Pi-Cars and Python – Driving Lesson #1 _ Pi-Cars