- 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?
In this session we are going to get the Pi-Car working through Scratch which is a tool that comes installed on the Raspberry Pi. It is a great way of learning programming without having to write lines of code as it is all done graphically.
To do this you need to use the Pi-Cars software that is available on the Pi-Store as detailed here. Or buy one of our SD cards that has it installed on already. If you cannot do any of these there are some detailed instructions for how to create and run the Python script that communicates with the GPIO pins – these are available here.
If you have any problems just leave a comment or email firstname.lastname@example.org.
- Lets get started
Open up Scratch by double clicking on the Scratchet icon on the desktop. This will start Scratch up and also the Python program that will listen to Scratch and send it onto the GPIO pins.
When you double click on this it will open up an example Scratch file that enables you to move the car around with the arrow keys. This is explained in this post.
Create a Scratch program
To learn about how to use Scratch the following instructions show you how to create something similar to the example file from an empty Scratch file. This will show you how some of Scratch works and how to use it.
Alternatively you can just start modifying the example Scratch file that opens when you click on the Scratchet to make the Pi-Car do different things – just save it as a different name so you can always go back to the original file that works.
So create a new empty Scratch file and then the first thing you need to do is let Scratch know that you want to communicate with something outside of Scratch itself.
To do this click on the blue sensing button in the window at the top left of Scratch then right click on the second item from the bottom. This should pop up a screen that gives you the option to ‘enable remote sensor connections’:
When this is done it should inform you that Remote Sensing is Connected. You can also click back on the terminal window and see that it is now connected and waiting for information from Scratch:
What we now need to do in Scratch is have a way of setting the GPIO pins on and off. The way this is done is by using things called variables. These will be sent by Scratch to the picars python program that is running which will then either turn the relevant GPIO pin on or off. When the variables are set to a 1 they will turn the pin on, when they are set to a 0 they will turn the pin off.
To create a variable click on the variables tab and make variable:
The names of the variables that the picars program is expecting are as follows:
So when you create a new variable name them exactly as above – make sure you do not have capital letters and the names should be exactly the same. This should mean you end up with four variables:
We now need to be able to set the variables based on certain triggers or in response to some action. If you know how to use Scratch you can choose any of the controls that are available. If you don’t know Scratch click on the control menu in the top left box and click and drag the box that says ‘When space key pressed’ into the stage area. Do this twice:
Note that the top control has been changed through the drop down menu to say when a key is pressed as opposed to space bar.
You can now set your variables to turn the GPIO pins on and off. To start with click on the variables button in the top left window and then click and drag the button that says ‘set gpio-output1 to 0’ and dock it into place underneath your control that says ‘when a key pressed’:
By default it will set the variable to ‘0’. We want to set it to a ‘1’ which will turn the GPIO pin on so click where it currently has a ‘0’ and enter a 1. We could try this now with the Pi-Car but before we do we will want a quick way of turning the GPIO pin off so the Pi-Car does not run away.
To do this click and drag the ‘set gpio-output1 to 0’ out and dock it under the ‘when space key pressed’. Do this four times and in the drop down box for the others set it to gpio-output2, gpio-output3 and gpio-output4 .
So try it out! Press the a key which should make the car go backwards and the space bar should stop the car going backwards. If you look in the picars program that should still be running you will see it print out that it has received an update from Scratch and will be changing the GPIO pin whenever the a key or space bar key is pressed:
Press the space bar and you should see the variables return to 0 and the car stop moving backwards. Try changing the variable that is turned on when the a key is pressed and work out which one results in which movement of the car.
So there you have it you have moved the Pi-Car from Scratch and you now have the building blocks to do much more complicated things in Scratch where you can use any of the control loops to turn the variables on and off.
If it hasn’t worked, don’t despair leave a comment or email to email@example.com make sure you mention which bits (if any!) have worked and which not.
You can then see which number results in which direction on the Pi-Car and then use the variables in any Scratch control loops and reacting to anything that you want.