Programming Raspberry Pi to Control LEDs attached to
the GPIO pins
Aim: Programming Raspberry Pi to Control LEDs attached to the GPIO pins
Objectives:
a. To understand basics of the GPIO Pins of Raspberry Pi.
b. To write a python program to control LEDs attached to the GPIO pins.
Raspberry Pi Pin-out (GPIO) :
Circuit Diagram:
1
Figure 1: Basic LED Common Anode Figure 2: Basic LED Common Cathode
GPIO Pin Connection:
Pin On Kit Pins on Raspberry Pi Module
GPIO Pin Name
LED 1 GPIO 14
LED 2 GPIO 15
LED 3 GPIO 18
LED 4 GPIO 23
LED 5 GPIO 24
LED 6 GPIO 25
LED 7 GPIO 8
LED 8 GPIO 7
GND GND
(Already Connected)
Procedure:
Note: The Raspbian operating system comes with a Python already installed in it.
1. Setting up the circuit:
a. Turn OFF the Raspberry Pi while building/connecting on the circuit board/
components according to the diagram.
b. Then turn the Raspberry Pi and check whether the Raspbian OS is loaded
properly or not. If not then check the circuit connection again.
2. Python Programming:
Python via IDLE
a. Start Raspberry Pi in desktop mode, open the applications menu in the top left of
your screen, and navigate to Programming > Python 3 (IDLE). This will open the
Python-shell.
b. Write a program to blink an LED and save it as “LED.py” file name.
2
Program:
#LED interfacing with Raspberry Pi
import RPi.GPIO as GPIO # import GPIO library
import time # import time library
GPIO.setmode(GPIO.BCM) #use BCM pin numbers
GPIO.setwarnings(False)
LED1=14
LED2=15
GPIO.setup(LED1,GPIO.OUT) # setup pin 14 as output
GPIO.setup(LED2,GPIO.OUT) #setup pin15 as output
time.sleep(1) #time delay for 1sec
GPIO.output(LED1,True) # set LED1 high
GPIO.output(LED2,False) # set LED2 low
time.sleep(1) # time delay for1 sec
GPIO.output(LED1,False) # set LED1 low
GPIO.output(LED2,True) # set LED2 high
time.sleep(1)
GPIO.output(LED1,True) #set LED1 high
GPIO.output(LED2,False) #set LED2 low
time.sleep(1)
GPIO.cleanup() #clear all pins
Steps to execute Program
1. Make the connections as given above.
2. Open „led.py‟ file in python3 IDE
3. Select Run from top menu and click Run Module.