Arduino 102 for intermediates 

RGB LED Project

In this project, you'll learn how to use an RGB LED with your Arduino board. An RGB LED combines red, green, and blue LEDs in a single package, allowing you to create a wide range of colors by adjusting the brightness of each component. We'll be using a common cathode RGB LED in this tutorial.

RGB LED 

You can use the RGB(Red-Green-Blue) lights to add more functionalities to your project. You can also obtain additional colors like Magenta, Cyan, Yellow, and White by combining the basic RGB colors. The basic RGB LED is available in two configurations, common anode and common cathode.  In this tutorial, we will use the common cathode RGB.

Understanding the Common Cathode RGB LED

A common cathode RGB LED has four pins: one common cathode (negative terminal) and three anodes for the red, green, and blue LEDs. In this configuration, the common cathode is connected to a ground (GND), and the individual color pins (red, green, blue) are controlled by applying a voltage to each pin. By varying the intensity of each color using Pulse Width Modulation (PWM), you can create a wide range of colors by mixing the red, green, and blue light.

A Common Cathode RGB LED has four legs:

Components Needed



Circuit Connection

Note: Use a breadboard for easier connections.

Arduino RGB LED Circuit 

Writing the Code

Start the Arduino IDE

  This will open a template sketch

 Note: the folder name must be the same as the file name.

 Inside the new sketch, before the “void setup” add the following pin initialization code:

  //RGB LED pins (~PWM)

       int green = 9;    //LED red

       int blue = 10;    //LED green

       int red = 11;     //LED blue

Red -> Blue -> Green -> White (two seconds delay between each color. )

Have fun by changing the PWM values from 0 to 255 to change the brightness of the LED and try to combine the basic RGB colors to get a new color.

-What do you get if you combine RED and GREEN? 

Hint: (red, 255), (green, 255), (blue, 0)


Customizing Colors

You can create any color by adjusting the values for red, green, and blue in the setColor() function. For example:


Conclusion

This project demonstrates how to use an RGB LED with an Arduino to create various colors by mixing red, green, and blue light. By adjusting the PWM values for each color component, you can create a wide range of colors for your projects.


For further exploration, try modifying the code to create different color patterns or add user input to control the LED color. The official Arduino website and community resources offer a wealth of information and inspiration for your next project. Happy experimenting!

Enter the following code into the Arduino IDE 

/* RGB LED TEST

  By. Fekadu Debebe

  April 21/2019

*/

//RGB LED pins (~PWM)

int green = 9;    //LED red

int blue = 10;    //LED green

int red = 11;     //LED blue


// Setup instructions

void setup()

{

  Serial.begin(9600); // serial monitor

  // Arduino pins input or output

  pinMode(red, OUTPUT);

  pinMode(green, OUTPUT);

  pinMode(blue, OUTPUT);

}

// Main loop

void loop()

{

  // PWM value from 0 to 255

  //Red, bright

  analogWrite(red, 255);  // 100% PWM

  analogWrite(green, 0);  // Green OFF

  analogWrite(blue, 0);   // Blue OFF

  delay(2000);            // wait for 2 sec

  // blue, bright

  analogWrite(red, 0);

  analogWrite(green, 0);

  analogWrite(blue, 255);

  delay(2000);

  //Green, bright

  analogWrite(red, 0);

  analogWrite(green, 255);

  analogWrite(blue, 0);

  delay(2000);

  // white, bright

  analogWrite(red, 255);

  analogWrite(green, 255);

  analogWrite(blue, 255);

  delay(2000);

}

You can also create a function to set the RGB color

// Define pins for the RGB LED

const int greenPin = 9;

const int bluePin = 10;

const int redPin = 11;

void setup() {

  // Initialize the pins as outputs

  pinMode(redPin, OUTPUT);

  pinMode(greenPin, OUTPUT);

  pinMode(bluePin, OUTPUT);

}

void loop() {

  // Set the RGB LED to red using the setColor subroutine

  setColor(255, 0, 0);  // Red

  delay(1000);

  setColor(0, 255, 0);  // Red

  delay(1000);

  setColor(0, 0, 255); // Red

  delay(1000);

  setColor(0, 0, 0); // Red

  delay(1000);

  //Add similar subroutine callouts

}

// Function to set the color of the RGB LED

void setColor(int red, int green, int blue) {

  analogWrite(redPin, red);

  analogWrite(greenPin, green);

  analogWrite(bluePin, blue);

}

Seven Segment Display

Seven-segment LED displays are most frequently used to generate numbers (0–9), but they also can be used to display hexadecimal (0–9, A,B, C, D, E, F). A seven-segment display is an electronic display that can produce decimal numerals from 0 to 9 and alphabets. Seven-segment displays consist of 7 LED also known as segments and a dot on its bottom right side which serves as a decimal point. The segments are labeled with letters A to G and DP for the decimal point.  

There are two types of seven-segment display, common anode (+) and common cathode (-).

Common Anode Displays

In this configuration, all the anode (+) legs are connected to Vcc and individual segments are turned on and off by controlling the cathodes. In common Anode the Cathode (-) side of LEDs is connected to a,b,c,d,e,f,g pins of the segment display.​ To drive a given segment of a common anode display, current must be taken out of the corresponding segment’s terminal (sink).

Common Cathode Displays

In this configuration, all the cathode (-) legs are connected to Vcc or Arduino GPIO, and individual segments are turned on and off by controlling the anodes. In common cathode displays, all of the cathodes are connected to the ground, and individual segments are turned on and off by switching power to the anodes. To drive a given segment of the common cathode display, current must be provided into the corresponding segment’s terminal (source). 

​Seven Segment Display HP 5082-7760 Package Drawing - G

For this project, we will be using the HP 5082-7760 seven-segment display (common cathode). 

Most seven segment displays like the HP 5082-7760 series, they may work without current limiting resistors; however, it’s always recommended to use the appropriate resistor based on the datasheet. Typically for a standard red 7-segment display, each LED segment can draw about 15 mA. Using the Ohms law, R = V/I and values from the datasheet, we can determine the resistor value.  

Determining LED resistor value

Our seven-segment display will need resistors at each leg to prevent the LED from drawing too much current. Our datasheet tells us each LED has a maximum forward voltage of 150mA, so we use a little less to save the LED a bit, we're picking 125mA. Using Ohms Law we can easily determine the resistance needed.

Where:

R = the resistance we want to calculate.

Vs = the source voltage (Arduino provides a 5V logic level).

V_led = the voltage drop across our LED (datasheet shows 1.6V).

I_led = the current through the resistor (our datasheet shows 150mA)

From this calculation we can see that the seven-segment display requires very low resistor value of 22.67 ohm. 

We need to put resistors at each anode pins of the seven-segment display. In our case, we can use a 22-ohm resistors or because the resistance value is too small, we can omit these resistors. Some online tutorials show using only one resistor at the cathode pin; however, this is not a recommended practice as the LED's would get different amounts of current for every digit you're trying to display. If you would only display the same digit every time, you can get away by calculating the total current resistor value but because each LED in our case is individually addressable; therefore, we need to put a resistor at all the anode pins or omit the resistors if the calculated resistance is too low. 

HP 5082-7760 7 Segment Display Pinout

Now let’s go over the Seven-Segment configuration so we know which pins light up which segments. The pinout for the 7-segment display is as follows.


Pins a, b, c, d, e, f, g and DP segment (decimal point) will be connected to digital pins of Arduino. By controlling each LED on the segment connected, numbers can be displayed on the 7-segment.

Pin 3 and 14 are internally connected to form a common ground pin. This pin should be connected to GND in the case of common cathode 7-segment display. 

Display control

In order to turn a specific segment, 5V will be supplied to the desired segment (A,B,C,D,E,F, or G). To get a digital number readout, the particular set of LEDs will be turned ON. For instance, to display the numerical digit 0, we will need to light up six of the LED segments (A,B,C,D,E,and F). Similarly, all the digits from ‘0 through 9’ and hexadecimal characters from ‘A through F’ can be displayed using a 7-segment display. 

Circuit Connection and Simulation

This project utilizes an Arduino UNO and a common cathode seven-segment display to show digits from 0 to 9. The setup involves connecting the seven-segment display to the Arduino UNO using resistors to limit current and ensure safe operation. Here’s a detailed guide to creating this project.

Components Needed:

​Wiring Diagram:

The attached GIF illustrates the wiring connections between the Arduino UNO and the seven-segment display. The seven segments (labeled a to g) and the decimal point (dp) are connected to the digital pins of the Arduino through resistors. 

Here is a typical pin configuration:

Seven-segment circuit connection

Once the circuit is built as shown above, upload the provided code into your Arduino UNO. (Code is located in the Collapsible group drop-down box below)

Seven-Segment Display Code

/*

   SevenSegment

*/

#define DP 5       // PIN5

#define seg_A 6    // PIN6

#define seg_B 7    // PIN7

#define seg_C 8    // PIN8

#define seg_D 9    // PIN9

#define seg_E 10   // PIN10

#define seg_F 12   // PIN11

#define seg_G 11   // PIN12

int COUNT = 1;     // counter

void setup()

{

  for (int i = 5; i < 13; i++)

  {

    pinMode(i, OUTPUT); // Set PINs 5-12 as outputs

  }

}

void loop()

{

  // digitalWrite(DP, HIGH);

  switch (COUNT)

  {

    case 1:

      one ();

      break;

    case 2:

      two ();

      break;

    case 3:

      three ();

      break;

    case 4:

      four ();

      break;

    case 5:

      five ();

      break;

    case 6:

      six ();

      break;

    case 7:

      seven ();

      break;

    case 8:

      eight ();

      break;

    case 9:

      nine ();

      break;

    case 10:

      zero ();

      break;  

    case 11:

      dot ();

      break;

  }

  if (COUNT < 12)

  {

    COUNT++;

    delay(1000);

  }

  if (COUNT == 12)

  {

    COUNT = 0;

    delay(1000);

  }

}


void zero ()

{

  digitalWrite(seg_A, HIGH);

  digitalWrite(seg_B, HIGH);

  digitalWrite(seg_C, HIGH);

  digitalWrite(seg_D, HIGH);

  digitalWrite(seg_E, HIGH);

  digitalWrite(seg_F, HIGH);

  digitalWrite(seg_G, LOW);

  digitalWrite(DP, LOW);


}

void one ()

{

  digitalWrite(seg_A, LOW);

  digitalWrite(seg_B, HIGH);

  digitalWrite(seg_C, HIGH);

  digitalWrite(seg_D, LOW);

  digitalWrite(seg_E, LOW);

  digitalWrite(seg_F, LOW);

  digitalWrite(seg_G, LOW);

  digitalWrite(DP, LOW);


}

void two ()

{

  digitalWrite(seg_A, HIGH);

  digitalWrite(seg_B, HIGH);

  digitalWrite(seg_C, LOW);

  digitalWrite(seg_D, HIGH);

  digitalWrite(seg_E, HIGH);

  digitalWrite(seg_F, LOW);

  digitalWrite(seg_G, HIGH);

  digitalWrite(DP, LOW);


}

void three ()

{

  digitalWrite(seg_A, HIGH);

  digitalWrite(seg_B, HIGH);

  digitalWrite(seg_C, HIGH);

  digitalWrite(seg_D, HIGH);

  digitalWrite(seg_E, LOW);

  digitalWrite(seg_F, LOW);

  digitalWrite(seg_G, HIGH);

  digitalWrite(DP, LOW);


}

void four ()

{

  digitalWrite(seg_A, LOW);

  digitalWrite(seg_B, HIGH);

  digitalWrite(seg_C, HIGH);

  digitalWrite(seg_D, LOW);

  digitalWrite(seg_E, LOW);

  digitalWrite(seg_F, HIGH);

  digitalWrite(seg_G, HIGH);

  digitalWrite(DP, LOW);

}

void five ()

{

  digitalWrite(seg_A, HIGH);

  digitalWrite(seg_B, LOW);

  digitalWrite(seg_C, HIGH);

  digitalWrite(seg_D, HIGH);

  digitalWrite(seg_E, LOW);

  digitalWrite(seg_F, HIGH);

  digitalWrite(seg_G, HIGH);

  digitalWrite(DP, LOW);

}

void six ()

{

  digitalWrite(seg_A, HIGH);

  digitalWrite(seg_B, LOW);

  digitalWrite(seg_C, HIGH);

  digitalWrite(seg_D, HIGH);

  digitalWrite(seg_E, HIGH);

  digitalWrite(seg_F, HIGH);

  digitalWrite(seg_G, HIGH);

  digitalWrite(DP, LOW);

}

void seven ()

{

  digitalWrite(seg_A, HIGH);

  digitalWrite(seg_B, HIGH);

  digitalWrite(seg_C, HIGH);

  digitalWrite(seg_D, LOW);

  digitalWrite(seg_E, LOW);

  digitalWrite(seg_F, LOW);

  digitalWrite(seg_G, LOW);

  digitalWrite(DP, LOW);

}

void eight ()

{

  digitalWrite(seg_A, HIGH);

  digitalWrite(seg_B, HIGH);

  digitalWrite(seg_C, HIGH);

  digitalWrite(seg_D, HIGH);

  digitalWrite(seg_E, HIGH);

  digitalWrite(seg_F, HIGH);

  digitalWrite(seg_G, HIGH);

  digitalWrite(DP, LOW);

}

void nine ()

{

  digitalWrite(seg_A, HIGH);

  digitalWrite(seg_B, HIGH);

  digitalWrite(seg_C, HIGH);

  digitalWrite(seg_D, HIGH);

  digitalWrite(seg_E, LOW);

  digitalWrite(seg_F, HIGH);

  digitalWrite(seg_G, HIGH);

  digitalWrite(DP, LOW);

}

void dot ()

{

  digitalWrite(seg_A, LOW);

  digitalWrite(seg_B, LOW);

  digitalWrite(seg_C, LOW);

  digitalWrite(seg_D, LOW);

  digitalWrite(seg_E, LOW);

  digitalWrite(seg_F, LOW);

  digitalWrite(seg_G, LOW);

  digitalWrite(DP, HIGH);

}

Steps:

Have fun....

If you have enjoyed the Arduino Intermediates tutorial, then check out the topic Arduino 103 on this website for more advanced Arduino projects. 


»»» END of  Arduino 102 »»»