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.
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: