Type to search

Featured GPIO Tutorials

Blinking LED on Raspberry Pi 3 using GPIO via Python

Share

Blinking LED on Raspberry Pi using Python

In this tutorial you will learn how to connect a LED light 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 GPIO pins.

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.

Requirements

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

Setup

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.

Software

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
    time.sleep(1)
    GPIO.output(LedPin, GPIO.LOW) # led off
    time.sleep(1)

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

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

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.

7 Comments

  1. Sepp Gabelberger August 10, 2017

    Thanks for a good article! Unfortunately, the diagram incorrectly shows the two legs of the resistor in the same column of the breadboard so it is effectively shorted. The legs need to be in different rows with the bottom leg aligned vertically with the red lead and the top leg aligned with the right leg of the LED.

    Reply
    1. RaspiNews August 22, 2017

      Schematic updated 🙂 Thank you for pointing out the issue.

  2. deepak August 22, 2017

    thank you very much

    Reply
  3. monu March 18, 2018

    Thanks it was very useful

    Reply
  4. Kevin April 21, 2018

    Hi I like your article very much indeed. I’m new to the Raspberry Pi. I have purchased a new Raspberry Pi 3 B & 16GB NOOBS SD Card. I understand the 1st bit of information using the terminal but you then say “we now need to use a python script to make it blink”
    Do I need to open python from the programming menu or am I doing the python script in terminal mode?
    I then go on to look at the program you wrote called “blink.py” but I noticed that in lines 1-27 there is a lot of spacing between the words, am I correct in saying for line 1 I would type
    1. import^RPi^.^GPIO^as^GPIO The^ I have typed in represent a tap of the space bar.
    I’m a beginner at this as I’ve never done any programming but I’m ok on the electronics and building of circuits.

    Reply
  5. Cong April 22, 2018

    Thank you very much!

    Reply
  6. Tdm May 9, 2018

    I get a trace back that says name is not defined. Any ideas?

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *