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 June 05, 2020 Choosing your first Raspberry Pi can be confusing for new enthusiasts. Richard Saville 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 to get started with, especially for beginner projects such as LEDs and buzzers. With just 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, you'll need to 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 running the latest Raspbian (Pi, screen, keyboard, mouse, power, SD card)A small breadboardA 5mm LEDA 330-ohm resistor2 male to female jumper wires Step 1 - Create the Circuit We're going to use 2 GPIO pins for this project, a ground pin (physical pin 39) for the ground leg of the LED, and a generic GPIO pin (GPIO 21, physical pin 40) to power the LED—but only when we decide to—which is where the code comes in. Firstly, turn off your Raspberry Pi. Now, using the jumper wires, connect the ground pin to a lane on your breadboard. Next, do the same for the GPIO pin, connecting to a different lane. Step 2 - Add the LED to the Circuit Next, add the LED and resistor to the circuit. LEDs have polarity, which means they have to be wired in a certain way. They usually have one longer leg, which is the anode (positive) leg, and usually, they have a flat edge on the LED plastic head, which denotes the cathode (negative) leg. A resistor is used to protect both 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: 330ohm. There is some math behind that, but for now, focus on the project, and you can always look into Ohm's law and related topics afterward. Connect one leg of the resistor to the GND lane on your breadboard and the other resistor leg to the lane connected to the shorter leg of your LED. The longer leg of the LED now needs to join the lane connected to the GPIO pin. Step 3 - Python GPIO Code (RPi.GPIO) At this moment, you have a circuit wired up and ready to go, but now you haven't told the GPIO pin to send out any power yet. So, your LED shouldn't be lit. The next step is to make a Python file to tell the GPIO pin to send out some power for 5 seconds, and stop. The latest version of Raspbian will have the necessary GPIO libraries installed already. Open a terminal window and create a new Python script by entering the following commands: touch led.pychmod +x led.pynano led.py This will open a blank file for us to enter our 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 5 seconds then turn off, ending the program.