Lab 3:
Working with GPIO
Welcome to the third lab of Term 2!
In today's lab, we will be learning about GPIO pins and how we can utilize them to control physical components.
Challenge 1: Build a simple circuit
Follow one of the given diagrams and recreate the circuit on your hardware.
Tips
Make sure to turn the Raspberry Pi OFF when you are wiring it.
Identify the positive and negative ends of your LED before plugging them in
Make sure to clarify that the jumper wires are all the same underneath the insulation, regardless of color.
Challenge 2: Write a script to make the LED blink like a heartbeat
Create a new Python file called toggle_led.py
Use the same LED circuit wiring as the previous activity.
Import the LED from gpiozero
Declare a led variable to match your GPIO connection.
Make a while True loop that continuously checks for user input: ( response = input(“on or off”) or similar.
Create two functions, (or one that handles both conditions ), that will turn the LED off if it receives “off” as an input, and vice versa.
Inform the user to try again with proper usage, if neither value is true.
Reference
Breadboards
Breadboards are components that you can use to create circuits. They allow you to connect the wires and other components without any soldering. All you need to do is plug the connections into the holes in the breadboard based on how you want to connect each of the components.
Parts of a Breadboard
Terminal Strips: (the blue or columns in the image on the left)
Power Rails: (the pink or rows in the image on the left) These are usually marked with (+) or (-) but they are just guides rather than functions.
Components connected into each of these strips or rails will share the same current.
GPIO Pins
Short for general purpose input/output, the GPIO pins on the Raspberry Pi allow for the device to interact with the world around it. You can connect components or circuits using the pins to get data on the environment. Each pin has a different purpose. You can find a more detailed description of each at this reference.
GPIO Zero Library
The library includes interfaces to many simple everyday components, as well as some more complex things like sensors, analogue-to-digital converters, full colour LEDs, robotics kits and more. Once you import the gpiozero library, you will be able to control specific components that are connected to the specified pins.
from gpiozero import LED, Button
from signal import pause
led = LED(17)
button = Button(3)
button.when_pressed = led.on
button.when_released = led.off
pause()
from gpiozero import LED
from time import sleep
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)