This worksheet will guide you through turning an LED on and off using Python & the GPIO Pins on the Raspberry Pi.
To begin load up the geany IDE by going to the Start menu --> Programming --> Geany
Next confirm you are working on a blank file by checking the tab for this program says untitled.
Begin by adding the following to the new file replacing
Your Name Here with your name.
These two lines are comments which only we can read. In python any line which has a
# at the beginning is a comment and is never read or interpreted by the computer.
Next save the program either by pressing
Ctrl & S or the save icon in geany.
Call this program
Confirm that the program is set to save in your home folder by clicking on the house icon which has your name next to it and click save.
Your comments should now appear in the colour red.
Now we can continue on with the rest of the program. Copy out the following code.
Each line here has been commented with what the line does. You don't need to copy these as they are for you to understand what each line does.
Now you can press F5 to run your program.
But there's an error!
Using GPIO pins on the Raspberry Pi requires us to run the python program as root. To make it so our program launches as root we need to make a change to a setting in geany.
Start by clicking Build at the top toolbar at the top and then Set Build Commands and a box should pop up like the following.
We want to change the execute command to use sudo python instead.
Add sudo to the beginning and your box shuld now look like this.
Now try re-running the program. The green LED should turn on.
To close your program press Ctrl & C into the terminal window. This will close the program and reset the GPIO pins so the green should go off.
And that is it for worksheet 1!
Make a modification to your code to have the program then sleep for 1 second, turn the LED off and then sleep for another second.
To make python sleep / wait for 1 second you can use the sleep function which is:
sleep(seconds) replacing seconds with the number of seconds you wish to sleep for.