0

members

Easy Arduino Starter Kit: Project 4 COLOR MIXING LAMP

of Arduino in UNO

Arduino Starter Kit: Project 4

Project name: COLOR MIXING LAMP

Discover: analog output, mapping values

Attachments: libraries and sketch

In this project, you needed these parts :

1.Aruduino Uno R3 (you can also use the other version of Arduino)

2.Jumper cables

3. Resistor 6 pcs (3 pcs 220 Ohm and 3 pcs 10 KOhm)

4. Breadboard half size 

5. RGB LED (common cathode used) 1 pc

6. Photoresistors (photocells) 3 pc

7. Arduino IDE ( you can download it from here  )

8. GELS (red, green, blue clors)

GENERAL

Blinking LEDs can be fun, but what about fading them, or mixing colors?
You might expect that it’s just a mater of providing less voltage to an LED to get it to fade. The Arduino can’t vary the output voltage on its pins, it can only output 5V. Hence you’ll need to use a technique called Pulse Width Modulation (PWM) to fade LEDs. PWM rapidly turns the output pin high and low over a fixed period of time. The change happens faster than the human eye can see. It’s similar to the way movies work, quickly flashing a number of still images to create the illusion of motion. When you’re rapidly turning the pin HIGH and LOW, it’s as if you were changing the voltage. The percentage of time a pin is HIGH in a period is called duty cycle. When the pin is HIGH for half of the period and LOW for the other half, the duty cycle is 50%. A lower duty cycle gives you a dimmer LED than a higher duty cycle. The Arduino Uno has six pins set aside for PWM (digital pins 3, 5, 6, 9, 10, and 11), they can be identifed by the ~ next to their number on the board.
What about fading LEDs, or mixing their colors? You might expect that it’s just a mater of providing less voltage to an LED to get it to fade. The Arduino can not vary the output voltage on its pins, it can only output 5V. You will need to use a technique called Pulse Width Modulation (PWM) to fade LEDs. PWM rapidly turns the output pin high and low over a fixed period of time. The change happens faster than the human eye can see. It’s similar to the way movies work, quickly flashing a number of still images to create the illusion of motion. When you’re rapidly turning the pin HIGH and LOW, it’s as if you were changing the voltage. The percentage of time a pin is HIGH in a period is called duty cycle. When the pin is HIGH for half of the period and LOW for the other half, the duty cycle is 50%. A lower duty cycle gives you a dimmer LED than a higher duty cycle. The Arduino Uno has six pins set aside for PWM (digital pins 3, 5, 6, 9, 10, and 11), they can be identifed by the ~ next to their number on the board.

For inputs in this project, you will use photoresistors (sensors that change their resistance depending on the amount of light that hits them, also known as photocells or light-dependent resistors). If you connect one end of the resistor to your Arduino, you can measure the change in resistance by checking the voltage on the pin.

THE CIRCUIT

Wire up your breadboard so you have power and ground on both sides. Place the three photoresistors on the breadboard so they cross the center divide from one side to the other. Attach one end of each photoresistor to power. Attach a 10-kilohm resistor to ground on the other side. This resistor is in series with the photoresistor, and together they form a voltage divider. The voltage at the point where they meet is proportional to the ratio of their resistances, according to Ohm’s Law. As the resistance of the photoresistor changes when light hits it, the voltage at this junction changes as well. On the same side as the resistor, connect the photoresistors to Analog In pins 0, 1, and 2 with hookup wire. Take the three colored gels and place one over each of the photoresistors. Place the red gel over the photoresistor connected to A0, the green over the one connected to A1, and the blue over the one connected to A2. Each of these filters lets only light of a specific wavelength through to the sensor it’s covering. The red filter passes only red light, the green filter passes only green light, and the blue filter passes only blue light. This allows you to detect the relative color levels in the light that hits your sensors. The LED with 4 legs is a common cathode RGB LED.

The LED has separate red, green, and blue elements inside, and one common ground (the cathode). By creating a voltage difference between the cathode and the voltage coming out of the Arduino’s PWM pins (which are connected to the anodes through 220-ohm resistors), you’ll cause the LED to fade between its three colors. Make note of what the longest pin is on the LED, place it in your breadboard, and connect that pin to ground. Connect the other three pins to digital pins 9, 10 and 11 in series with 220-ohm resistors. Be sure to connect each LED lead to the correct PWM pin.

THE CODE

Set up constants for the pins you are using for input and output, so you can keep track of which sensor pairs with which color on the LED. Use const int for the datatype. Add variables for the incoming sensor values and for the output values you will be using to fade the LED. You can use the int datatype for all the variables. In the setup(), begin serial communication at 9600 bps. You will use this to see the values of the sensors in the serial monitor. Additionally, you will be able to see the mapped values you’ll use to fade the LED. Also, define the LED pins as outputs with pinMode(). In the loop() read the sensor values on A0, A1, and A2 with analogRead() and store the value in the appropriate variables. Put a small delay() between each analogRead() as the ADC takes a millisecond to do its work. Print out the sensor values on one line. The “\t” is the equivalent of pressing the “tab” key on the keyboard. The function to change the LED’s brightness via PWM is called analogWrite(). It needs two arguments: the pin to write to, and a value between 0-255. This second number represents the duty cycle the Arduino will output on the specified pin. A value of 255 will set the pin HIGH all the time, making the attached LED as bright as it can be. A value of 127 will set the pin HIGH half the time of the period, making the LED dimmer. 0 would set the pin LOW all the time, turning the LED off. To convert the sensor reading from a value between 0-1023 to a value between 0-255 for analogWrite(), divide the sensor reading by 4. Print out the new mapped values on their own line.

THE START

Once you have your Arduino programmed and wired up, open the serial monitor. The LED will probably be an off-white color, depending on the predominant color of the light in your room. Look at the values coming from the sensors in the serial monitor, if you’re in an environment with stable lighting, the number should probably be fairly consistent. Turn off the light in the room you’re in and see what happens to the values of the sensors. With a flashlight, illuminate each of the sensors individually and notice how the values change in the serial monitor, and notice how the LED’s color changes. When the photoresistors are covered with a gel, they only react to light of a certain wavelength. This will give you the opportunity to change each of the colors independently.

You may notice that the photoresistor’s output doesn’t range all the way from 0 to 1023. You’ll probably notice that the LED’s fading is not linear. When the LED is about at half brightness, it appears to stop getting much brighter. This is because our eyes don’t perceive brightness linearly. The brightness of the light depends not only on the level that you analogWrite() but also on the distance of the light from the diffuser, the distance of your eye from the light, and the brightness of the light relative to other light in the room.

THE SUMMARY
You now have control over the RGB LED. It's easy to bright or dim LEDs now. analogWrite() is the function that allows you to PWM components atached to pins 3, 5, 6, 9, 10, or 11, varying the duty cycle.
There are a number of different ways you can diffuse the light. A ping pong ball with a hole cut out for the LED to slide into makes for a nice diffuser or covering the light in translucent glue, or sanding the surface of the light.

See one of movies on Youtube about it - link here



Other projects of Arduino

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 14-08-2017
Viewed: 1266 times