What if we can generate different colors using a single RGB led and make our room’s corner more attractive? In this tutorial, we will design a color mixing lamp using RGB (Red, Blue and Green) LED, LDR (Light Dependent Resistor), Surilli GSM and Arduino IDE. This color mixing lamp changes color when there is change in light in the room. So this lamp will automatically change its color according to the light conditions in the room.
Every color is the combination of red, green and blue. So we can generate any color by using red, green and blue colors. Here we will vary PWM, i.e. intensity of light on LDRs. That will further change the intensity of red, green and blue color in RGB LED, and as a result different colors will be produced.
What Is a Light Dependent Resistor (LDR)?LDRs are made from semiconductor materials to enable them to have their light-sensitive properties. LDRs or "Photo Resistors" works on the principle of “Photo Conductivity”.
Basic Principle of LDRs:Whenever light falls on the surface of the LDR, the conductance of the element increases. In other words, the resistance of the LDR falls when the light falls on the surface of the LDR. This property of the decrease in resistance for the LDR is achieved because it is a property of semiconductor material used on the surface.
In this tutorial, three LDR sensors are used to control the brightness of individual red, greed and blue LED inside RGB LED.
RGB LED means red, blue and green LEDs. RGB LED products combine these three colors to produce over 16 million hues of light. Note that not all colors are possible. Some colors are “outside” the triangle formed by the RGB LEDs. Also, pigment colors such as brown or pink are difficult, or impossible, to achieve.
There are two types of RGB LEDs, one is common cathode type (common negative) and other is common anode type (common positive) type.
In CC (Common Cathode or Common Negative), there will be three positive terminals each terminal representing a color and one negative terminal representing all three colors.
In Common Anode type, if we want RED LED to be ON in, we need to ground the RED LED pin and power the common positive. The same goes for all the LEDs.
In this tutorial, we are going to use CA (Common Anode or Common Positive) type.
- Surilli GSM
- 1 x Breadboard
- 3 x 221 ohm Resistors
- 3 x 1k ohm Resistors
- Connecting wires
- 3 x LDRs
- 1 x RGB LED (Diffused Common Anode Type)
RED PIN (RGB LED) connected to PIN 0 of 1k ohm Resistor.
PIN 1 of 1k ohm Resistor ---> PIN 11 (SURILLI GSM).
COMMON ANODE PIN (RGB LED) ---> USB PIN (SURILLI GSM).
BLUE PIN (RGB LED) connected to PIN 0 of 1k ohm Resistor.
PIN 1 of 1k ohm Resistor ---> PIN 10 (SURILLI GSM).
GREEN PIN (RGB LED) connected to PIN 0 of 1k ohm Resistor.
PIN 1 of 1k ohm Resistor ---> PIN 9 (SURILLI GSM).
PIN 0 (3 LDR'S) ---> USB PIN (SURILLI GSM).
PIN 1 (3 LDR'S) connected with 221 ohm Resistors.
FIRST PIN of each resistor connected to GND PIN (SURILLI GSM).
SECOND PIN of each 221 ohms resistor in series with all the PIN 1 of all the 3 LDR's connected to PIN A0, A1 and A2 respectively (SURILLI GSM).
Set Up Arduino IDE for Surilli:Make sure you have selected the right port, board and processor for the Surilli as shown in the picture below and it is programmable (compile and upload “Blink” from File>Examples>Digital>Blink onto your Surilli to check if everything is working fine).
The Circuitry:The circuitry is very simple. It's mostly the programming. Follow the figure below to set up your hardware.
Now you have completed setting up your hardware and Arduino IDE. Copy and paste the Arduino sketch given below into your Arduino IDE. Make sure that you have the right board and COM port selected.
Arduino Code:const byte red_sensor_pin = A0;
const byte green_sensor_pin = A1;
const byte blue_sensor_pin = A2;
const byte green_led_pin = 9;
const byte blue_led_pin = 10;
const byte red_led_pin = 11;
unsigned int red_led_value = 0;
unsigned int blue_led_value = 0;
unsigned int green_led_value = 0;
unsigned int red_sensor_value = 0;
unsigned int blue_sensor_value = 0;
unsigned int green_sensor_value = 0;
void setup() {
pinMode(red_led_pin,OUTPUT);
pinMode(blue_led_pin,OUTPUT);
pinMode(green_led_pin,OUTPUT);
Serial.begin(9600);
}
void loop() {
red_sensor_value = analogRead(red_sensor_pin);
delay(50);
blue_sensor_value = analogRead(blue_sensor_pin);
delay(50);
green_sensor_value = analogRead(green_sensor_pin);
// print those values onto the serial monitor
Serial.println("Raw Sensor Values:");
Serial.print("\t Red: ");
Serial.print(red_sensor_value);
Serial.print("\t Blue: ");
Serial.print(blue_sensor_value);
Serial.print("\t Green: ");
Serial.println(green_sensor_value);
// convert from 0-1023 to 0-255
red_led_value = red_sensor_value / 4; // define Red LED
blue_led_value = blue_sensor_value / 4; // define Blue LED
green_led_value = green_sensor_value / 4; // define Green LED
// print mapped values to serial monitor
Serial.println("Mapped Sensor Values:");
Serial.print("\t Red: ");
Serial.print(red_led_value);
Serial.print("\t Blue: ");
Serial.print(blue_led_value);
Serial.print("\t Green: ");
Serial.println(green_led_value);
// use analogWrite() to set output for RGB LED
analogWrite(red_led_pin,red_led_value); // indicate red LED
analogWrite(blue_led_pin,blue_led_value); // indicate blue LED
analogWrite(green_led_pin,green_led_value); // indicate green
}
As we are using three LDR’s so, when light incident on these sensors, it’s resistance changes as a result voltages also changes at analog pins of Arduino which is acting as a input pins for sensors.
When intensity of light changes on these sensors, it’s respective led in RGB will glow with amount of resistance changing and we have different color mixing in RGB led using PWM.
DEMONSTRATION (WORKING):
To demonstrate this, if for example we cover the LDR connected with RED PIN of RGB LED and then at the same time, cover that LDR connected with BLUE PIN of RGB LED then we can see that both the RED and the BLUE LED will flash at the same time and it will keep on flashing until we donot remove our hand over both the LDR's. The same process can be repeated with GREEN and BLUE or vice versa.
Play with the program to see how it reacts to different values and logic.
If you make something fun and interesting, do share it with our community.
That’s all for now. If you have any queries, visit surilli.io or contact our support. Stay connected with the Surilli family for more amazing stuff. :-)
Comments