RGB LED Controlled by a Potentiometer
There are two types of LEDs: single-colour LEDs and RGB LEDs. While single-colour LEDs emit any one colour, RGB LEDs emit multiple colors. Single-colour LEDs have two leads, positive (anode) and negative (cathode). RGB LEDs have fours leads. They can be setup to have a common cathode or a common anode. In the common cathode setup, there are one negative and three positive leads, one for each of the primary colours (Red, Green, and Blue). In the common anode setup, there are one positive and three negative leads (one for each of the primary colours). By varying the power of the signal supplied to the RGB leads multiple colours can be emitted.
The signal power can be varied physically by attaching a Potentiometer to each of the RGB leads or programatically by sending PWM signals to the RGB leads.
We will look at both approaches in this project.
RGB LEDs have a wide range of applications such as decorative lighting and in circuits where visual feedback through different colors is needed.
Components
Component | Purpose |
---|---|
Arduino Nano | The microcontroller. |
Potentiometer | Used to send a varying signal voltage to the RGB LED. |
RGB LED | The LED controlled by the potentiometer. |
Circuit Diagram
Connections
Code
int redPin = 3; // Red RGB pin -> D3
int greenPin = 5; // Green RGB pin -> D5
int bluePin = 6; // Blue RGB pin -> D6
int potRed = A0; // Potentiometer controls Red pin -> A0
int potGreen = A1; // Potentiometer controls Green pin -> A1
int potBlue = A2; // Potentiometer controls Blue pin -> A2
void setup() {
pinMode(redPin,OUTPUT);
pinMode(bluePin,OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(potRed, INPUT);
pinMode(potGreen, INPUT);
pinMode(potBlue, INPUT);
}
void loop() {
// Reads the current position of the potentiometer and converts
// to a value between 0 and 255 to control the according RGB pin with PWM
// RGB LEDs come with two pin types: Commmon Anode and Common Cathode
// The signal to the pins is provided accordingly with the code snippets below.
// RGB LED COMMON ANODE
analogWrite(redPin, 255-(255./1023.)*analogRead(potRed));
analogWrite(greenPin, 255-(255./1023.)*analogRead(potGreen));
analogWrite(bluePin, 255-(255./1023.)*analogRead(potBlue));
// Uncomment for RGB LED COMMON CATHODE
/*
analogWrite(redPin, (255./1023.)*analogRead(potRed));
analogWrite(greenPin, (255./1023.)*analogRead(potGreen));
analogWrite(bluePin, (255./1023.)*analogRead(potBlue));
*/
delay(10);
}