Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Mega Components

ELEGOO MEGA · LESSON 4

RGB LED

Fade a common-cathode RGB LED from red to green to blue and back.

Mix colors by varying three PWM outputs.

Parts List

  • ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
  • An 830-contact solderless breadboard joins inserted leads through hidden spring strips so circuits can be rebuilt without solder. Full-size rectangular board, center channel, numbered five-hole terminal groups and side power rails. Counted contact holes are not component units.
  • A through-hole RGB LED combines red, green and blue emitters for additive color mixing. Four-lead dome; common polarity is not established for every kit. Verify common polarity and channel order; similar pin counts do not guarantee the same map.
  • 220 Ω resistor provides a fixed opposition to current. LED current limiting is a common use when its unverified power and tolerance limits fit the circuit. Small axial body with one wire at each end. Find the labeled value compartment and measure an isolated loose part with a meter; the shared inventory image is a form reference.
  • Male-to-male jumper leads make removable point-to-point connections for low-current prototype signals. Pins at both ends; count individual leads rather than ribbon groups. Check intact insulation and snug contacts before use.

Assembly

  1. Disconnect USB and identify the LED’s common cathode and color leads.
  2. Place the four LED leads in separate breadboard strips and fit one 220 Ω resistor per color.
  3. Check the three PWM connections against the diagram before reconnecting USB.
Illustration © ELEGOO. All rights reserved.
Illustration © ELEGOO. All rights reserved. Open full-size ↗
Reviewed pin-to-pin connections
FromToPurpose / notes
Mega: D6 / REDRed resistor220Ω: input
Red resistor220Ω: outputRGB LED: R anode
Mega: D5 / GREENGreen resistor220Ω: input
Green resistor220Ω: outputRGB LED: G anode
Mega: D3 / BLUEBlue resistor220Ω: input
Blue resistor220Ω: outputRGB LED: B anode
RGB LED: common cathodeMega: GND
Open vector connection diagram ↗

Code

PlatformIO configuration
platformio.ini
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
main.cpp
#include <Arduino.h>

// uint8_t is a whole number from 0 to 255; these constants name the three RGB pins.
const uint8_t RED = 6;
const uint8_t GREEN = 5;
const uint8_t BLUE = 3;
// Change this delay to make the color transitions slower or faster.
const unsigned long FADE_DELAY_MS = 10;

// Run once: set each LED channel as an output, then start on red.
void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  // analogWrite() uses PWM on these Mega pins: 255 is fully on; 0 is off.
  analogWrite(RED, 255);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0);
}

// Keep cycling between red, green, and blue.
void loop() {
  // A for loop repeats with level increasing by one each time. This fades red into green.
  for (int level = 0; level < 255; ++level) {
    // As level rises, reduce red while increasing green.
    analogWrite(RED, 254 - level);
    analogWrite(GREEN, level + 1);
    delay(FADE_DELAY_MS);
  }
  // Now fade from green to blue.
  for (int level = 0; level < 255; ++level) {
    analogWrite(GREEN, 254 - level);
    analogWrite(BLUE, level + 1);
    delay(FADE_DELAY_MS);
  }
  // Finally fade from blue back to red.
  for (int level = 0; level < 255; ++level) {
    analogWrite(BLUE, 254 - level);
    analogWrite(RED, level + 1);
    delay(FADE_DELAY_MS);
  }
}

Expected behavior

The LED fades red→green→blue→red continuously; each transition takes about 2.55 s.

Experiments & Challenges

  • Change FADE_DELAY_MS to alter the transition speed.
  • Change the order of the three transitions.

Troubleshooting

  • Wrong colors: check the actual R/G/B leads; leg order is part-specific.