Led Blinking using Raspberry Pi – Python

Led Blinking using Raspberry Pi – Python

Contents

Led blinking is one of the beginner circuits which helps one to get acquainted with GPIO pins of Raspberry Pi. Here we use Python language to write the code for blinking Led at one second intervals.

Components required

  • One led
  • 100 ohm resistor
  • Jumper cables

Raspberry Pi GPIO Specifications

  • Output Voltage : 3.3V
  • Maximum Output Current : 16mA per pin with total current from all pins not exceeding 50mA

For controlling a Led using Raspberry Pi, both python and the GPIO library is needed.

Installing Python GPIO Library

Note: Python and GPIO library are preinstalled if you are using Raspbian.

  • Make sure your Rasperry Pi is connected to the internet using either a LAN cable or a WiFi adapter.
  • Open the terminal by double clicking the LXTerminal icon
  • Type the following command to download the GPIO library as a tarball
        wget http://raspberry-gpio-python.googlecode.com/files/RPi.GPIO-0.4.1a.tar.gz
  • Unzip the tarball
        tar zxvf RPi.GPIO-0.4.1a.tar.gz
  • Change the directory to unzipped location
        cd RPi.GPIO-0.4.1a
  • Install the GPIO library in python
        sudo python setup.py install

Raspberry Pi GPIO Pin Out

Raspberry Pi has 17 GPIO pins out of 26 pins


Raspberry Pi GPIO layout
Raspberry Pi GPIO layout

Circuit Diagram

  • Connect the Led to 6 (ground) and 11 (gpio) with a 100Ω resistor in series
Connecting LED to Raspberry Pi
Connecting LED to Raspberry Pi

Python Programming

  • Open Terminal
  • Launch IDLE IDE by typing
sudo idle
RaspberryPi terminal
RaspberryPi terminal

This launches IDLE with superuser privileges which is necessary execute scripts for controlling the GPIO pins

RaspberryPi idle
RaspberryPi idle
  • After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
RaspberryPi idle new window
RaspberryPi idle new window
  • Type the code below in the window
import time
import RPi.GPIO as GPIO       ## Import GPIO library
GPIO.setmode(GPIO.BOARD)      ## Use board pin numbering
GPIO.setup(11, GPIO.OUT)      ## Setup GPIO Pin 11 to OUT
while True:
	GPIO.output(11,True)  ## Turn on Led
	time.sleep(1)         ## Wait for one second
	GPIO.output(11,False) ## Turn off Led
	time.sleep(1)         ## Wait for one second
RaspberryPi idle code
RaspberryPi idle code
  • Save the code by FILE>SAVE or Ctrl+S
 RaspberryPi idle save
RaspberryPi idle save
  • To run your code RUN>RUN or Ctrl+F5
RaspberryPi idle run
RaspberryPi idle run
  • The Led will start blinking now.

Video

Share this post

  • Can we make it blink when no internet connection detected.? I want to use it for router restart when no internet.


  • >