Introduction: Control LED Using Raspberry Pi GPIO
This is a basic project to connect an LED to RPi GPIO and control using a python script.
Step 1: Components
You'll need the following components to connect the circuit.
1. Raspberry Pi
2. LED
3. Resistor - 330 ohm
4. Breadboard
5. 2 Male-Female Jumper Wires
Except for the Raspberry Pi, you can buy a basic starter kit which will include all the other components.
Step 2: Connecting the Circuit
Please keep the Raspberry Pi turned off until the circuit is connected to avoid accidentally shorting any components.
The LED has 2 legs. The longer leg, 'anode', is always connected to positive supply. The shorter leg,'cathode', is always connected to ground.
You need a resistor is the circuit to limit the amount of current in the circuit. Without the resistor the current flowing through the LED will be much larger and lead to a short damaging the circuit.
You can identify a 330 Ohm resistor using the color coding on the resistor:
- If there are four colour bands, they will be Orange, Orange, Brown, and then Gold.
- If there are five bands, then the colours will be Orange, Orange, Black, Black, Brown.
Connect the circuit:
- Use a jumper wire to connect the ground ( Pin 3) of GPIO to rail marked in blue on the breadboard.
- Connect the resistor from the same row on the breadboard to a column on the breadboard.
- Connect the LED with the cathode in the same row as the resistor. Insert the anode in the adjacent row.
- Use another jumper cable to connect the GPIO Pin 21 ( 3.3 V) in the same row as the anode of LED.
This completes the circuit. Refer to the picture and the schematic above.
Step 3: Script to Control GPIO
Once the circuit is complete turn on RPi. Open a text editor like nano/vi using a terminal.
Create a file gpio.py and copy the below code. You can also download it from GitHub repo.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(21,GPIO.OUT)
print "LED on"
GPIO.output(21,GPIO.HIGH)
time.sleep(10)
print "LED off"
GPIO.output(21,GPIO.LOW)
Exit the file and run the program in the terminal using:
python gpio.py
Step 4: Take It a Step Further
Now that you have interfaced Raspberry Pi and an LED, you can try taking it a step further.
You can use Amazon Echo to control the RPi GPIO !!