Arduino Starter Series - Colour Mixing Lamp
This is a project from my Arduino starter series.
This project comes from the Arduino Starter Kit Projects Book. The project uses photo-resistors to control an RGB LED. This is a great project for beginners. Enjoy!
Project Requirements
- Arduino Uno/Duemilanove/Diecimila
- USB cable to program Arduino
- Breadboard
- RGB LEDS (Looks like a white LED)
- Three Photoresistors
- Three 220 Ohm Resistors
- Three 10k Resistors
- Wires to power the components on the breadboard
The Breadboard Setup
The Code
As usual I try comment as much of the code as possible. Also be aware that I change the code for the project from what is defined in the Arduino Projects Book. It's not that I don't think the code is good or bad, I just find that there are more interesting ways to tackle these projects and providing an alternative method to approaching this project always leads to better understanding.
// The pins that the RGB LED connects to.
// I tested the RGB LED light first by connecting the different legs to power first.
// This gave me an indication of which leg was for which colour
const int greenLEDPin = 4;
const int redLEDPin = 5;
const int blueLEDPin = 6;
// The photoresisters get connected to analog pins
const int redSensorPin = A0;
const int greenSensorPin = A1;
const int blueSensorPin = A2;
// These values will be generated by the photoresistors for each of the three colors
int redSensorValue = 0;
int greenSensorValue = 0;
int blueSensorValue = 0;
// These will be the values that are used to set the LED colour.
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
void setup() {
// Fire up the Serial monitoring so we can debug the code
Serial.begin(9600);
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}
void loop() {
// The first thing to do here is to read the sensor data using the analogRead function
// The delay gives the analog to digital conversion (ADC) time to process
redSensorValue = analogRead(redSensorPin);
delay(5);
greenSensorValue = analogRead(greenSensorPin);
delay(5);
blueSensorValue = analogRead(blueSensorPin);
// We need to convert the 10-bit data provided by ADC into 8-bit.
// This is easy to do by simply dividing the value by 4
redValue = redSensorValue / 4;
greenValue = greenSensorValue / 4;
blueValue = blueSensorValue / 4;
// Now that we have the correct values we can output them to the Serial monitor
// and see how the photoresisters work when you interact with them.
Serial.print("Mapped sensor Values \t red: ");
Serial.print(redValue);
Serial.print("\t green: ");
Serial.print(greenValue);
Serial.print("\t Blue: ");
Serial.println(blueValue);
// Finally we need to update the pins on the RGB LED to control the LED colour
analogWrite(redLEDPin, redValue);
analogWrite(greenLEDPin, greenValue);
analogWrite(blueLEDPin, blueValue);
}
I have uploaded this project onto my Github page