Dylan: Programming

Steps:

Pre-requisites: Make sure GPIO library is imported on the Pi

$ sudo apt-get install python-rpi.gpio python3-rpi.gpio

  1. Update the Pi to make sure we are using the most recent OS and library files
    1. Why? The files we need get updated a lot. We want to make sure we are using the most updated files.
      1. sudo apt-get update
      2. sudo apt-get upgrade

Pin Identification Diagram
  1. Get familiar with the pins on the Pi using the diagram above.

  2. Wire the Pi and LED to breadboard
    1. Blue/Black wire to ground (Breadboard to Pin 6)
    2. Red wire to hot pin (Breadboard to Pin 8)
    3. Install LED
    4. Install Resistor

  1. Import libraries and functions (GPIO and Sleep)

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library

from time import sleep # Import the sleep function from the time module


4. Add line to ignore any warnings:

GPIO.setwarnings(False) # Ignore warning for now

5. Add line to set pin numbering system

GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

6. Add line to set pin assignment and initial value (Low=off)

GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial

Breakdown:

  • GPIO.setup – tells the Pi that we are going to change the pin settings.
  • 8 – tells the Pi that we are using Pin 8.
  • GPIO.OUT – tells the Pi that it is going to be an output Pin, meaning that when it is set to LOW, nothing will happen, but when it is set to HIGH, the Pi will send 5v of electricity through the Pin.
  • initial=GPIO.LOW – tells the Pi that when the program starts running, we don’t want any electricity to run through the Pin.

7. Add several lines for the blinking loop:

while True: # Run forever
    GPIO.output(8, GPIO.HIGH) # Turn on
    sleep(1)                  # Sleep for 1 second
    GPIO.output(8, GPIO.LOW)  # Turn off
    sleep(1)                  # Sleep for 1 second
  • The first line is a loop. It basically tells the Pi to execute the code indented underneath it forever.
  • The second line says “For Pin number 8, change the output setting to HIGH. This causes electricity to run through Pin 8 at 5v, and should make the LED light up.
  • The third line uses the “sleep” library which is a separate program and it tells the Pi to wait 1 second before moving on to the next line of code.
  • The fourth line is almost the same as the first line, but it changes the output to LOW, shutting of electricity to Pin 8, and turning off the LED.
  • The fifth line tells the Pi to wait another second before repeating the loop.

8. Look at the full program to see how all of the parts work together. Compare the code we have written to the full program below. They should match!

9. Save the file as “DylanBlink.py”

10. Run the file:

$ python DylanBlink.py

11. Look at the Pi and verify that the light is blinking.


12. Stop the blinking (CTRL C) or stop directly in the IDE.

13. Experiment with changing the sleep amounts.

  • higher blink amounts
  • lower blink amounts

14. We are done!


import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time module

GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial value to low (off)

while True: # Run forever
 GPIO.output(8, GPIO.HIGH) # Turn on
 sleep(1) # Sleep for 1 second
 GPIO.output(8, GPIO.LOW) # Turn off
 sleep(1) # Sleep for 1 second