Interfacing RGB LED with Arduino using three potentiometer / Controlling RGB LED colors using three potentiometers
Note: Here Common Anode RGB LED is used. Please use only PWM pins of Arduino (pin 3,5,6,9,10,11) to connect LED.
Working Video:
Arduino Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); //LCD address 0x27
int redPin= 3;
int greenPin = 5;
int bluePin = 6;
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight(); // lcd backlight ON
lcd.setCursor(0,0); // 0 row, 0 column
//Defining the pins as OUTPUT/ INPUT
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
}
void loop()
{
int r = analogRead(A0);
r=map(r,0,1018,255,0);
int g = analogRead(A1);
g=map(g,0,1018,255,0);
int b = analogRead(A2);
b=map(b,0,1018,255,0);
lcd.clear();
lcd.print("R="); lcd.print(r); lcd.print(",");
lcd.print("G="); lcd.print(g); lcd.print(",");
lcd.print("B="); lcd.print(b);
delay(100);
setColor(r, g, b);
}
void setColor(int redValue, int greenValue, int blueValue)
{
analogWrite(redPin, 255-redValue);
analogWrite(greenPin, 255-greenValue);
analogWrite(bluePin, 255-blueValue);
}
No comments:
Post a Comment