Welcome back to TinkerLabs!
In this progect totorial, we shall see how to make a light controlled RGB led.
The main components are the RGB led and The Light Sensor.
What is the purpose of the Project?This Project is a light sensitive RGB led. Basically, if the light sensor detects no light it turns on the Led. It can be used as a night lamp that automatically turns on at night.
First, what is a RGB Led?RGB LED is the short form of Red Green Blue.
This led combines these primary colors to make over 16 million hues
the RGB led I am using has 4 pins:
1. GND
2. R(red)
3. G(green)
4. B(blue)
How does a light sensor work?Light sensor, or a photoresistor is a light-sensitive sensor that can indicate the presence or absense of light. It is also known as a LDR (Light-dependent resistor)
The Photoresistor has 3 Pins:
1.VCC (5v)
2.GND(ground)
3. OUT(output)
THE CODE>>First, we need to declare some variables.
const int R = A0;
const int G = A1;
const int B = A2;
int RValue;#ValueofcolorRED
int GValue;#ValueofcolorGREEN
int BValue;#ValueofcolorBLUE
const int sensorPin = 2;#Declaringthatthephotoresistorisconnectedtodigitalpin2
int readsensor;#Variabletoreadthephotoresistor
Next, We declare R, G and B to be a be used as OUTPUT and the sensor as INPUT.
void setup() {
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(sensorPin, INPUT);
}
Now we need to make a command called "color", to change the color of the LED.
void color(int RValue, int GValue, int BValue){
analogWrite(R, RValue);
analogWrite(G, GValue);
analogWrite(B, BValue);
}
The void loop contains an "If, Else" statement to check if the sensor detects light. if it does not detect light, then it will turn on the LED and change it's color from red to green to blue with a delay of 100 milliseconds till it detects light again. When it doesn't detect light, it turns off the LED
void loop() {
readsensor = digitalRead(sensorPin);
if(readsensor == HIGH){
color(255,0,0);
delay(100);
color(0, 0, 255);
delay(100);
color(0, 255, 0);
delay(100);
}
else{
color(0,0,0);
}
}




_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)





Comments