The Raspberry Pi and the book monster – Session 2

The children from all three schools seemed very excited about the second session.

We recapped session one and many of the children were able to recall several of the acronyms we discussed and were given the task of setting up the Pi independently for this session.

We revisited the GPIO pins and discussed the fact that these pins were programmable so that they could act as outputs or inputs and we discussed inputs and outputs for devices in our world such as burglar alarms, traffic lights, remote controls, etc.

second circuitWe created a similar circuit to the one we did in session one but this time we used a GPIO pin rather than the 3.3v pin. When we did this the LED did not light up and we discussed that we had to programme how the GPIO pin we were using worked.

We used the command sudo idle from the command line to launch the program Python. The sudo command is important here and means Super-User DO allowing us to directly control the GPIO pins.

We created a new python file called Lights v1 and then looked at the following code.

import RPi.GPIO as GPIO ## Import GPIO library
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO4 (Pin 7) to OUT
GPIO.output(7,True) ## Turn on GPIO4 (pin 7)

The ## is very important when coding in Python as they do not represent any code but allow the programmer to make a comment about the code and what it does. This is very useful when the code does not work and it needs to be debugged. It is particularly useful when the code is shared with other coders as it allows them to understand what each line of code is designed to do. We have highlighted this in red here for clarity only.

  • The first line ensures the Pi can use the commands to control the GPIO pins.
  • The GPIO pins have two modes of identification. For example, GPIO4 can also be referred to as pin 7.  The second line tells the Pi the naming system we will use to identify the pins.
  • The third line tells the Pi the GPIO pin we are using (pin 7 also known as GPIO4) to act as an output
  • The fourth line turns the pin on and sends an electrical current through the pin.

The children were aware of the importance of syntax when coding and were very careful not to make any mistakes. They then saved their Python file and ran it. The LED came on and the satisfaction on the faces of the children was clear to see.

 

The last activity of the session was to try and get the LED to flash. To do this we had to use the time library and the sleep command. Below is an example of the code the children created to make a flashing LED in a file they called lights v2.

import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import ‘time’ library. Allows us to use ‘sleep’
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO4 (Pin 7) to OUT
GPIO.output(7,True) ## Turn on GPIO4 (pin 7)
time.sleep(5) ## wait 5 seconds
GPIO.output(7, False) ## Turn off GPIO4 (pin 7)
time.sleep(5) ## wait 5 seconds
GPIO.output(7,True) ## Turn on GPIO4 (pin 7)
time.sleep(5) ## wait 5 seconds
GPIO.output(7, False) ## Turn off GPIO4 (pin 7)
time.sleep(5) ## wait 5 seconds
GPIO.cleanup() ## resets GPIO pins

All the groups were successful in getting their LED to flash

About Paul Scott

Curriculum Innovation manager working strategically with local, regional and national partners ensuring the service’s provision continually evolves to meet the needs of schools, the local community and businesses.
This entry was posted in Mr Scott. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *