Вы находитесь на странице: 1из 14

Lets get

physical
Scratch +
Python

Workshop outline
Why physical computing
Build simple circuit using Raspberry Pi
Control an output using Scratch on Raspberry Pi
Create more complex circuits and programs
You will need:

#picademy

rpf.io/learn

Physical Computing
Enables creators to connect the virtual world with the physical, great for engagement.

#picademy

rpf.io/learn

Simple Circuit
Your Raspberry Pi can act as the power supply for simple circuits, we can use this test our
hardware is working.

#picademy

rpf.io/learn

Simple Circuit
Your Raspberry Pi can act as the power supply for simple circuits, we can use this test our
hardware is working.

#picademy

rpf.io/learn

Inside a breadboard

#picademy

rpf.io/learn

Switching Circuit
Your Raspberry Pi can act as a switch in the circuit, which can be controlled in software.

#picademy

rpf.io/learn

Switching Circuit
Your Raspberry Pi can act as a switch in the circuit, which can be controlled in software.
Connect the positive leg of the LED to GPIO pin 17 on your Raspberry Pi

#picademy

rpf.io/learn

Scratch on the Raspberry Pi


Open Scratch and activate the GPIO server.

gpioserveron
#picademy

rpf.io/learn

Blinking LED
Setup
your
pin
(17)
as an
outpu
t.

config17o
ut

Switc
h pin
17 on

gpio17on

Switc
h pin
17 of

gpio17of

Pause
Loop
forev
er

#picademy

Combine these blocks to create a program


which continually flashes your led on and
of.
Can you:
Flash your LED at diferent speeds, how
fast can you make it flash?
Can you make a dot (short flash) and
dash (long flash) and use these to
make a distress beacon. S(...) O(---)
S(...)
Add more LEDs to create a set of traffic
lights and program an appropriate
sequence.
Use another form of output such as a
buzzer or motor.
Incorporate physical output into your
other scratch projects
rpf.io/learn

Reflection

#picademy

rpf.io/learn

Example Programs
# Flashing LED

This Python code is equivalent to the


previous Scratch blocks.

from gpiozero im port LED


from tim e im port sleep

To run the code:


1. Menu Programming Python 3 (IDLE)
2. Click File then New Window
3. Type out the Python Code
4. Press F5 to run the code (saving it
first)

led = LED (17)


w hile True:
led.on()
sleep(1)
led.off
()
sleep(1)

#picademy

The ## red comments are merely annotation


and ignored by the computer.

rpf.io/learn

Example Programs
# Flashing LED

This Python code is equivalent to the


previous Scratch blocks.

from gpiozero im port LED

To run the code:


1. Menu Programming Python 3 (IDLE)
2. Click File then New Window
3. Type out the Python Code
4. Press F5 to run the code (saving it
first)

led = LED (17)


led.blink()

The ## red comments are merely annotation


and ignored by the computer.
What do the other lines do?
Try some other values in led.blink(), what
would these do:
. led.blink(5)
. led.blink(2, 0.5)
. led.blink(0.1, 10)

#picademy

rpf.io/learn

Further abstraction
G PIO Zero includes severalabstractions for
use w ith m ore com plex hardw are:

# Traf f
c Lights
i
from gpiozero im port Traf f
cLights
i
from tim e im port sleep
lights = Traf f
cLights(9, 10, 11)
i
lights.red.on()
sleep(3)
lights.red.off
()
lights.am ber.on()

Can you create a fulltraf f


c lights sequence?
i

#picademy

rpf.io/learn

Вам также может понравиться