RGB LED | Arduino Tutorial
Maybe your like
- TUTORIALS
- HARDWARE & TOOLS
- REFERENCES
- FAQs
- ABOUT US
SENSORS/ACTUATORS
- Arduino - Software Installization
- Arduino - Hardware Preparation
- Arduino - Hello World
- Arduino - Code Structure
- Arduino - Serial Monitor
- Arduino - Serial Plotter
- Arduino - LED - Blink
- Arduino - LED - Blink Without Delay
- Arduino - Blink multiple LED
- Arduino - LED - Fade
- Arduino - RGB LED
- Arduino - Traffic Light
- Arduino - Button
- Arduino - Button - Debounce
- Arduino - Button - Long Press Short Press
- Arduino multiple Button
- Arduino - Switch
- Arduino - Limit Switch
- Arduino - DIP Switch
- Arduino - Button - LED
- Arduino - Button - Relay
- Arduino - Button Toggle LED
- Arduino - Button Toggle Relay
- Arduino - Button - Piezo Buzzer
- Arduino - Button - Servo Motor
- Arduino - Potentiometer
- Arduino - Potentiometer fade LED
- Arduino - Potentiometer Triggers LED
- Arduino - Potentiometer Triggers Relay
- Arduino - Potentiometer Triggers Piezo Buzzer
- Arduino - Potentiometer Triggers Servo Motor
- Arduino - Rotary Encoder
- Arduino - Rotary Encoder LED
- Arduino - Rotary Encoder - Servo Motor
- Arduino - Piezo Buzzer
- Arduino - Buzzer
- Arduino - Motor
- Arduino - DC Motor
- Arduino - DC Motor - Limit Switch
- Arduino - Servo Motor
- Arduino - MG996R
- Arduino - Servo Motor controlled by Potentiometer
- Arduino - Light Sensor
- Arduino - LDR Module
- Arduino - Light Sensor Triggers LED
- Arduino - Light Sensor Triggers Relay
- Arduino - Light Sensor Triggers Servo Motor
- Arduino - Ultrasonic Sensor
- Arduino - Ultrasonic Sensor - LED
- Arduino - Ultrasonic Sensor - Relay
- Arduino - Ultrasonic Sensor - Piezo Buzzer
- Arduino - Ultrasonic Sensor - Servo Motor
- Arduino - Ultrasonic Sensor - LCD
- Arduino - Ultrasonic Sensor - OLED
- Arduino - Motion Sensor
- Arduino - Motion Sensor - LED
- Arduino - Motion Sensor - Relay
- Arduino - Motion Sensor - Piezo Buzzer
- Arduino - Motion Sensor - Servo Motor
- Arduino - Motion Sensor - MP3 Player
- Arduino - Motion Sensor - LED Strip
- Arduino - Relay
- Arduino - Relay Shield
- Arduino - 2-Channel Relay Module
- Arduino - 4-Channel Relay Module
- Arduino - Controls Fan
- Arduino - Controls Heating Element
- Arduino - Car
- Arduino - Actuator
- Arduino - Actuator with Feedback
- Arduino - Joystick
- Arduino - Joystick - Servo Motor
- Arduino - Soil Moisture Sensor
- Arduino - Soil Moisture Sensor Pump
- Arduino - Automatic Irrigation System
- Arduino - LCD
- Arduino - LCD I2C
- Arduino - LCD 20x4
- Arduino - LCD Keypad Shield
- Arduino - OLED
- Arduino - TFT LCD Display
- Arduino - TFT LCD Touch Screen Display
- Arduino - Round Circular TFT LCD Display
- Arduino - Button Count - OLED
- Arduino - Button Count - LCD
- Arduino - 74HC595 4-Digit 7-Segment Display
- Arduino - TM1637 4-Digit 7-Segment Display
- Arduino - LED Matrix
- Arduino - Bluetooth LED Matrix
- Arduino - LED Matrix via Web
- Arduino - Keypad
- Arduino - Keypad 1x4
- Arduino - Keypad - LCD
- Arduino - Keypad - Beep
- Arduino - Keypad - Buzzer
- Arduino - Keypad - Relay
- Arduino - Keypad - Servo Motor
- Arduino - Keypad - Solenoid Lock
- Arduino - Keypad - Electromagnetic Lock
- Arduino - Temperature Sensor
- Arduino - Temperature Sensor - LCD
- Arduino - Temperature Sensor - OLED
- Arduino - Temperature Sensor - LED Matrix
- Arduino - Temperature via Web
- Arduino - Temperature Sensor - Servo Motor
- Arduino - Temperature Sensor - Relay
- Arduino - DHT11
- Arduino - DHT22
- Arduino - Temperature Humidity Sensor
- Arduino - Temperature Humidity Sensor - LCD
- Arduino - Temperature Humidity Sensor - OLED Display
- Arduino - LM35 Temperature Sensor
- Arduino - TMP36 Temperature Sensor
- Arduino - Display Temperature from LM35 Sensor on OLED
- Arduino - Display Temperature from LM35 Sensor on LCD
- Arduino - Control Temperature
- Arduino - Cooling System using DHT Sensor
- Arduino - Cooling System using DS18B20 Temperature Sensor
- Arduino - Heating System
- Arduino - DHT11 - Relay
- Arduino - DHT11 - OLED
- Arduino - DHT11 - LCD
- Arduino - DHT22 - Relay
- Arduino - DHT22 - OLED
- Arduino - DHT22 - LCD
- Arduino - Touch Sensor
- Arduino - Touch Sensor - LED
- Arduino - Touch Sensor - Relay
- Arduino - Touch Sensor Toggle LED
- Arduino - Touch Sensor Toggle Relay
- Arduino - Touch Sensor - Piezo Buzzer
- Arduino - Touch Sensor - Servo Motor
- Arduino - Touch Sensor - Solenoid Lock
- Arduino - Door Sensor
- Arduino - Door Sensor - LED
- Arduino - Door Sensor - Relay
- Arduino - Door Sensor Toggle LED
- Arduino - Door Sensor Toggle Relay
- Arduino - Door Sensor - Piezo Buzzer
- Arduino - Door Sensor - Servo Motor
- Arduino - Solenoid Lock
- Arduino - Electromagnetic Lock
- Arduino - Button Controls Electromagnetic Lock
- Arduino - Keypad Door Lock
- Arduino - Door Lock System using Password
- Arduino - Controls Door Lock via Web
- Arduino - RFID/NFC
- Arduino - RFID/NFC - Relay
- Arduino - RFID/NFC - Servo Motor
- Arduino - RFID - Solenoid Lock
- Arduino - RFID - Electromagnetic Lock
- Arduino - RFID/NFC Door Lock System
- Arduino - RTC
- Arduino - DS1307 RTC Module
- Arduino - OLED Clock
- Arduino - LCD Clock
- Arduino - 7-segment Clock
- Arduino - Water Sensor
- Arduino - Water Leak Detector
- Arduino - Water Sensor Relay
- Arduino - Water Sensor Pump
- Arduino - Water/Liquid Valve
- Arduino - Controls Pump
- Arduino - Button - Pump
- Arduino - Rain Sensor
- Arduino - Rain Sensor - LED
- Arduino - Rain Sensor - Relay
- Arduino - Rain Sensor - Buzzer
- Arduino - Rain Sensor - Servo Motor
- Arduino - Measure Voltage
- Arduino - Sound Sensor
- Arduino - Sound Sensor - LED
- Arduino - Sound Sensor - Relay
- Arduino - Sound Sensor - Servo Motor
- Arduino - Force Sensor
- Arduino - Flame Sensor
- Arduino Fire Alarm
- Arduino - Gas Sensor
- Arduino - Gas Sensor - Relay
- Arduino - Infrared Obstacle Avoidance Sensor
- Arduino - Stepper Motor
- Arduino - Controls 28BYJ-48 Stepper Motor using ULN2003 Driver
- Arduino - Controls Stepper Motor using L298N Driver
- Arduino - DRV8825 Stepper Motor Driver
- Arduino - Stepper Motor and Limit Switch
- Arduino - GPS
- Arduino - LED Strip
- Arduino - NeoPixel LED Strip
- Arduino - WS2812B LED Strip
- Arduino - Dotstar Led Strip
- Arduino - Micro SD Card
- Arduino - Write Variable to SD Card
- Arduino - Log Data with Timestamp to SD Card
- Arduino - Read Config from SD Card
- Arduino - Audio
- Arduino - MP3 Player
- Arduino - RFID MP3 Player
- Arduino - Siren
- Arduino - SoftwareSerial
- Arduino RS232
- Arduino RS485
- Arduino RS422
- Arduino Serial to WiFi Converter
- Arduino RS232 to WiFi
- Arduino RS485 to WiFi
- Arduino RS422 to WiFi
- Arduino - IR Remote Control
- Arduino - Bluetooth
- Arduino - BLE
- Arduino controls Servo Motor via Bluetooth
- Arduino controls LED via Bluetooth
- How to get started with Arduino Uno R4
- Arduino Uno R4 WiFi controls LED via Web
- Arduino Uno R4 WiFi controls Relay via Web
- Arduino Uno R4 LED Matrix displays Number Character
INTERNET OF THING (IoT)
- Arduino - Ethernet
- Arduino - Ethernet Module
- Arduino - Ethernet Shield 2
- Arduino - PHPoC Shield
- Arduino - WiFi
- Arduino - TCP Client
- Arduino - Web Server
- Arduino - Web Server Multiple Pages
- Arduino - Web Server Password
- Arduino - WebSocket
- Arduino - Web Plotter
- Arduino - Controls Servo Motor via Web
- Arduino - Controls Car via Web
- Arduino - IoT
- Arduino - MQTT
- Arduino Shield
- Arduino - HTTP Request
- Arduino - HTTPS Request
- Arduino - MySQL
- Arduino - Modbus
- Communication between two Arduino
- Arduino to Arduino MQTT Communication
- Arduino - IFTTT
- Arduino - Make Voice Phone Call
- Arduino - Send SMS Message
- Arduino - Gmail
- Arduino - Door Open Email Notification
- Arduino - Motion Sensor Email Notification
- Arduino - Water Leak Email Notification
- Arduino - Secure Your Belongings: Detect Theft and Send Email Alerts
- Arduino - Send Email
- Arduino - Door Open - Send Email Notification
- Arduino - Temperature - Send Email Notification
LIBRARIES
- Arduino - Button Library
- Example - 01.Single Button
- Example - 02.Single Button Events
- Example - 03.Single Button Debounce
- Example - 04.Single Button All
- Example - 05.Multiple Button All
- Example - 06.Button Count
- Example - 07.Button Array
- Arduino - LED Library
- Example - LED Blink
- Example - LED Blink In Period
- Example - LED Blink Number Of Times
- Example - LED Fade In Fade Out
- Example - LED On Off
- Example - LED Toggle
- Example - Multiple LED
- Example - LED Array
- Arduino - Output Library
- Example - 01.On Off
- Example - 02.Toggle
- Example - 03.Single Blink Without Delay
- Example - 04.Single Blink Change Frequency
- Example - 05.Multiple Blink Without Delay
- Example - 06.Multiple Blink With Offset
- Example - 07.Blink In Period
- Example - 08.Pulse
- Arduino - Buzzer Library
- Example - Beep
- Example - Melody
- Example - Melody Repeat
- Arduino - Analog Keypad Library
- Example - Analog Keypad
- Example - Analog Button Array
In this tutorial, we are going to learn:
- How RGB LED works.
- How to connect the RGB LED to Arduino.
- How to program Arduino to control the color of RGB LED.
Hardware Required
| 1 | × | Official Arduino Uno |
| 1 | × | Alternatively, DIYables STEM V3, Fully Compatible with Arduino Uno R3 |
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) |
| 1 | × | USB 2.0 cable type C/B (for USB-C PC) |
| 1 | × | RGB LED |
| 1 | × | Alternatively, RGB LED Module |
| 1 | × | Breadboard |
| 1 | × | Jumper Wires |
| 1 | × | Recommended: Screw Terminal Block Shield for Arduino Uno |
| 1 | × | Recommended: Breadboard Shield for Arduino Uno |
| 1 | × | Recommended: Enclosure for Arduino Uno |
| 1 | × | Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO |
Or you can buy the following kits:
| 1 | × | DIYables STEM V3 Starter Kit (Arduino included) |
| 1 | × | DIYables Sensor Kit (30 sensors/displays) |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
About RGB LED
The RGB LED can emit any colors by mixing the 3 basic colors red, green and blue. Actually, it consists of 3 separate LEDs red, green and blue packed together in a single case.
Pinout
RGB LED 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
To connect RGB LED to Arduino, we need to use current-limiting resistors. This can make the wiring complex. Fortunately, we can use the RGB LED module, which already has built-in current-limiting resistors.
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
※ NOTE THAT:
The common pin can be cathode or anode, depending of the RGB LED type. This tutorial uses a common cathode one.
How it works
In the nature of physics, a color is composed of three color values: Red (R), Grean (G) and Blue (B). Each color value ranges from 0 to 255.
The mix of three values creates 256 x 256 x 256 colors in total.
If we provide PWM signals (with duty cycle from 0 to 255) to R, G, B pins, we can makes RGB LED displays any color we want.
The duty cycle of PWM signals to R, G and B pins correspond to color values of Red (R), Grean (G) and Blue (B)
Wiring Diagram
- Wiring diagram between Arduino and RGB LED

This image is created using Fritzing. Click to enlarge image
- Wiring diagram between Arduino and RGB LED module

This image is created using Fritzing. Click to enlarge image
How To Control RGB LED
Let's lern step-by-step how to control the GRB LED to any color, for example #00979D:
- Determine which color you want to display, get its color code. Tips:
- You can pick up color code you want from the color picker
- If you want to use color in an image, use online Colors From Image tool
- Convert color code to R, G, B values using the tool from w3school. Take note these values. in this case: R = 0, G = 151, B = 157
- Define Arduino pins that connects to R, G, and B pins. For example:
- Configure these Arduino pins to the output mode
- Control LED to emit that color (#00979D → R = 0, G = 151, B = 157)
Arduino - RGB LED Example Code
The below code changes color of LED among following colors in sequence:
- #00C9CC (R = 0, G = 201, B = 204)
- #F7788A (R = 247, G = 120, B = 138)
- #34A853 (R = 52, G = 168, B = 83)
When using many colors, we could shorten the code by creating a function:
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rgb-led */ const int PIN_RED = 5; const int PIN_GREEN = 6; const int PIN_BLUE = 9; void setup() { pinMode(PIN_RED, OUTPUT); pinMode(PIN_GREEN, OUTPUT); pinMode(PIN_BLUE, OUTPUT); } void loop() { // color code #00C9CC (R = 0, G = 201, B = 204) setColor(0, 201, 204); delay(1000); // keep the color 1 second // color code #F7788A (R = 247, G = 120, B = 138) setColor(247, 120, 138); delay(1000); // keep the color 1 second // color code #34A853 (R = 52, G = 168, B = 83) setColor(52, 168, 83); delay(1000); // keep the color 1 second } void setColor(int R, int G, int B) { analogWrite(PIN_RED, R); analogWrite(PIN_GREEN, G); analogWrite(PIN_BLUE, B); }Addtional Knowledge
- For RGB LED with common Anode, you need to:
- Connect the common pin to 3.3V of Arduino.
- Change R, G and B values in analogWrite() function to 255 - R, 255 - G, and 255 - B, respectively
- A sequences of RCB LED connected together creates the RGB LED Strip. LED strip can be categorized in to the addressable LED strip and non-addressable LED Strip. We are going to make tutorials for each types of LED strip.
※ NOTE THAT:
Avoid using a single resistor in the common pin of RGB LED instead of three resistors in the other pins.
As we know, three LEDs in a single RGB package are in parallel. In ideal conditions, It is ok to use a single resistor in the common pin. However, in practice, do not use it. That is because the real world LED doesn't have the same characteristics. Three LEDs in the RGB package are NOT identical ⇒ Resistors of LEDs are different ⇒ The current is distributed unequally to each LED ⇒ Brightness is not the same and this may destroy a LED, and then destroy the other LEDs.
The Best Arduino Starter Kit
- See the best Arduino kit for beginner
See Also
- Arduino - LED - Blink
- Arduino - LED - Blink Without Delay
- Arduino - Blink multiple LED
- Arduino - LED - Fade
- Arduino - Traffic Light
- Arduino - Button - LED
- Arduino - Button Toggle LED
- Arduino - Potentiometer fade LED
- Arduino - Potentiometer Triggers LED
- Arduino - Light Sensor Triggers LED
- Arduino - Ultrasonic Sensor - LED
- Arduino - Motion Sensor - LED
- Arduino - Touch Sensor - LED
- Arduino - Touch Sensor Toggle LED
- Arduino - Door Sensor - LED
- Arduino - Door Sensor Toggle LED
- Arduino - Rain Sensor - LED
- Arduino - Sound Sensor - LED
- Arduino - LED Strip
- Arduino - NeoPixel LED Strip
- Arduino - WS2812B LED Strip
- Arduino - Dotstar Led Strip
- Arduino controls LED via Bluetooth
- Arduino Uno R4 WiFi controls LED via Web
※ OUR MESSAGES
- We are AVAILABLE for HIRE. See how to hire us to build your project
- If this tutorial is useful for you, please give us motivation to make more tutorials.
- You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!
TABLE OF CONTENTS
- Hardware Required
- About RGB LED
- Wiring Diagram
- How To Control RGB LED
- Arduino - RGB LED Example Code
- Addtional Knowledge
- The Best Arduino Starter Kit
- See Also
Tag » Arduino Lesson 4 Rgb Led Code
-
Lesson 4 - RGB LED - YouTube
-
Getting Started With The Elegoo Mega 2560, Lesson 4 RGB LED
-
Lesson 4 RGB LED - GitHub
-
[PDF] Lesson 4 RGB LED - Fab Academy
-
Lesson 4 - RGB LED - Getting Started With Arduino - Research Guides
-
Arduino Tutorial #4: RGB LED - Legacy Personal Blogs
-
Arduino Lesson 4. Eight LEDs And A Shift Register
-
How To Use A RGB LED With Arduino | Tutorial - How To Mechatronics
-
Arduino RGB LED Tutorial - Starting Electronics
-
How To Use An RGB LED - Arduino Tutorial : 4 Steps (with Pictures)
-
Arduino Lesson 4 - Control An RGB LED With PWM - Adeept Video
-
Lesson 4: The Arduino Programming Environment - Tech Explorations
-
Arduino Tutorial #4: RGB LEDs - Pinterest
-
[PDF] Arduino: RGB LEDs Diagrams & Code - Brown County Library