Raspberry Pi - LED RGB

This tutorial instructs you how to use Raspberry Pi to control RGB LED. In detail, we will learn:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×RGB LED
3×220 ohm resistor
1×(Alternative) RGB LED Module
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Block Shield for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B
1×(Optional) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of RGB LED

The RGB LED is capable of producing any color by combining the three primary colors: red, green, and blue. It is made up of three distinct LEDs ( red, green, and blue) but all contained in a single housing.

The RGB LED Pinout

An RGB LED has four pins:

  • Common (Cathode-) pin which must be connected to GND (0V)
  • R (red) pin to control red
  • G (green) pin to control green
  • B (blue) pin to control blue
RGB LED pinout

If we wanna hook up an RGB LED to an Raspberry Pi, we gotta use current-limiting resistors. It can be kinda tough to wire up, but don't worry. We can use this RGB LED module, that already has resistors built-in.

RGB LED Module also includes four pins:

  • Common (Cathode-) pin: needs to be connected to GND (0V)
  • R (red): pin is used to control red
  • G (green): pin is used to control green
  • B (blue): pin is used to control blue
RGB LED Module Pinout

※ NOTE THAT:

The common type of pin for a RGB LED can vary; it can be either a cathode or anode. This tutorial will use a common cathode pin.

How it works

In the realm of physics, three color values - Red (R), Green (G) and Blue (B) - are combined to form a color. Each of these values has a range from 0 to 255. The combination of three values produces a total of 256 x 256 x 256 colors.

We can make RGB LED displays any color we desire by program Raspberry Pi to generate PWM signals (with duty cycle ranging from 0 to 255) to the R, G, and B pins. The duty cycle of PWM signals sent to the R, G and B pins are in proportion to the Red (R), Green (G) and Blue (B) color values respectively.

Wiring Diagram

  • Wiring diagram between Raspberry Pi and RGB LED
The wiring diagram between Raspberry Pi and RGB LED

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram between Raspberry Pi and RGB LED module
The wiring diagram between Raspberry Pi and RGB LED module

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

How To Control RGB LED

Let's learn how to control the GRB LED to any color, for example #00979D step-by-step:

  • First, determine which color you want to display and get its color code. Tips:
    • You can use the color picker to select the color code you want
    • If you want to use a color from an image, you can use the online Colors From Image tool
  • Then, convert the color code to R, G, B values using the tool from w3school. Make sure to take note of these values. In this case: R = 0, G = 151, B = 157
RGB LED color picker
  • Specify the Raspberry Pi pins that are connected to the R, G, and B pins. For example:
LED_R_PIN = 13 LED_G_PIN = 12 LED_B_PIN = 18
  • Set the Raspberry Pi pins to output mode:
GPIO.setmode(GPIO.BCM) GPIO.setup([LED_R_PIN, LED_G_PIN, LED_B_PIN],GPIO.OUT)
  • Setup the Raspberry Pi pins to output PWM signal with frequency of 1000Hz:
RED = GPIO.PWM(LED_R_PIN, 1000) GREEN = GPIO.PWM(LED_G_PIN, 1000) BLUE = GPIO.PWM(LED_B_PIN, 1000)
  • Control the LED to light up the color #00979D, which corresponds to R = 0, G = 151, B = 157.
RED.ChangeDutyCycle(_map(0, 0, 255, 0, 100)) GREEN.ChangeDutyCycle(_map(151, 0, 255, 0, 100)) BLUE.ChangeDutyCycle(_map(157, 0, 255, 0, 100))

Raspberry Pi - RGB LED Example Code

The Raspberry Pi code below changes the color of the LED in a sequence of the following shades:

  • #00C9CC (Red = 0, Green = 201, Blue = 204)
  • #F7788A (Red = 247, Green = 120, Blue = 138)
  • #34A853 (Red = 52, Green = 168, Blue = 83)
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-led-rgb """ import RPi.GPIO as GPIO from time import sleep LED_R_PIN = 13 LED_G_PIN = 12 LED_B_PIN = 18 GPIO.setmode(GPIO.BCM) GPIO.setup([LED_R_PIN, LED_G_PIN, LED_B_PIN],GPIO.OUT) RED = GPIO.PWM(LED_R_PIN, 1000) GREEN = GPIO.PWM(LED_G_PIN, 1000) BLUE = GPIO.PWM(LED_B_PIN, 1000) def _map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) RED.start(0) GREEN.start(0) BLUE.start(0) try: while True: # color code #00C9CC (R = 0, G = 201, B = 204) RED.ChangeDutyCycle(_map(0, 0, 255, 0, 100)) GREEN.ChangeDutyCycle(_map(201, 0, 255, 0, 100)) BLUE.ChangeDutyCycle(_map(204, 0, 255, 0, 100)) sleep(1) # color code #F7788A (R = 247, G = 120, B = 138) RED.ChangeDutyCycle(_map(247, 0, 255, 0, 100)) GREEN.ChangeDutyCycle(_map(120, 0, 255, 0, 100)) BLUE.ChangeDutyCycle(_map(138, 0, 255, 0, 100)) sleep(1) # color code #34A853 (R = 52, G = 168, B = 83) RED.ChangeDutyCycle(_map(52, 0, 255, 0, 100)) GREEN.ChangeDutyCycle(_map(168, 0, 255, 0, 100)) BLUE.ChangeDutyCycle(_map(83, 0, 255, 0, 100)) sleep(1) finally: RED.stop() GREEN.stop() BLUE.stop() GPIO.cleanup()

When using many colors, we could shorten the Raspberry Pi code by creating a function:

""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-led-rgb """ import RPi.GPIO as GPIO from time import sleep LED_R_PIN = 13 LED_G_PIN = 12 LED_B_PIN = 18 GPIO.setmode(GPIO.BCM) GPIO.setup([LED_R_PIN, LED_G_PIN, LED_B_PIN],GPIO.OUT) RED = GPIO.PWM(LED_R_PIN, 1000) GREEN = GPIO.PWM(LED_G_PIN, 1000) BLUE = GPIO.PWM(LED_B_PIN, 1000) def _map(x, in_min, in_max, out_min, out_max): return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) def setColor(r,g,b): RED.ChangeDutyCycle(_map(r, 0, 255, 0, 100)) GREEN.ChangeDutyCycle(_map(g, 0, 255, 0, 100)) BLUE.ChangeDutyCycle(_map(b, 0, 255, 0, 100)) RED.start(0) GREEN.start(0) BLUE.start(0) try: while True: # color code #00C9CC (R = 0, G = 201, B = 204) setColor(0, 201, 204); sleep(1) # color code #F7788A (R = 247, G = 120, B = 138) setColor(247, 120, 138); sleep(1) # color code #34A853 (R = 52, G = 168, B = 83) setColor(52, 168, 83); sleep(1) finally: RED.stop() GREEN.stop() BLUE.stop() GPIO.cleanup()

Addtional Knowledge

For a RGB LED with a common Anode, you need to:

  • Connect the common pin to 3.3V of a Raspberry Pi.
  • Utilize the analogWrite() function and set the R, G and B values to 255 - R, 255 - G, and 255 - B, respectively.

An RGB LED Strip is made up of a sequence of RCB LEDs connected together. LED strips can be divided into addressable and non-addressable varieties. We will be making tutorials for each type of LED strip.

※ NOTE THAT:

Do not use a single resistor in the common pin of an RGB LED instead of three resistors in the other pins.

It is true that the three LEDs in a single RGB package are in parallel. In theory, it is ok to use a single resistor in the common pin. However, in reality, this is not recommended. This is because the real world LEDs do not have the same characteristics. The three LEDs in the RGB package are NOT identical ⇒ The resistors of the LEDs are different ⇒ The current is not evenly distributed to each LED ⇒ The brightness is not uniform and this could lead to the destruction of one LED, and eventually the other LEDs.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!