Home » Blog » Blinking LED on Raspberry Pi using Python
Blinking LED on Raspberry Pi using Python

Blinking LED on Raspberry Pi using Python

Blinking LED on Raspberry Pi using Python

In this tutorial you will learn how to connect a LED to your Raspberry Pi’s on-board general purpose input-output (GPIO) pin and make it blink using Python. Before proceeding with the tutorial, let’s get familiar with what exactly is a GPIO Pin?

GPIO is a generic pin on an integrated circuit (In case of Pi its BCM2837 SoC) whose behavior (including whether it is an input or output pin) can be controlled (programmed) by the user at run time. By making connections to these pins (also known as header pins) the Raspberry Pi can interface to the electronic world consisting of sensors and indicators and actuators.

The simplest thing you can do with an Raspberry Pi to see an actual physical output is to blink an LED! It’s more like an electronics version of the classical hello world program that we write when learning a new software language.


For this tutorial, you will be needing some passive electronics components which you can easily find in your local hardware store.

  1. Jumper Wires
  2. LED’s, preferably Red ones
  3. 470 Ohm resistor
  4. Breadboard
  5. Latest ver. of Raspbian

We’re going to use pin 9 and 11 as you can see in the picture below. You can refer to this link for Raspberry Pi GPIO numbers. Make sure to setup the circuit exactly as the below schematic diagram and take note of the LED polarity. The negative lead of the LED is the shorter of the two leads.The bottom of the LED next to the positive lead is usually curved. This is useful if you cut the leads off shorter first and then forget which lead was the shorter one. A series resistor of 470 ohm is required to limit the current in the LED to a safe value.

Blinking LED Raspberry Pi Wiring


To make the LED blink we will be using RPi.GPIO: a popular Python based library used on Raspberry Pi platforms to control GPIO pins.The RPi.GPIO module is already installed by default in Raspbian. But to make sure that it is at the latest version connect your Raspberry Pi to the internet using either a LAN cable or a WiFi adapter and type in the below commands in terminal.

$ sudo apt-get update
$ sudo apt-get install python-rpi.gpio python3-rpi.gpio

Now we need to create a Python script to make it blink! The Python source code for flashing the LED is listed below:

Create new python file, blink.py copy the code below and save it.

import RPi.GPIO as GPIO
import time

LedPin = 11    # pin11

def setup():
  GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
  GPIO.setup(LedPin, GPIO.OUT)   # Set LedPin's mode is output
  GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to turn on led

def blink():
  while True:
    GPIO.output(LedPin, GPIO.HIGH)  # led on
    GPIO.output(LedPin, GPIO.LOW) # led off

def destroy():
  GPIO.output(LedPin, GPIO.LOW)   # led off
  GPIO.cleanup()                  # Release resource

if __name__ == '__main__':     # Program start from here
  except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.

To run the code from the command prompt on the Raspberry Pi, enter the following:

sudo python blink.py

You must be in the same directory as the blink.py file. If everything went according to plan, you should see the LED blink with a delay of 1 second. Press Ctrl + C to terminate the program.