The Raspberry Pi and the book monster – Session 3

Session 3 was going to be a challenging session as we would be introducing multiple LEDs, breadboards (we did look at breadboards briefly in session 2) and programming the LEDs to work independently.

The children quickly and confidently set up their Pi and opened their last program in Python. In session 2 we had to wire an LED to the Pi but some of the jumper leads we used did not connect to the pins very securely so we substituted this with a breadboard for ease of use. A breadboard allows several electrical components such as switches, LEDs and buzzers to be connected easily to test circuits. There are different sizes of breadboard and the one below is the breadboard we used.

breadboard

 

 

 

 

 

 

It is important to understand how the breadboard is wired and the image below shows how the pins on top of the board are connected by wires internally.

breadboard wiring

 

 

 

 

 

 

 

We used the breadboard to create a simple circuit with one LED and used our lights v2 program to make it flash.

I then asked the children to make the following circuits and predict if they would work.

series circuits

These circuits are called series circuits as each component is linked to the other in a linear way. The LEDs did not light up. Without getting too technical there is not enough voltage from the GPIO pin to light all three LEDs in this way.

I then asked the children to connect the LEDS as below. This circuit is called a parallel circuit and because each LED is connected directly to the GPIO pin there is no loss of voltage and so all 3 LEDs light up.

parallel circuit

 

The children were now able to control all 3 LEDs and we discussed if we could control the LEDs individually from the one GPIO pin. Obviously we could only program the GPIO pin connected to all 3 LEDs so the answer was no. The children then discussed how they could achieve this by using more than one GPIO pin per LED and changing their code.

 

 

They very carefully created the circuit below to control all 3 LEDs independently.

breadboard multiple gpios

 

 

 

 

 

 

 

They were then asked to alter the code in Lights v2 to utilise all 3 connected GPIO pins and save this as Lights v3.

 

The initial code changed as below

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
???????
???????

became

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.setup(11, GPIO.OUT) ## Setup GPIO17 (Pin 11) to OUT
GPIO.setup(13, GPIO.OUT) ## Setup GPIO21 (Pin 13) to OUT

Below is an example of the finished code controlling all three LEDs.

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.setup(11, GPIO.OUT) ## Setup GPIO17 (Pin 11) to OUT
GPIO.setup(13, GPIO.OUT) ## Setup GPIO21 (Pin 13) to OUT
GPIO.output(7,True) ## Turn on GPIO4 (pin 7)
time.sleep(3) ## wait 3 seconds
GPIO.output(11, True) ## Turn off GPIO17 (pin 11)
time.sleep(3) ## wait 3 seconds
GPIO.output(13,True) ## Turn on GPIO21(pin 13)
time.sleep(3) ## wait 3 seconds
GPIO.output(7, False) ## Turn off GPIO4 (pin 7)
GPIO.output(11, False) ## Turn off GPIO17 (pin 11)
GPIO.output(13, False) ## Turn off GPIO21 (pin 13)
GPIO.cleanup() ## resets GPIO pins

The children then created their own code to make their own sequences of lights.

I was very impressed on how much progress the children had made in three sessions. They were now able to set up the Pi, use a breadboard to make their own circuits, connect this to the Pi and then create original code to control the LEDs to flash to their own sequences using Python.

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 *