You are on page 1of 6

Lab: Blinking an LEDs using Python and Raspberry Pi

Objectives
 Part 1: Setting up the Prototyping Lab with the Raspberry Pi
 Part 2: Set up the python code

Background / Scenario

In this lab you will learn more about how the Raspberry Pi can be used to write an application
that interacts with external electronics using the GPIO ports on the Raspberry Pi. In Part 1, you
will build the circuit with an external RGB LED that is connected to the GPIO ports on the
Raspberry Pi. In Part 2, you will create a program, using Python that will blink the RGB LED
when uploaded and run on the Raspberry Pi.

Required Resources

 PC with Internet Access


 Raspberry Pi with power cable and either a wired or wireless network connection
Part 1: Setting up the Prototyping Lab with the Raspberry Pi

It is important to know how the GPIO pins on the Raspberry Pi are numbered and accessed. The
Raspberry Pi 3 model B uses 3 different pin numbering schemes described in the figure below.
Pi 4 should be same. The recommended one is the BCM pin numbering scheme:
Step 1: Build the prototyping circuit on the breadboard.

Note: The Raspberry Pi should not be powered on when building the prototyping circuit.

1. Select 3 – 330 Ohm (Ω) resistor, 1 RGB LED.


2. 3 jumper wire (any color available), 3 jumper wires from the Raspberry Pi starter
kit.
3. Insert the RGB LED onto the breadboard as shown.
4. Insert the resistors onto the breadboard as shown

You will need the pinout for the RGB led. This has 4 legs on it and it contains 3 colors
within the LED, RED, GREEN, and BLUE. One of the legs is the “common” leg that will be
connected to “ground” on the RPi.

On this example, the common lead is pin 3, which is the longest lead (or leg). Your RGB led
should be the same. The other 3 leads will be attached to pins 23, 24, and 25 on the RPi
and you will control turning them on or off with python code.
Step 2: Complete the prototyping circuit by connecting it to the Raspberry Pi.

Note: The GPIO pins on the Raspberry Pi are male. If available, use male to Female jumper wires
to connect from the breadboard to the Raspberry Pi. Otherwise, use the GPIO ribbon cable,
provided in the starter kit, to convert the GPIO pins to female connectors.

1. Connect one the lead/legs of the RGB LED to the pins on the RPi as shown in the
picture below. The pins are BCM 23, 24, and 25.

2. Connect the common lead/leg to the ground pin, physical pin 20 one on the
Raspberry Pi.

3. Connect resistors to each of the leads as shown in picture.


Part 2: Write the Python Code
With the circuit created we need to write the Python script to blink the LED.
1. With the library installed now open the nano editor, save the file as “rgb_led.py”
a. $ nano rgb_led.py
2. To initialize the GPIO ports on the Raspberry Pi we need to first import the Python
library, then initialize the library and setup pin 11 as an output pin.
Next we need to write code to turn the LED on and off in 1 second intervals by setting the
output pin to either high (on) or low (off). We do this inside an infinite loop so our program
keeps executing until we manually stop it. (Ctrl + C)
To follow the code below, use the GPIO pin positions as:

Blue – pin 25
Green – pin 24
Red – pin 23

Please double-check before you start your system. You don’t want to burn the LEDs or the
Raspberry Pi.

1. On the RPi, open a Terminal window


2. Then use the “nano” editor to write the code.
a. $ nano rgb_led.py
3. Write the code

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
RED = 25
GREEN = 24
BLUE = 23
GPIO.setup(RED,GPIO.OUT)
GPIO.output(RED,0)
GPIO.setup(GREEN,GPIO.OUT)
GPIO.output(GREEN,0)
GPIO.setup(BLUE,GPIO.OUT)
GPIO.output(BLUE,0)
try:
while (True):
request = raw_input(“RGB—>”)
if (len(request) == 3):
GPIO.output(RED,int(request[0]))
GPIO.output(GREEN,int(request[1]))
GPIO.output(BLUE,int(request[2]))
except KeyboardInterrupt:
GPIO.cleanup()

Run the program


$ python3 rgb_led.py

If LED is not blinking, check your wiring and also check your python code for any errors.

You might also like