Skip to main content
Chemistry LibreTexts

1.4: GPIO Outputs - First Circuit

  • Page ID
    208820
  • hypothes.is tag: s20iostiot4ualr
    Download Assignment: S2020iot04

    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

    Further Reading

     

    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.
     

    gpio of RPi

    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.

    RPi CobblerFigure \(\PageIndex{1}\): Breadboard connected to GPOI pins via ribbon and cobbler. Be sure to POWER OFF before connecting devices to your Pi!

    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

     

    Task 3: Building your First Circuit

    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 urrent 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.
    first circuit
    Figure \(\PageIndex{2}\): Vilros type cobbler with power pins

    B. AdaFruit Type Cobbler

    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.
    adafruit cobler setup
    Figure \(\PageIndex{3}\): Adafruit type cobbler where you have to connect the power rails, here we are connecting the 3.3V rails.

     

    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)

     

    Task 4: GPIOZero Programs

    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.

     

    Task 5: Add second LED Circuit

    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.

    circuitFigure \(\PageIndex{4}\): Common anode RGB LED connected with either a 220 or 330 ohm resistor.

    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.

     

    • Was this article helpful?