Lesson 5 – Lighting up an RGB LED

Booting up the Raspberry Pi

  1. This section assumes that you have read through “Lesson 0 – Setup” and have a working Raspberry Pi you can connect to remotely using SSH or VNC.
  2. If that’s not the case please head back to “Lesson 0 – Setup” and work through the instructions provided.
  3. Let’s now get started and boot up the Raspberry Pi.
  4. Grab a good quality USB cable and a USB power adaptor (2.5A).
  5. Plug one end of the USB cable into the plug and the other microUSB end into the Raspberry Pi.
  6. This should now power up the Raspberry Pi.
  7. Once the Raspberry Pi has booted up, please ensure that it is able to connect it to the network so that you can access it over VNC.
  8. If you are using a local monitor connected to the Raspberry Pi, you are all sorted.
  9. Else get connected to the Raspberry Pi using VNC.

Important Concepts

01 / What is an RGB LED – RGB LED stands for Red, Green and Blue LEDs. RGB LED products combine these three colors to produce over 16 million hues of light. Some colors are “outside” the triangle formed by the RGB LEDs. Also, pigment colors such as brown or pink are difficult, or impossible, to achieve. In fact, an RGB LED is a combination of 3 LEDs in just one package that allows the programmer to display all sorts of colors using the Red, Gree, Blue combination. Here’s what an RGB LED might look like.

The three terminals you see are Red, Ground, Green and Blue. This particular LED is a common Cathode ( -ve) LED.

There are common anode RGB LEDs and common cathode RGB LEDs. See figure below to understand what each of them might look like :

As you can see, the 3 LEDs can share the cathode or the anode depending on whether they are a common anode or a common cathode type. This results in an RGB LED that has 4 pins, one for each LED, and one common cathode or one common anode. The common anode RGB LED is the most popular type.

Let’s put together the circuit

  1. Our next tutorial is called, “Lighting up an RGB LED”.
  2. We will start off this tutorial by first putting together the circuit using a breadboard.
  3. We will wire up an RGB LED, connect it up to the Raspberry Pi through 3 x 220 Ohm resistors, power it up and write some python code.

In our previous example we’ve used the PWM signal to gradually increase and decrease the brightness of an LED. In this lesson, we will use a similar Pulse Width Modulation (PWM) approach to control the brightness of an RGB LED getting it to light-up in various kinds of colors. We will generate a PWM signal and vary the duty time of the signal to change the rate at which the RGB LED brightness increases and decreases. We use 3 x 220 Ohm resistors to limit the current flowing through the LED. Now, let’s get started.

Components required for this tutorial include –

  1. 1 Raspberry Pi
  2. 1 Breadboard
  3. 1 Raspberry Pi Cobbler / T-Extension Board
  4. 1 40-Pin Cable to connect the Raspberry Pi Cobbler / T-Extension Board to the Raspberry Pi
  5. 1 RGB LED (Common Cathode type)
  6. 3 x Resistor (220Ω)
  7. Dupont jumper wires

PLEASE NOTEPlease make sure you have disconnected your breadboard from the Raspberry Pi before commencing build of the circuit. Once you have put the circuit together, get someone around you to review the circuit and confirm that the connections are proper before you proceed and power up the breadboard.

Wiring Up

Lets wire up your circuit based on the fritzing diagram below. Your connections should look like the following –

  1. Setup the Raspberry Pi Cobbler / T-Extension Board on the Breadboard.
    1. See the fritzing diagram below to understand what a suitable setup might look like.
    2. Note that one pair of the Ground/3.3V connects to the upper two (horizontally connected) rows of the breadboard.
    3. The second pair of the Ground/5V connects to the lower two (horizontally connected) rows of the breadboard.
  2. Let’s now wire up the RGB LED to our circuit. Wire up the 3 x 220Ω resistors to the Red, Green and Blue Legs of the LED.
  3. There are two types of RGB LED’s i.e. common anode and common cathode. Ours is a common cathode type RGB LED.
  4. Each of the R (Pin 1) – G (Pin 3) – B (Pin 4)  legs are connected to the B17, B18, B27 pins on the Raspberry Pi through the 220 Ohm resistor.
  5. Please make sure you connect the relevant RGB pins to the relevant pins on the Raspberry Pi Cobbler / T-Extension Board
  6. Pin 2 of the RGB LED is the common cathode and needs to be connected to the ground of the Raspberry Pi

Refer to the fritzing diagram below for a view of all of the connections that need to be made for this tutorial.

How Does It Work

We can see from the schematic diagram below that each of the Anode legs (Red, Green, Blue) of the LED connects to a 220 Ohm current-limiting resistor and then to pins B17, B18, B27 (on the Raspberry Pi using the Raspberry Pi Cobbler / T-Extension Board). Therefore, to turn on the LED, we need to program the B17, B18. B27 pins on the Raspberry Pi to be high (3.3 Volt). This can be achieved using a few different programming languages but in our case we will be using Python and we will use the Pulse Width Modulation approach to gradually increase, gradually decrease brightness of the LED in a continuous cycle.

When you are done implementing the code, why not vary the PWM duty cycle and come up with some interesting lighting patterns out. Build on the example given below and try creating different lighting patterns. Here’s what the real world project will look like once you have wired up all the relevant components i.e. Raspberry Pi 3B+, Raspberry Pi Cobbler / T-Extension Board, breadboard, 3 x 220 Ohm resistors and 1 x RGB LED.

Let’s Write Some Python Code

Open up the Thonny editor on your Raspberry Pi and let’s start putting together some code……

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
# Set up a color table in Hexadecimal
COLOR = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]
# Set pins' channels with dictionary
pins = {'Red':17, 'Green':18, 'Blue':27}
def print_message():
	print ("========================================")
	print ("|              Breath LED              |")
	print ("|    ------------------------------    |")
	print ("|       Red Pin connect to GPIO0       |")
	print ("|      Green Pin connect to GPIO1      |")
	print ("|       Blue Pin connect to GPIO2      |")
	print ("|                                      |")
	print ("|  Make a RGB LED emits various color  |")
	print ("|                                      |")
	print ("|                            SunFounder|")
	print ("========================================\n")
	print 'Program is running...'
	print 'Please press Ctrl+C to end the program...'
	raw_input ("Press Enter to begin\n")
def setup():
	global p_R, p_G, p_B
	# Set the GPIO modes to BCM Numbering
	# Set all LedPin's mode to output, 
	# and initial level to High(3.3v)
	for i in pins:
		GPIO.setup(pins[i], GPIO.OUT, initial=GPIO.HIGH)
	# Set all led as pwm channel,
	#  and frequece to 2KHz
	p_R = GPIO.PWM(pins['Red'], 2000)
	p_G = GPIO.PWM(pins['Green'], 2000)
	p_B = GPIO.PWM(pins['Blue'], 2000)
	# Set all begin with value 0
# Define a MAP function for mapping values.
# Like from 0~255 to 0~100
def MAP(x, in_min, in_max, out_min, out_max):
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# Define a function to set up colors 
# input color should be Hexadecimal with 
# red value, blue value, green value.
def setColor(color):
	# Devide colors from 'color' veriable
	R_val = (color & 0xFF0000) >> 16
	G_val = (color & 0x00FF00) >> 8
	B_val = (color & 0x0000FF) >> 0
	# Map color value from 0~255 to 0~100
	R_val = MAP(R_val, 0, 255, 0, 100)
	G_val = MAP(G_val, 0, 255, 0, 100)
	B_val = MAP(B_val, 0, 255, 0, 100)
	# Change the colors
	print "color_msg: R_val = %s,	G_val = %s,	B_val = %s"%(R_val, G_val, B_val)	 
def main():
	while True:
		for color in COLOR:
def destroy():
	# Stop all pwm channel
	# Turn off all LEDs
	GPIO.output(pins, GPIO.HIGH)
	# Release resource
# If run this script directly, do:
if __name__ == '__main__':
	# When 'Ctrl+C' is pressed, the child program 
	# destroy() will be  executed.
	except KeyboardInterrupt:

See a PDF copy of the tutorial here – <Link>

About the Raspberry Pi

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.

The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.

You can read more about the Raspberry Pi here – RaspberryPi.org.


  1. This development track is based on the Rasbperry Pi and the SunFounder Super Starter Kit v3.0 for the Raspberry Pi.
  2. You will need access to both the Raspberry Pi 3 B and the electronics components part of the SunFounder Super Starter Kit v3.0 for the Raspberry Pi  kit to be able to work on these tutorials.
  3. If you haven’t purchased the Raspberry Pi 3 B yet please head over to our store and purchase one now. You can pick up the SunFounder Super Starter Kit v3.0 for the Raspberry Pi from SunFounder’s website.
  4. Depending on where you live you might also be able to pick up the Raspberry Pi and SunFounder Super Starter Kit v3.0 for the Raspberry Pi at your local electronics hobby store.
  5. You can read more about the Raspberry Pi here – RaspberryPi.org.