Computers, Laptops & Tablets Accessories & Hardware 20 20 people found this article helpful How to Light an LED with the Raspberry Pi's GPIO Control LED lights from your Pi with Python by Richard Saville Writer Richard Saville is a former Lifewire writer and computer enthusiast who has invented several add-on boards for Raspberry Pi and has been published in MagPi and other outlets. our editorial process Facebook Twitter Richard Saville Updated on September 22, 2020 Accessories & Hardware Raspberry Pi The Quick Guide to Webcams Keyboards & Mice Monitors Cards HDD & SSD Printers & Scanners Tweet Share Email The GPIO is how the Raspberry Pi talks to the outside world. It uses code to program the signals and voltages to and from the 40-pin header. Coding with the GPIO is reasonably simple, especially for beginner projects such as LEDs and buzzers. With a couple of components and a few lines of code, you can light or flash an LED as part of your project. This tutorial shows what you need to light an LED using Python code on your Raspberry Pi by using the traditional RPi.GPIO method. What You Need to Start the Project To start the project, begin with the list of items below. You should be able to find these items in your favorite maker store or online auction sites. A Raspberry Pi workstation with the latest Raspbian (Pi, screen, keyboard, mouse, power, and SD card)A small breadboardA 5 mm LEDA 330-ohm resistorTwo male to female jumper wires Create the Circuit You'll use two GPIO pins for this project: A ground pin (physical pin 39) for the ground leg of the LED.A generic GPIO pin (GPIO 21, physical pin 40) to power the LED—but only when you decide to—which is where the code comes in. Turn off the Raspberry Pi. Use the jumper wires to connect the ground pin to a lane on the breadboard. Do the same for the GPIO pin, but connect it to a different lane. Add the LED to the Circuit Next, add the LED and resistor to the circuit. LEDs have polarity, which means LEDs must be wired in a certain way. LEDs usually have one longer leg, which is the anode (positive) leg, and a flat edge on the LED plastic head, which denotes the cathode (negative) leg. A resistor protects the LED from receiving too much current and the GPIO pin from giving too much. This could damage both. There is a bit of a generic resistor rating for standard LEDs: 330 ohms. There is some math behind that, but for now, focus on the project. You can always look into Ohm's law and related topics afterward. Connect one leg of the resistor to the GND lane on the breadboard and the other resistor leg to the lane connected to the shorter leg of the LED. Join the longer leg of the LED to the lane connected to the GPIO pin. Create the Python GPIO Code (RPi.GPIO) At this moment, you have a circuit wired up and ready to go, but you haven't told the GPIO pin to send any power. So, the LED shouldn't be lit. The next step is to make a Python file to tell the GPIO pin to send power for five seconds, and then stop. The latest version of Raspbian has the necessary GPIO libraries installed. Open a terminal window and create a new Python script by entering the following commands: touch led.pychmod +x led.pynano led.py This opens a blank file where you'll enter the code. Enter the lines below: #! /usr/bin/python# Import the libraries we needimport RPi.GPIO as GPIOimport time# Set the GPIO modeGPIO.setmode(GPIO.BCM)# Set the LED GPIO numberLED = 21# Set the LED GPIO pin as an outputGPIO.setup(LED, GPIO.OUT)# Turn the GPIO pin onGPIO.output(LED,True)# Wait 5 secondstime.sleep(5)# Turn the GPIO pin offGPIO.output(LED,False) Press Ctrl+X to save the file. To run the file, enter the following command in the terminal and press Enter: python led.py The LED should light for five seconds then turn off, ending the program.