Physical Computing with Scratch and Python...Dec 05, 2016 · session 5 : physical computing with...
Transcript of Physical Computing with Scratch and Python...Dec 05, 2016 · session 5 : physical computing with...
session ..... : session title .....
white space
Session 5 :
Physical Computing with Scratch and Python
: : :
wiwMK Academy : STEM Workshop
Compiled by: Nhamburo Ziyenge, MInstP., AMIMA
November 15, 2016. Updated: December 6, 2016
[ Use of the Raspberry Pi to Promote Community Computing Literacy ]
[ Model Community Computing Hub ]
1 / 20
session 5 : physical computing with scratch and python
GPIO Pins
The objective of this session is to implement a traffic light system both in
Scratch and Python.
This requires an understanding the layout of the GPIO pins as well as the code
that allows activation of the different pins.
Considerable care is required in order to safely connect devices to the GPIO pins.
Randomly connecting devices to the GPIO can easily damage the Raspberry Pi.
There are 40 GPIO pins on both the Raspberry Pi 2 and Raspberry Pi 3 (see
illustration on the next slide)
The pins have physical layout numbers starting from 1 (top left) and 2 (top
right) to pin 40 (bottom right) – with all the odd numbered pins on the left and
the even numbered pins on the right.
In the first session we used pins 2 (or 4) (tops right) for providing 5V supply to
the touch-screen display and pin 6 (third from the top on the right)
2 / 20
session 5 : physical computing with scratch and python
Raspberry Pi GPIO Pin Layout
3 / 20
session 5 : physical computing with scratch and python
GPIO LED Test
To test that an LED is working, it can be connected directly to a Raspberry Pi
power supply pins such as the 5V as illustrated here – Note the long and short
legs of the LED.
4 / 20
session 5 : physical computing with scratch and python
LED Test Circuit Schematic
The LED test circuit can be represented schematically as illustrated here.
5 / 20
session 5 : physical computing with scratch and python
Ground PinsThere is a total of 8 ground GPIO pins – namely, pin 6, 9,
14, 20, 25, 30, 34, and 39 – all shaded in the illustration.
In the LED test slide above, pin 6 is illustrated as the
ground pin that connects to the shorter leg of the LED.
If pin 6 is already being used for connecting the 5V supply
between the Raspberry Pi and the touch-screen display,
then any of the other ground pins can be used instead.
We cannot implement the traffic light system by
connecting the LEDs directly to the power supply pins as
illustrated in the LED test slide above – because that
would mean the LEDs would be switched on all the time.
For the traffic light system, we need to use one of the
GPIO pins that can be configured to be an output or input
pin from within the Raspberry Pi.
6 / 20
session 5 : physical computing with scratch and python
Configurable GPIO Pins
Any of the other pins – that are not power supply
pins or ground pins can be configured to work as
output pins or input pins.
Here we will use pins 36 (for Red), 38 (for
Amber) and 40 (for Green) as output pins
A GPIO pin configured as an input pin can also
be used to detect the closure of a switch
connected between one of the 3.3V pins and
ground – when the switch is open, the GPIO
reads a HIGH – but when the switch is closed, the
GPIO reads a LOW value.
We will use pin 12 as the input pin with a switch
connected to detect when the switch is pressed.
7 / 20
session 5 : physical computing with scratch and python
Breadboard Wiring
Get your wiring
checked before
turning the
Raspberry Pi on.
8 / 20
session 5 : physical computing with scratch and python
Scratch Traffic Light Code
Scratch uses broadcast blocks to
communicate with the GPIO pins.
The first broadcast you need is
gpioserveron which activates the
GPIO functionality.
After activating the GPIO
functionality, each of the traffic light
LEDs can be configured as an
output.
In our case, we are using pins 36, 38
and 40 to connect to the Red,
Amber and Green LEDs respectively
– as illustrated in the previous slide.
To configure a pin as an output pin,
use a broadcast block illustrated
below (for pin 36):
9 / 20
session 5 : physical computing with scratch and python
LED On / OffTo switch ON pin 36 (for example)
broadcast a gpio36on message –
after broadcasting the config36out
signal.
To switch OFF pin 36 (for example)
broadcast a gpio36off message –
after broadcasting config36out.
The two scripts shown here illustrate
switching pin 36 ON and then OFF.
Use these two scripts to test that the
red LED does switch ON and OFF in
response to pressing the green-flag.
Repeat for pins 38 (amber LED) and
40 (green LED).
10 / 20
session 5 : physical computing with scratch and python
LED On then OFF
This script switches ON the red LED (pin 36) and waits 3 seconds before
turning it OFF.
After that if stops when it reaches the “stop script” block.
11 / 20
session 5 : physical computing with scratch and python
Repeated Flashing LED
Here we are switching pin 36 (red LED) ON, waiting for 2 seconds, then OFF
and waiting another 2 seconds, then ON again etc.
The flashing is repeated 10 times.
12 / 20
session 5 : physical computing with scratch and python
Flashing LED : Forever
In this case the LED will keep flashing, waiting for 2 seconds between ON and
OFF.
The LED will keep flashing until the script is interrupted.
13 / 20
session 5 : physical computing with scratch and python
Traffic Light Diagram
This diagram illustrates the electrical connections of the three LEDs to pins 36
(red LED), 38 (amber LED) and 40 (green LED) used for the traffic light
system.
14 / 20
session 5 : physical computing with scratch and python
Traffic Light Sequence
This is the complete
traffic light sequence.
Red
Red / Amber
Green
Amber
15 / 20
session 5 : physical computing with scratch and python
Access Python IDLE
16 / 20
session 5 : physical computing with scratch and python
Create New Python FileThe >>> prompt provides direct
access to the Python interpreter.
In stead of typing Python commands
directly at this prompt, we want to
create a new Python script in which
to create the code to drive the LEDs.
Create a new file by clicking on File
on the main menu and save the file
as pythongpio.py
Make sure the extension is .py
otherwise the interpreter will not
recognise it as a Python file.
To configure the GPIO pins follow
the steps on the following slide.
17 / 20
session 5 : physical computing with scratch and python
Python GPIO Configuration
It is important to know the location
where the file you create is located.
To configure the GPIO pins 36, 38
and 40 as output pins, copy the
Python code on the right into the
newly created file then save the file.
To run the file you need to open the
command terminal (see next slide)
and navigate to the directory where
you saved the file.
The while loop that flashes pin 36
ON and OFF.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
GPIO.setup(40, GPIO.OUT)
while True:
GPIO.output(36, GPIO.HIGH)
time.sleep(1)
GPIO.output(36, GPIO.LOW)
time.sleep(1)
18 / 20
session 5 : physical computing with scratch and python
Command Line Terminal
19 / 20
session 5 : physical computing with scratch and python
Complete Traffic Light Code
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
GPIO.setup(40, GPIO.OUT)
while True:
GPIO.output(36, GPIO.HIGH) # Red LED ON
time.sleep(3)
GPIO.output(38, GPIO.HIGH) # Amber LED ON with Red ON
time.sleep(1)
GPIO.output(36, GPIO.LOW) # Red LED OFF
GPIO.output(38, GPIO.LOW) # Amber LED OFF
GPIO.output(40, GPIO.HIGH) # Green LED ON
time.sleep(3)
GPIO.output(40, GPIO.LOW) # Green LED OFF
GPIO.output(38, GPIO.HIGH) # amber LED ON
time.sleep(1)
Technical support and
guidance will be
provided to run the
code and to debug it as
necessary.
A separate session will
be devoted to
familiarisation with the
Raspbian Linux
platform.
20 / 20