Arduino Project 4 of 100: RGB LED – Fade

Aquarius: 1  2  3  4  5  6  7  8  9  10  11  12  13
aquarius project
email me

In this project, I test an RGB LED. The RGB color model is an additive color model in which the red, green and blue primary colors of light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green, and blue. What does this mean? It means that rather than having discrete LEDs, this one kind of LED can emit any color as you need it. With a little programming, it can blink, fade in and out, and cycle through the color spectrum. In this demo, I fade between red, green, and blue.


PARTS

1 x Arduino UNO R3
3 x 220 Ohm Resistors
1 x RGB (Cathode) LED
5 x Jumper Wires
1 x Breadboard
5 Volt Power Source


MEDIA

VIDEOS


CONNECTIONS

Add 3 220R resistors to breadboard
Add RGB LED to breadboard
– RGB LED+ pin1 to 220R resistor
– RGB LED+ pin3 to 220R resistor
– RGB LED+ pin4 to 220R resistor
Jumper wire from RGB LED- pin2 to ground rail
Jumper wire from ground rail to ground on controller
Jumper wire from 220R resistor to pin11
Jumper wire from 220R resistor to pin10
Jumper wire from 220R resistor to pin9

see Wokwi, Board Simulator

 

OPERATIONS

Include Libraries and Define Constants:

  • The Arduino.h library is included for standard Arduino functions.
  • Constants are defined for the pin numbers connected to the red, green, and blue LEDs (RED_PIN, GREEN_PIN, BLUE_PIN), and a constant for the delay time used in fading the LEDs (FADE_DELAY).

Setup Function:

  • The setup() function is called once when the Arduino starts.
  • It sets the LED pins (11, 10, and 9) as outputs using pinMode().

Main Loop:

  • The loop() function runs continuously after the setup() function.
  • It sequentially fades each LED in and out. First, the red LED fades in and out, followed by the green LED, and finally the blue LED.
    Fading Functions:
  • fadeLedIn(byte pin) gradually increases the brightness of the specified LED from off (0) to full brightness (255).
  • Inside the function, a for loop increments the brightness value from 0 to 255.
    analogWrite(pin, brightness) sets the LED brightness.
  • delay(FADE_DELAY) pauses for a short time before the next increment.
  • fadeLedOut(byte pin) gradually decreases the brightness of the specified LED from full brightness (255) to off (0).
  • A for loop decrements the brightness value from 255 to 0.
  • Similar to fadeLedIn, analogWrite(pin, brightness) sets the LED brightness, and delay(FADE_DELAY) pauses before the next decrement.

 

Code.cpp

 

// MrNetTek 
// eddiejackson.net
// 8/5/2024
// free for public use 
// free to claim as your own

#include "Arduino.h"

const byte RED_PIN = 11;
const byte GREEN_PIN = 10;
const byte BLUE_PIN = 9;
const unsigned int FADE_DELAY = 5; // timing

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
  fadeLedIn(RED_PIN);
  fadeLedOut(RED_PIN);
  fadeLedIn(GREEN_PIN);
  fadeLedOut(GREEN_PIN);
  fadeLedIn(BLUE_PIN);
  fadeLedOut(BLUE_PIN);
}

void fadeLedIn(byte pin) {
  // Fade LED in from off to full brightness
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(pin, brightness);
    delay(FADE_DELAY);
  }
}

void fadeLedOut(byte pin) {
  // Fade LED out from full brightness to off
  for (int brightness = 255; brightness >= 0; brightness--) {
    analogWrite(pin, brightness);
    delay(FADE_DELAY);
  }
}


 
 
 

NOTES

 
Arduino UNO R3 Datasheet

Arduino UNO R3 Documentation


delay()

pinMode

analogWrite

 

Tags: Arduino, maker, builder, engineer, inventor, Project Aquarius