This will be our first hardware tutorial so far. Setting up the Pi will not be part of this tutorial. The RGB LED modules can emit various colors of light. Three LEDs of red, green, and blue are packaged into a transparent or semitransparent plastic shell with four pins led out. The three primary colors of red, green, and blue can be mixed and compose all kinds of colors by brightness, so you can make an RGB LED emit colorful light by controlling the circuit.


In this experiment, we will use Pulse Width Modulation, or PWM, to control the brightness of RGB. PWM is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off with no intermediary state. This on-off pattern can simulate voltages in between full on (3 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 3v controlling the brightness of the LED. Please note that we will connect VCC to 3.3V and not to 5V.


The oscillogram above shows that the amplitude of DC voltage output is 3.3V. However, the actual voltage output is only 1.65V through PWM, for the high level only takes up 50% of the total voltage within a period (3.3V*0.5 = 1.65V).

The three basic parameters of PWM are easier to understand when visualized:


  1. Duty cycle – proportion of “on” time
  2. Period – number of pulses per second

Thus, using PWM we can cover a voltage amplitude from 0-3.3V, although in reality we can only produce “on” or “off” signals.

RGB LEDs can be categorized into common anode LED and common cathode LED. In this experiment, we use a common cathode RGB LED. The schematic diagram of the module is as shown below:


Notice that the current will flow from VCC through the LED’s cathode, through the GPIO pin and finally to ground. That means that the LED will light up when the GPIO outputs 0V. A PWM of 0% will light up the led with maximal current flow.

We will be using the RPi.GPIO library which provide software PWM utility. Here are the main functions:

# Create a PWM instance
p = GPIO.PWM(channel, frequency)

# Start PWM
p.start(dc)  # dc is the duty cycle (0.0 <= dc <= 100.0)

# Change the frequency
p.ChangeFrequency(freq)  # freq is the new frequency in Hz

# Change the duty cycle
p.ChangeDutyCycle(dc)  # 0.0 <= dc <= 100.0

# Stop PWM

Experiment 1: Single LED

Let’s start with a very simple PWM example and just one LED.

Step 1

Build the circuit according to the following method. Please refer to the pinout below for the hardware connection.

Note: Check first where pin 1 is and then go to the wanted pin.



Raspberry Pi RGB LED Module
3.3V VCC
No connection G
No connection B

You can choose any of the three available LEDs. We will start with the red one.

Step 2

Run the code below. You should see the LED dim, and brighten.

import time
import RPi.GPIO as GPIO

LedPin = 11
GPIO.setup(LedPin, GPIO.OUT)

# We want here to let that led blink 50 times each second
# channel = LedPin , frequency=50Hz
p = GPIO.PWM(LedPin, 50)  

# start with duty cyle = 0%
    while 1:
        for dc in range(0, 101, 5):
        for dc in range(100, -1, -5):
except KeyboardInterrupt:

Experiment 2: 3 LEDs

Step 1

Build the circuit according to the following method:

Raspberry Pi RGB LED Module
3.3V VCC

Step 2

Here we input any value between 0 and 255 to the three pins of the RGB LED to make it display different colors. Run the code below and you will see LED light up, and display different colors in turn.

Now it is your turn to play with the code. So feel free to take the code apart and modify it as you want.