You are on page 1of 14

Electronics Project Kits For Students Click Here

ELECTRONICS HUB
P R O J E C T S | T U T O R I A L S

HOME GENERAL HOW TO BLINK AN LED USING RASPBERRY PI AND PYTHON

How to Blink an LED using Raspberry Pi and Python


SEPTEMBER 5, 2017 BY EH STAFF LEAVE A COMMENT

Contents [hide]

1 GPIO (General Purpose Input/Output) Pins of Raspberry Pi


2 Circuit Diagram of Blinking LED with Raspberry Pi
2.1 Circuit 1
2.2 Circuit 2
3 Components Required
4 Circuit Principle
5 Code for Blinking an LED with Raspberry Pi
6 How to operate the Blinking LED using Raspberry Pi?
7 Code Explanation
8 Related Articles

In this tutorial, I will show you how to blink an LED using Raspberry Pi. In order to blink the
LED, we need to access the GPIO (General Purpose Input/Output) Pins of the Raspberry Pi.

In the previous tutorials, we have seen how to configure headless setup of Raspberry Pi,
setting up Wi-Fi, installing touch screen LCD, assigning static IP address to Raspberry Pi and
also we have seen how to write our first Python program in Raspberry Pi.

In all the previous tutorials (except in case of Touch Screen setup with Raspberry Pi), we
havent used any of the GPIO pins on the Raspberry Pi.

Let us discuss some of the features of the Raspberry Pis GPIO Pins before proceeding with the
further step of how to Blink an LED using Raspberry Pin and its GPIO Pins.

Before going in to the details, try to read How to configure the headless setup of Raspberry
Pi?
GPIO (General Purpose Input/Output) Pins of Raspberry Pi
Raspberry Pi has a powerful feature in the form of the General Purpose Input / Output or
simply GPIO Pins. GPIO Pins form the physical interface between the real world and the
Raspberry Pi.

Different external components like LEDs, Motors, Sensors, Display, etc. are connected to the
Raspberry Pi through these GPIO Pins. In our project, we are going to blink an LED using
Raspberry Pi and hence the knowledge of all the GPIO Pins is important.

The latest revision of the Raspberry Pi series i.e. Raspberry Pi 3 Model B has 40 GPIO Pins. Out
of these 40 GPIO pins few are power pins i.e. 3.3V Pins (2), 5V Pins (2) and GND (8). In the rest of
the 28 pins, few are truly general purpose GPIO Pins while few pins have a dual function.

The following image shows the GPIO Pins of the Raspberry Pi 3 Model B board.
The numbers in the center (that are circled) are the Physical Pins of the Raspberry Pi. They are
also called as Board Pins or Numbers. The GPIO Numbers (like Physical Pin 3 is GPIO2) are
those which are seen by the Processor. This numbering is called as GPIO Numbering or BCM
Numbering.

NOTE: Be careful about the numbering scheme you are going to use in your program. In this
project of how to blink and LED using Raspberry Pi and Python Program, Ive used the Board
Numbering.

Circuit Diagram of Blinking LED with Raspberry Pi


In order to Blink an LED using Raspberry Pi, we need to first connect the LED to the Raspberry
Pi. There are two ways you can connect your LED to the Raspberry Pi. Ill show both ways of
connecting the LED.

Circuit 1
In the first circuit, the anode of the LED is connected to GPIO25 (Physical Pin 22) through a 1K
current limiting resistor. The cathode of the LED is connected to the GND Pin.
In this circuit, the GPIO Pins will act as source and provides with 3.3V when it is activated.

Circuit 2
In this circuit, the anode of the LED is connected to the 3.3V supply pin of the Raspberry Pi
through the 1K resistor. The cathode of the LED is connected to GPIO25 (Physical Pin 22).

In this circuit, the GPIO pin acts as the sink (GND).

NOTE: Ill be concentrating on the first circuit, where the GPIO pin GPIO25 acts as the source.
The code explained in the further sections will be specific to this circuit. The code can also be
used for second circuit with slight or no modifications.

Components Required
Raspberry Pi 3 Model B (any Raspberry Pi would do fine)
5mm LED x 1
1K Resistor (1/4 Watt) x 1
Mini Breadboard x 1
Connecting wires
Miscellaneous (Computer, Ethernet cable, Power Supply for Raspberry Pi etc.)
Also read this: Your first Python Program on Raspberry Pi

Circuit Principle
The principle of operation is very simple. The GPIO pin (GPIO25) is made HIGH for a second
and is made LOW for the next second. This process is kept on loop so that we get effect of
Blinking LED.

In order to control the GPIO Pin i.e. making it HIGH and LOW, Ill be using Python programming
and few Python Packages.

Code for Blinking an LED with Raspberry Pi


1 #!/usr/bin/env python

2 import RPi.GPIO as GPIO # RPi.GPIO can be referred as GPIO from now

3 import time

4
5 ledPin = 22 # pin22
6
7 def setup():

8 GPIO.setmode(GPIO.BOARD) # GPIO Numbering of Pins

9 GPIO.setup(ledPin, GPIO.OUT) # Set ledPin as output


10 GPIO.output(ledPin, GPIO.LOW) # Set ledPin to LOW to turn Off the LED

11
12 def loop():

13 while True:
14 print 'LED on'

15 GPIO.output(ledPin, GPIO.HIGH) # LED On

16 time.sleep(1.0) # wait 1 sec

17 print 'LED off'

18 GPIO.output(ledPin, GPIO.LOW) # LED Off


19 time.sleep(1.0) # wait 1 sec

20 def endprogram():

21
22 GPIO.output(ledPin, GPIO.LOW) # LED Off
23 GPIO.cleanup() # Release resources

24
25 if __name__ == '__main__': # Program starts from here

26 setup()
27 try:

28 loop()

29 except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the destroy() will be executed.

30 endprogram()

blinkLed.py hosted with by GitHub view raw

How to operate the Blinking LED using Raspberry Pi?


First of all, Ive used the Vim Editor for writing the Python Program. Vim is a Command Line
Editor and is a very simple and easy to use text editor. First, login in to your Raspberry Pi using
SSH (like Putty for example).

To install the Vim Editor (by default, Raspbian has Vi editor and to get full features of Vim),
enter the following code in the SSH Terminal.

sudo apt-get install vim


Now open a blank Python file using Vim editor with the file name being blinkLed.py. For this,
use the following command.

sudo vim blinkLed.py

NOTE: Ive created a folder Python_Progs in the Desktop of the Raspberry Pi. So, first I should
be present in that directory and then create the above Python file. (To change directories, use
cd command).

After opening the blinkLed.py file, copy the above code and paste it there. Since it is a Python
code, you need to be careful with the Tab characters as it is important to group the instruction
as blocks in Python.

Save the blinkLed.py file and close the Vim Editor. To check the code, use the following
command in the SSH Terminal (Putty).

sudo python blinkLed.py

If everything goes well, your LED should blink at an interval of 1 second i.e. one for one second
and off for the other second.

Code Explanation
In the Python program, first we have imported two packages RPi.GPIO and time. The package
RPi.GPIO will help us in controlling the GPIO Pins of the Raspberry Pi.

The first important function of the RPi.GPIO Module is the setmode(). Using GPIO.setmode(), we
can select either GPIO Numbering of the Pins or Physical Numbering. By using
GPIO.setmode(GPIO.BOARD), we are selecting the Physical Numbering Scheme.

NOTE: For GPIO Numbering, you can use GPIO.setmode(GPIO.BCM).

The next function is the setup(pin,mode). This function will allow us to set the pin as either
input (GPIO.IN) or as output (GPIO.OUT). In the program, I have set the ledPin as output by
using GPIO.setup(ledPin, GPIO.OUT) .

After setting the LED Pin as OUTPUT, now we need to set the state of this OUTPUT i.e. HIGH
(GPIO.HIGH) or LOW (GPIO.LOW). For this, we need to use the function output(pin, state). So, in
our program, we need to use GPIO.output(ledPin, GPIO.HIGH) for turning ON the LED and
GPIO.output(ledPin, GPIO.LOW) for turning it OFF.

The last function is the GPIO.cleanup(). With the help of this function, we can make a clean exit
from the program as it will clean all the resources that are used in the program.

Applications

In this project, we have seen how to blink an LED using Raspberry Pi and Python
Program. This project will help you in understanding some basics of the GPIO Pins of
Raspberry Pi.
With this project as reference, we can develop many other projects like driving motors,
connecting LCDs, etc.

Related Articles

How to setup WiFi on First Python Program How to setup WiFi on How to Setup Static IP
Raspberry Pi 3 on the Raspberry Pi Raspberry Pi 2 using Address on Raspberry
USB Dongle Pi?

How to Setup Interfacing 162 LCD Raspberry Pi Basic RGB LED Bulb
Touchscreen LCD on with Raspberry Pi Setup without Monitor
Raspberry Pi?? and Keyboard
(Headless Setup)

FILED UNDER: DIY PROJECTS, GENERAL, RASPBERRY PI

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

Comment

Name *
Email *

Website

reCAPTCHA

Please upgrade to a supported browser


to get a reCAPTCHA challenge.

Alternatively if you think you are getting


this page in error, please check your
internet connection and reload.

Why is this happening to me?

POST COMMENT

Search this website

Suggest A Topic [or] Project


COMPLETE STEP BY STEP GUIDE

ads by media.net

Python Programming Tutorials

Programming Courses

Object-Oriented Programming

Electronic Circuit Design

Electronics Project Ideas

Basic Electronics Tutorial

Android App Development

Electronics Hub

YouTube 18K

Electronics Hub

Follow On
Electronics Hub

Suivre

SUBSCRIBE FOR FREE PROJECT CIRCUITS

Enter your email address:

SUBSCRIBE

Delivered by FeedBurner

Electronics Hub
667,241 likes

Like Page Sign Up

Be the first of your friends to like this


ads by media.net

4 Pin Power Connector

Pin And Socket

ATX Motherboard

New Invention Ideas

Home Security Systems

Android App Development

Electronic Circuits Projects

Electronics Project Ideas

Electronic Project Kits

Simple Electronics Projects

Embedded Systems Projects

Engineering Projects 2017

Solar Power Systems

Motor Control Circuits

DIY Solar Panels

PROJECTS BY CATEGORY

Arduino Projects (200+)


Electronics Projects (250+)
Mini Project Circuits (160+)
Mini Project Ideas (150+)
ECE Projects (150+)
EEE Projects (150+)
8051 Projects (110+)
Raspberry Pi Projects (101+)
Electrical Project Ideas (100+)
Embedded Projects (100+)
Latest Electronics Ideas (100+)
Microcontroller Mini Projects (100+)
Robotics Projects (100+)
VLSI Projects (100+)
Solar Projects (100+)
IOT Projects (100+)

Communication Projects (70+)


LED Projects (70+)
Power Electronics Projects (60+)
RFID Projects (60+)
Home Automation Projects (50+)
Matlab Projects (50+)
EIE Projects (50+)
Wireless Projects (50+)
LabView Projects (45+)
Zigbee Projects (45+)
GSM Projects (40+)
555 Timer Circuits (40+)
Sensor Projects (40+)
ARM Projects (60+)
DTMF Projects (30+)
PIC Projects (30+)
Electrical Mini Projects (25)

GENERAL

Tutorials

Symbols

Courses

Calculator

Contact

PROJECTS

Electrical

Electronics

Embedded

Power

Robotics
ARM

IOT

PROJECTS

Mini projects

Microcontroller

Aurdino
Solar

Free circuits

Home Automation

TUTORIALS

Capcitors
Resitors

Filters

Diodes

Transistors

TUTORIALS

Amplifiers
IO Devices

Thyristors

DC Circuits

Nummber System

FOLLOW US

Facebook

Youtube

Google Plus

Twitter

Copyright 2017 Electronicshub.org