# 1.4: GPIO Outputs - First Circuit

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\avec}{\mathbf a}$$ $$\newcommand{\bvec}{\mathbf b}$$ $$\newcommand{\cvec}{\mathbf c}$$ $$\newcommand{\dvec}{\mathbf d}$$ $$\newcommand{\dtil}{\widetilde{\mathbf d}}$$ $$\newcommand{\evec}{\mathbf e}$$ $$\newcommand{\fvec}{\mathbf f}$$ $$\newcommand{\nvec}{\mathbf n}$$ $$\newcommand{\pvec}{\mathbf p}$$ $$\newcommand{\qvec}{\mathbf q}$$ $$\newcommand{\svec}{\mathbf s}$$ $$\newcommand{\tvec}{\mathbf t}$$ $$\newcommand{\uvec}{\mathbf u}$$ $$\newcommand{\vvec}{\mathbf v}$$ $$\newcommand{\wvec}{\mathbf w}$$ $$\newcommand{\xvec}{\mathbf x}$$ $$\newcommand{\yvec}{\mathbf y}$$ $$\newcommand{\zvec}{\mathbf z}$$ $$\newcommand{\rvec}{\mathbf r}$$ $$\newcommand{\mvec}{\mathbf m}$$ $$\newcommand{\zerovec}{\mathbf 0}$$ $$\newcommand{\onevec}{\mathbf 1}$$ $$\newcommand{\real}{\mathbb R}$$ $$\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}$$ $$\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}$$ $$\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}$$ $$\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}$$ $$\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}$$ $$\newcommand{\laspan}[1]{\text{Span}\{#1\}}$$ $$\newcommand{\bcal}{\cal B}$$ $$\newcommand{\ccal}{\cal C}$$ $$\newcommand{\scal}{\cal S}$$ $$\newcommand{\wcal}{\cal W}$$ $$\newcommand{\ecal}{\cal E}$$ $$\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}$$ $$\newcommand{\gray}[1]{\color{gray}{#1}}$$ $$\newcommand{\lgray}[1]{\color{lightgray}{#1}}$$ $$\newcommand{\rank}{\operatorname{rank}}$$ $$\newcommand{\row}{\text{Row}}$$ $$\newcommand{\col}{\text{Col}}$$ $$\renewcommand{\row}{\text{Row}}$$ $$\newcommand{\nul}{\text{Nul}}$$ $$\newcommand{\var}{\text{Var}}$$ $$\newcommand{\corr}{\text{corr}}$$ $$\newcommand{\len}[1]{\left|#1\right|}$$ $$\newcommand{\bbar}{\overline{\bvec}}$$ $$\newcommand{\bhat}{\widehat{\bvec}}$$ $$\newcommand{\bperp}{\bvec^\perp}$$ $$\newcommand{\xhat}{\widehat{\xvec}}$$ $$\newcommand{\vhat}{\widehat{\vvec}}$$ $$\newcommand{\uhat}{\widehat{\uvec}}$$ $$\newcommand{\what}{\widehat{\wvec}}$$ $$\newcommand{\Sighat}{\widehat{\Sigma}}$$ $$\newcommand{\lt}{<}$$ $$\newcommand{\gt}{>}$$ $$\newcommand{\amp}{&}$$ $$\definecolor{fillinmathshade}{gray}{0.9}$$

hypothes.is tag: s20iostiot4ualr

## Learning Objectives

Students will be able to:

Content:

• Access the GPIO on the Raspberry Pi
• Build simple circuits
• turn an LED on and off with BPIOzero

Process:

• write code that turns on LED lights

Prior Knowledge

• Python and IoT concepts from previous activities

## Task 1: Electricity and Electric circuit basics

Read the following webpage and view the embedded videos on voltage, current, and Ohm’s law.

## Model 1: GPIO Pins

General Purpose Input/Output on the Raspberry Pi:

Your raspberry pi has a set of bi-directional input/output pins that allow you to interact with the physical world. There are two ways to address the numbers: Broadcom (BCM) pin numbering and board numbering. Board numbering goes from 1-40 corresponding to the pins on the device. Any pin marked GPIO can be used for input and output. Some pins are reserved as power outputs, electrical grounds, or for other purposes.

Critical Thinking Questions:

1. What are the board numbers for GPIO pins that are closest to the USB ports on your Raspberry Pi?
2. Provide the board pin number(s) for each of the following:
1. Which board pins (numbers) provide 5V of electrical power potential?
2. Which board pins provide 3V of electrical power potential?
3. Which board pins are electrical ground?
4. How many GPIO pins are available on your Raspberry Pi?
3. What colors are used to represent
1. Ground
2. 5V
3. 3V
4. Input/output
4. You have connected a device to board number 11. What GPIO number will you refer to for programming purposes?

Note

Whenever connecting/disconnecting items from your Raspberry Pi, you should always power off first to protect the Pi computer

## Task 2: Setting up Pi and Cobbler

Setting up your Raspberry Pi to build circuits requires us to use prototyping board called a breadboard. Connect your cobbler to the 40 pin ribbon connector. Make sure your pins are not bent, and that the cobbler starts at line 1 of the breadboard.

If this is the first time setting up a breadboard you should watch the following video.

Companion video available here: https://youtu.be/Ljd3dpfa1JQ

The cobbler lets us connect the breadboard easily to the raspberry Pi. At this point, boot up your Raspberry Pi.

Note

There are two common types of cobblers, those with power pins (like the Vilros Cobbler) and those without power pins (like the AdaFruit Cobbler)

• Vilros Type cobbler that has 3.3V and 5V pins for the outer power rails
• Adafruit Type cobbler, which does not have these pins built into it, and so you need to run a line to the power rails

In this activity you will connect a red LED to a circuit on the bread board with either a 220 or 330 ohm resistor.  The resister is needed to reduce the current so you do not burn out the LED (V=IR). Before building your first circuit watch the following YouTube. Then follow the procedures below for a Vilros Cobbler (part A) or an AdaFruit cobbler (part B), depending on which type of cobbler you have

Companion video available here: https://youtu.be/R-u0DTWCcXk

### A. Vilros Type Cobler

1.  Start by placing one end of a black male-to-male jumper cable in the negative (ground) line connected to 3v3 of your vilros cobbler. Connect other end of the jumpber to slot 30a.
2. Slightly bend a 330 ohm resistor to make a U shape. Put one end in the 30b slot, and the other end in the 34b slot.
3. Take a red LED and connect the short end (cathode) to slot 34c. Connect the long end (anode) to slot 36c.
4. Place one end of a red male-to-male jumper cable in slot 36a.
5. Place the other end of the red jumper cable in the 3v3 + line.

1. Start by placing one end of black male-to-male jumper cable in the negative (ground) line and connect it to a GND on the Adafruit Cobbler.
2. Place one end of a red male-to-male jumper cable to the positive (+) line and connect it to 3V3 (Pin1) on the cobbler.
3. Place one end of a black male-to-male jumper cable in the negative (ground) line connected to 3v3 of your cobbler. Connect other end of the jumper to slot 30a.
4. Slightly bend a 330 ohm resistor to make a U shape. Put one end in the 30b slot, and the other end in the 34b slot.
5. Take a red LED and connect the short end (cathod to slot 34c. Connect the long end (anode) to slot 36c.
6. Place one end of a red male-to-male jumper cable in slot 36a.
7. Place the other end of the red jumper cable in the 3v3+ line.

Note

For all subsequent builds with the Adafruit type cobbler you will need to hook up the power lines as in step 1 & 2.  For the rest of the activities in this lesson you need to keep the cables to the 3.3 V pin connected, although in some subsequent activities you will be running power through the 5 V pin.

Important!  If you don’t have a resistor in this circuit, you will send too much electricity to the LED. It will light up once briefly, and then not work again (much like a blown fuse or light bulb)!

Once you have built your circuit answer the following Critical Thinking Questions.

Critical Thinking Questions:

1. What happened to when you connected the red jumper to the 3v3 line?
2. Disconnect the end of the black jumper from the ground line leaving the other end in slot 30a.
1. What happened? Why?
2. Reconnect the black jumper to any point on the 3v3 ground line. What happens if you choose different slots?
3. Disconnect the end of the red jumper from the 3v3 + line leaving the other end in slot 36c.
1. What happened? Why?
2. Reconnect the red jumper to any point on the 3v3 ground line. What happens if you choose different slots?
3. What can you conclude about how the cobbler and the 3v3 lines on the breadboard work?
4. Replace the resistor in you circuit with a different one from your kit, and test the circuit. What do you think would happen to the intensity of the light if you switched the 220 ohm resistor in this circuit to one of:
1. Higher resistance (e.g. 1000 ohms)?
2. Lower resistance (e.g. 100 ohms)?
5. Remove the end of the red jumper from the 3v3+ line, and place it in the 5V +line. How does the difference in voltage affect the brightness of the red LED? Use the waterfall analogy to explain the differences you see.
6. Disconnect the red jumper from the 5V line (leaving the other end connect to 36c). Describe what happens during each of these changes: (consult Model 1 for help)
1. Connect the end of the jumper to GPIO pin 2.
2. Connect the jumper to board pin 17.
3. Connect it to GPIO 17. (Leave it plugged in here regardless of the outcome)

Note

Included in the Raspbian OS is a library for interfacing to many simple input and output components called GPIOzero. It also has functions for more complex items such as sensors.

Remember: To stop any program from running you can use <Ctrl +C> to stop it.

Enter the following program into the Thonny IDE and run it.

IoCT Program 3.1

from gpiozero import LED
from time import sleep

red = LED(17)
try:
while True:
red.on()
sleep(1)
red.off()
sleep(1)
except KeyboardInterrupt:
red.off()
quit()


Critical Thinking Questions:

1.  Change the value of each sleep line to 3. Rerun the program.
2. Next to each line of code above, indicate what the statement does.
3. Change your code to make a program that cycles your red light on and off 5 times.

Connect a new yellow LED and resistor to create a new circuit connected to GPIO18.

Critical Thinking Questions:

1. Alter your program to indicate a yellow LED is now on GPIO18, and it should blink in unison with the red light.
2. Alter your program to have the yellow and red alternatively blink. That is when the red light is on, the yellow light is off.

Enter the following program and run it.

IoCT Program 3.2

from gpiozero import PWMLED
from time import sleep

led = PWMLED(17)
try:
while True:
led.value = 0
sleep(1)
led.value = 0.5
sleep(1)
led.value = 1
sleep(1)
except KeyboardInterrupt:
led.value = 0
quit()


Critical Thinking Questions:

1. Change the led.value to other numbers between 0 and 1 and rerun the code.
2. What does the PWMLED function do?

## Task 6: Common Anode RGB Circuit

Build the Following Circuit, Remember, whenever connecting/disconnecting items from your Raspberry Pi, you should always power off first, to protect the Raspberry Pi.

Instructions for setting up circuit in figure $$\PageIndex{4}$$.

1. Start by placing the LED into the breadboard. There are four legs. The longest one is the anode and will need to be connected to 3.3v on the rail. For simplicity, connect the longest leg into slot 45c. You should have one leg(pin) in slot 44c, one leg in slot 46c and one leg in slot 47c.
2. Connect a 220 or 330 ohm resistor in slot 45b and jumper it directly to the 3v3 (+) rail.
3. Connect a red jumper from GPIO6 to 44d.
4. Connect a green jumper from GPIO13 to 46d.
5. Connect a blue jumper from GPIO19 to 47d.

Note

RGB (Red Green Blue) is an additive color model based on 8 bits, see, https://rgbcolorcode.com/.  (R,G,B) values of (0,0,0) corresponds to black and (255,255,255) corresponds to white. It is not important that you use slots 44-47,  but the 4 pins on the common anode RGB LED are short/long/short/short, and in that order represent R/Anode/G/B.  You then need to connect the Anode to hot, and the R,G & B to the gpio pins as defined in your code. The first line of code in IOCT Program 3.3 states: led=REBLED(red=6, green=13, blue=19), and so we are connecting the red to gpio6, green to gpio 13 and blue to gpio19. You can use any GPIO pin you want, as long as you properly assign it in your code.

Enter the following program and run it.

IOCT Program 3.3

from gpiozero import RGBLED
from time import sleep

led= RGBLED(red= 6, green = 13, blue = 19, active_high = False)
led.red = 0
led.green = 0
led.blue = 0
led.red = 1
sleep(1)
led.red = 0
led.green = 1
sleep(1)
led.green = 0
led.blue = 1
sleep(1)
led.blue = 0
led.red = 0.1
sleep(1)
led.red = 0
led.green = 0.1
sleep(1)
led.green = 0
led.blue = 0.1
sleep(1)
led.red = 0
led.green = 0
led.blue = 0
sleep(1)


Critical Thinking Questions

1. Next to each line of code above, indicate what that does.
2. How does changing the values affect the program?

Enter the following program and run it

IoCT Program 3.4

from gpiozero import RGBLED
from time import sleep

led= RGBLED(red= 6, green = 13, blue = 19, active_high = False)
led.color= (0,0,0)
led.color= (1,0,0)
sleep(1)
led.color= (0,1,0)
sleep(1)
led.color= (0,0,1)
sleep(1)
led.color = (0.5,0,1)
sleep(1)
led.color= (0.5,1,0)
sleep(1)
led.color = (0, 0.7, 0.7)
sleep(1)
led.color = (0,0,0)


Critical Thinking Questions:

1. Next to each led.color line in the code above, write the color the LED makes.
2. Color mixed light in equal amounts of red and green should give a yellow light. Color mixed in equal amounts of red and blue should give a magenta/purple light. Alter the code above to have values in line 12 to have equal amounts of red and blue, and line 14 be equal amounts of red and green. Why did the original code not use equal values?
3. Color mixed light in equal amounts of red, green, and blue should give a white light. Write a new program that will make the LED produce white light. Report your values to the class.

This page titled 1.4: GPIO Outputs - First Circuit is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.