Wednesday, 24 February 2021

Task#3___Control RGB LED by using three photo sensors

For this task, we use three Photo sensors to control RGB LED. And an RGB LED is a combination of three LEDs in just one package: red, green and blue. It generates different colors by adjusting the brightness of each of the three LEDs of the RGB LED.

        - Input: 3 pins for photo sensors (analog)
        - Output: 3 pins for three LED (digital by PWM)
  • Conditions 
            - Read the values coming through photosensors
            - Light on RGB LED following the signal values
  • Making 
Let's build your circuit board like allow.

Testing while we upload code to Arduino UNO first.

  • Coding
   Here is the program coding for a run into the microcontroller 

int LDR1_val;
int LDR2_val;
int LDR3_val;
const int LED_R = 9;
const int LED_G = 10;
const int LED_B = 11;
const int LDR1  = A0;
const int LDR2  = A1;
const int LDR3  = A2;

void setup() {
  Serial.begin(9600);

  pinMode(LED_R,OUTPUT);
  pinMode(LED_G,OUTPUT);
  pinMode(LED_B,OUTPUT);

}

void loop() {
  
  LDR1_val = analogRead(LDR1);
  delay(5);
  LDR2_val = analogRead(LDR2);
  delay(5);
  LDR3_val = analogRead(LDR3);
  delay(5);
  LDR1_val = map(LDR1_val,0,1023,0,255);
  LDR2_val = map(LDR2_val,0,1023,0,255);
  LDR3_val = map(LDR3_val,0,1023,0,255);
  Serial.print(LDR1_val);
  Serial.print("    ");
  Serial.print(LDR2_val);
  Serial.print("    ");
  Serial.println(LDR3_val);

  if(LDR1_val >=20){
    digitalWrite(LED_R,HIGH);
  }else{
    digitalWrite(LED_R,LOW);
  }
    if(LDR2_val >=30){
    digitalWrite(LED_G,HIGH);
  }else{
    digitalWrite(LED_G,LOW);
  }
    if(LDR3_val >=40){
    digitalWrite(LED_B,HIGH);
  }else{
    digitalWrite(LED_B,LOW);
  }

}






// prepared by Mr. Rothny
//Lead by Dr. Kwanghoon Kim


No comments:

Post a Comment