Tutorial 19: GPIO Introduction with a LED

Alright let's get our hands dirty with some hardware! In this tutorial I introduce you to the Raspberry Pi's GPIO header. I show you how you can blink a LED using a Python 3 script. A simple digital output.


DIFFICULTY
EASY
LINUX UNDERSTANDING
LITTLE
CIRCUITRY KNOWLEDGE
LITTLE
PYTHON PROGRAMMING
SOME
ABOUT
0
MINUTES
  • How to read the Pi’s GPIO Header
  • How to use a breadboard
  • How to build a LED circuit the Pi can drive
  • How to blink a LED using Python 3
You can copy / paste the code below if you’re having issues with typos or want a shortcut. However I recommend that you follow along in the tutorial to understand what is going on!


import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)

blinkCount = 3
count = 0
LEDPin = 22

# Setup the pin the LED is connected to
GPIO.setup(LEDPin, GPIO.OUT)

try:
    while count < blinkCount:
        GPIO.output(LEDPin, True)
        print("LED ON")
        sleep(3)
        GPIO.output(LEDPin, False)
        print("LED OFF")
        sleep(1)
        count += 1
finally:
    # Reset the GPIO Pins to a safe state
    GPIO.cleanup()

>
These images may help you complete the project: