In this tutorial we'll learn how to control a LED using the GPIO and Python
Materials:
- Raspberry Pi with Raspbian or similar
- 2 pin LED
- 1K resistor
- 2 wires male/female
- Breadboard
The code to make the LED blink 3 times is:
import RPi.GPIO as GPIO
import time
LED_PIN = 11 # Set LED_PIN variable to 11
GPIO.setmode(GPIO.BOARD) # Set GPIO mode to BOARD to use pin numbers
GPIO.setup(LED_PIN, GPIO.OUT) # Prepare the LED pin to be used
for i in range(3): # Turn ON and OFF the LED 3 times.
GPIO.output(LED_PIN, GPIO.HIGH) # Set LED pin on
time.sleep(0.5)
GPIO.output(LED_PIN, GPIO.LOW) # Set LED pin off
time.sleep(0.5)
GPIO.cleanup() # Clear GPIO
Here we can see the circuit between the rPi and the LED:
Here you can see a video about how to assemble the circuit and run the code: