Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: $2000
Amount Raised: $808

Donations Received

Top Donor: C.C. $100

X

Common Cathode Red and Green LED Module Arduino Tutorial

Created on: 1 December 2017

Common cathode red and green LED module Arduino tutorial that shows how to use the common cathode red and green LED module from the 37 in 1 sensor kit for Arduino from Geekcreit or Elegoo and others.

This tutorial shows basic use of the two dual colour LED modules with Arduino, first without PWM and then with PWM (Pulse Width Modulation).

The 37 in 1 sensor kit module for Arduino contains two 3-pin LED modules that both have a dual colour LED. One module contains a 5mm LED and the other module contains a 3mm LED as shown in the image below.

Two Colour LED Modules from the 37 in 1 Sensor Kit for Arduino

Two Colour LED Modules from the 37 in 1 Sensor Kit for Arduino

These modules may be called any one of the following, depending on which kit they are from:

  • Common cathode RED & GREEN LED module
  • Two color common cathode LED module
  • TWO-COLOR
  • MINI TWO-COLOR
  • Two color 5mm
  • Two color 3mm

For the pinout and circuit diagram of the dual colour LED module, see Geekcreit common cathode 2 colour LED module pinout reference.

Common Cathode Dual LED Arduino Circuit

Below is an image of the circuit built on breadboard in this tutorial using an Arduino Uno.

Common Cathode 2 Colour LED Module Arduino Breadboard Circuit

Common Cathode 2 Colour LED Module Arduino Breadboard Circuit

See the circuit diagram below that shows how to connect the two colour LED module to an Arduino. The following parts are used:

  • Arduino Uno and USB cable (can also use an Arduino MEGA)
  • Electronic breadboard
  • Three hook-up or jumper wires
  • One 220 Ohm resistor
  • One 2k2 Ohm resistor
  • Common cathode LED module from the kit
Common Cathode 2 Colour LED Module Arduino Circuit

Common Cathode 2 Colour LED Module Arduino Circuit

As can be seen in the circuit diagram, the left pin of the LED module is connected to a GND pin of the Arduino. The middle pin of the LED module is connected to pin 6 of the Arduino through a 2k2 resistor. The right pin of the LED module connects to pin 5 of the Arduino through a 220 Ohm resistor.

Arduino pins 5 and 6 were chosen to control the LEDs because they are both PWM pins. PWM on these pins is available, but optional, so the LED module can be switched on and off, or PWM controlled.

Principle of Operation of the Two Colour Common Cathode LED Module

The two colour LED is basically two LEDs in one LED package. The cathode of each LED are connected together and are found on a single common pin of the LED package, hence it is a common cathode device.

LED Module Pins

Each anode pin from each LED has its own pin on the LED device package. Although the middle pin of the LED itself is the common cathode, this is routed to the left pin of the module board. The middle pin of the module is the anode for the red LED an the right pin of the module is the anode for the green LED. Refer to the common cathode LED module pinout for more details.

Powering the LED Module

With the common anode connected to GND or negative of the power supply, the green and red LEDs can be switched on by applying the positive of the power supply to each anode through a current limiting resistor. Each colour can be switched on individually, or both can be switched on at the same time.

Current Limiting Resistors

In the circuit for this tutorial, a 2k2 resistor is used on the red LED anode and a 220 Ohm resistor is used on the green LED anode. The resistor serves as a current limiting resistor so that the LED does not draw too much current which could destroy it or circuitry in the microcontroller that connects to the Arduino pin.

Two different value resistors are used with the two different LED colours because red and green LEDs have different forward voltages and characteristics. Using two different value resistors gets the different colour LEDs approximately the same brightness.

Common Cathode LED Arduino Sketches

Three Arduino sketches can be found below that control the two colour LED module from the above circuit. The first sketch switches the two LED colours on alternately – first red then green with a delay in between. In the second sketch first red is switched on, then green, then both red and green.

PWM is used to control the brightness of the red then green colours of the LED in the third sketch by fading them in and out. Pins that control the two colours can be changed at the top of the sketch, just make sure that the wiring is changed accordingly. Also make sure that the pins have PWM capabilities when using the PWM sketch if changing the pin numbers.

Alternating Red and Green

Red is switched on for 1 second (1000ms) and green off of the LED module in the sketch below. Green is then switched on and red off for 1 second. As this program is in the main loop, it repeats continually.

// common cathode red and green LED
// switches red on, then green on
#define RED_LED   6
#define GRN_LED   5

void setup() {
  pinMode(RED_LED, OUTPUT);
  pinMode(GRN_LED, OUTPUT);
}

void loop() {
  // red on, green off
  digitalWrite(RED_LED, HIGH);
  digitalWrite(GRN_LED, LOW);
  delay(1000);
  // red off, green on
  digitalWrite(RED_LED, LOW);
  digitalWrite(GRN_LED, HIGH);
  delay(1000);
}

Red, Green and Both

This sketch works the same as the previous sketch, except that it adds an extra step by leaving both red and green on for 1 second.

// common cathode red and green LED
// switches red on, then green on, then both red and green on
#define RED_LED   6
#define GRN_LED   5

void setup() {
  pinMode(RED_LED, OUTPUT);
  pinMode(GRN_LED, OUTPUT);
}

void loop() {
  // red on
  digitalWrite(RED_LED, HIGH);
  digitalWrite(GRN_LED, LOW);
  delay(1000);
  // green on
  digitalWrite(RED_LED, LOW);
  digitalWrite(GRN_LED, HIGH);
  delay(1000);
  // both on
  digitalWrite(RED_LED, HIGH);
  delay(1000);
}

Common Cathode LED PWM Control

PWM is used in this sketch to fade red of the dual LED module from dim to bright, then bright to dim. Green is then faded from dim to bright and bright to dim. This happens continually as the PWM code is in the main loop.

// common cathode red and green LED
// uses PWM to control red and green brightness
#define RED_LED   6
#define GRN_LED   5
#define STEP_TIME 10
int pwm_val = 0;

void setup() {
  pinMode(RED_LED, OUTPUT);
  pinMode(GRN_LED, OUTPUT);
}

void loop() {
  // red dim to bright
  for (pwm_val = 0; pwm_val <= 255; pwm_val++) {
    analogWrite(RED_LED, pwm_val);
    delay(STEP_TIME);
  }
  // red bright to dim
  for (pwm_val = 255; pwm_val >= 0; pwm_val--) {
    analogWrite(RED_LED, pwm_val);
    delay(STEP_TIME);
  }
  // green dim to bright
  for (pwm_val = 0; pwm_val <= 255; pwm_val++) {
    analogWrite(GRN_LED, pwm_val);
    delay(STEP_TIME);
  }
  // green bright to dim
  for (pwm_val = 255; pwm_val >= 0; pwm_val--) {
    analogWrite(GRN_LED, pwm_val);
    delay(STEP_TIME);
  }
}

Amazon.com

Amazon.co.uk

← Go back to Active Buzzer Module Tutorial









Donate to Starting Electronics



Payoneer