Josh StidhamBill RuiJonathan Tenney
Published © GPL3+

RGB LED Selector

Not sure what colored light best sets the mood? Use the RGB LED to test every color of the visible light spectrum!

BeginnerFull instructions provided15 minutes270
RGB LED Selector

Things used in this project

Hardware components

EK-TM4C123GXL TM4C Tiva LaunchPad
Texas Instruments EK-TM4C123GXL TM4C Tiva LaunchPad
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×4
Jumper wires (generic)
Jumper wires (generic)
×4
Resistor 220 ohm
Resistor 220 ohm
×1
SparkFun Breadboard Power Supply Stick 5V/3.3V
SparkFun Breadboard Power Supply Stick 5V/3.3V
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Breadboard (generic)
Breadboard (generic)
×1
Tilt Switch, SPST
Tilt Switch, SPST
×1

Software apps and online services

Energia
Texas Instruments Energia

Story

Read more

Schematics

Schematics

Code

Code

C/C++
#include <LiquidCrystal.h>

// define input pins
#define REDI A9  // PE_4
#define GREENI A8  // PE_5
#define BLUEI A10  // PB_4
// define output pins
#define REDO 30
#define GREENO 39
#define BLUEO 40

int sensorPins[] = {REDI, GREENI, BLUEI};  // input pins for the potentiometers
int ledPins[] = {REDO, GREENO, BLUEO};  // LED pins
int brightness[3];  // brightness of leds

//define LCD pins
const int rs = 10, en = 9, d4 = 8, d5 = 13, d6 = 12, d7 = 11;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


void setup() 
{
  //for debugging
  Serial.begin(9600);
  lcd.begin(16, 2);
  //welcome message
  lcd.print("Welcome to RGB!");
  //test LEDs
  analogWrite(ledPins[0], 255);
  delay(500);
  analogWrite(ledPins[0], 0);
  analogWrite(ledPins[1], 255);
  delay(500);
  analogWrite(ledPins[1], 0);
  analogWrite(ledPins[2], 255);
  delay(500);
  analogWrite(ledPins[2], 0);
  delay(500);
}

void loop() 
{
  
   //read the value from the sensor:
  for(int i = 0; i < 3; i++)
  {
    int value = (int) map(analogRead(sensorPins[i]), 0, 4095, 0, 255);  // scale the input value from 0-4095 to 0-255

    //set brightness to 0 when value is too small to completely turn off LED
    if(value < 1)
    {
      brightness[i] = 0;
    }
    else
    {
      brightness[i] = value;
    }
    delay(10);
  }
  delay(30);
  lcd.setCursor(0, 0);
  String redString = String(brightness[0], HEX);
  String greenString = String(brightness[1], HEX);
  String blueString = String(brightness[2], HEX);
  //ensure all have length 2
  if(redString.length() == 1)
  {
    redString = "0" + redString;
  }
    if(greenString.length() == 1)
  {
    greenString = "0" + greenString;
  }
    if(blueString.length() == 1)
  {
    blueString = "0" + blueString;
  }
  //print hex code to lcd
  lcd.print("HEX CODE: "  + redString + greenString + blueString);
  
  
   //write values to LEDs
  for(int i = 0; i < 3; i++)
  {
    analogWrite(ledPins[i], brightness[i]);  // turn the led on at given brightness
  }
  
}

Credits

Josh Stidham
1 project • 0 followers
Contact
Bill Rui
1 project • 0 followers
Contact
Jonathan Tenney
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.