Pi-Cars and Python – Driving Lesson #1

  • 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.

  • Starting up IDLE3

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:

Select LX Terminal

Double click on the highlighted LX Terminal icon in order to get to a command prompt

Into the black window that pops up type the following command and press enter.

sudo idle3

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):

IDLE Python Shell

Running ‘sudo idle3’ in the command prompt should bring up the shell shown above.

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.

IDLE3 with GPIO instructions

The IDLE3 shell with the GPIO instructions typed in.

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.setup(12, GPIO.OUT)

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:


GPIO.output(12,1)

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:


GPIO.output(12,0)

Your IDLE3 shell should now look like the following:

IDLE3 Shell with GPIO start and stop commands

IDLE3 shell showing the commands that turn on the GPIO pin and moves the Pi-Car.

  • 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:

Open New Window in IDLE3 Python

Open up a new window in IDLE3 to create a program that you can write instructions to

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:

IDLE3 window for Python program

Window with the GPIO setup information typed in.

We now need to add to the window the instruction to make the Pi-Car move forwards:


GPIO.output(12,1)

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:


time.sleep(1)

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:


GPIO.output(12,0)

Your window should now look like the below:

IDLE3 window with sleep and GPIO start and stop commands

Window with all the commands to move the Pi-Car forwards, sleep for a second and then stop.

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:

Run program from IDLE3 window

To run the program you have written click on Run and then Run Module.

After you click on this you will be informed that you need to save the source as below:

IDLE3 save source file option

Save source file option in IDLE3

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:

IDLE3 Save Program

Save your Python program to somewhere – /home/pi is a good place.

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:

IDLE Python shell after running program

When the program is run the IDLE shell opens to show any output from the program (there is none in this one)

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 info@pi-cars.com .

PDF Version

PDF version of this article – Pi-Cars and Python – Driving Lesson #1 _ Pi-Cars

Advertisements

About Barnaby Kent

http://www.myactionreplay.com
This entry was posted in Under the bonnet and tagged , , , , . Bookmark the permalink.

4 Responses to Pi-Cars and Python – Driving Lesson #1

  1. Pingback: Introduction to Python, constants and print statements – Driving Lesson #2 | Pi-Cars

  2. Pingback: Variables and while loops – Driving Lesson #3 | Pi-Cars

  3. Pingback: Python if statements – Driving Lesson #4 | Pi-Cars

  4. Pingback: Python while loops – Driving lesson #5 | Pi-Cars

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s