Project 04

I'm having some problems with the LED. For some reason, it's really hard to make it glow green. I know that the LED is functioning properly, I've made it glow only green, but as soon as I mix colours I get this problem. My r, g and b values are very similar to each other when equally exposed to a light source. They also correspond to being covered up in the same way.
My friend has the same problem as me. Is making the LED glow green some inherent problem this type of LED has or have I made some mistake?

This is my scaled down code, without the printing and stuff.

int r, g, b;
void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}
void loop() {
  r = analogRead(A0);
  g = analogRead(A1);
  b = analogRead(A2);
  analogWrite(11, (r/4));
  analogWrite(9, (g/4));
  analogWrite(10, (b/4));
 }

I have the same issues.

I have the same, I suspect it's either a weak 'green' component in the LED or our eyes are just much more responsive to blue and red, so they drown out the green. In the end I ended up dividing red and blue by 10 and green by 3 to make the different colours visible

nass:
I have the same, I suspect it's either a weak 'green' component in the LED or our eyes are just much more responsive to blue and red, so they drown out the green. In the end I ended up dividing red and blue by 10 and green by 3 to make the different colours visible

Sorry about my noob question but, how do you know for how much you have to "divide" the colors, or you just tryed different values till you get the led glow white?

Tzyn:

nass:
I have the same, I suspect it's either a weak 'green' component in the LED or our eyes are just much more responsive to blue and red, so they drown out the green. In the end I ended up dividing red and blue by 10 and green by 3 to make the different colours visible

Sorry about my noob question but, how do you know for how much you have to "divide" the colors, or you just tryed different values till you get the led glow white?

Open the serial monitor and look at the values. In my case, the r-g-b values are 131, 105 & 86, which should imply a reddish-green color, but it's more purple (reddish-blue). Getting white is very difficult, as the green element just doesn't transmit well (due either to our lack of sensitivity to green or to a weak green LED element).

I manually set the values to see what different ratios gave and the only way I could get a good green led was to set the r-g-b to 3-255-3. Even setting the red and blue to 5 made the light noticeably non-green.

To get a white led (ok, mostly white when looking from the side), I manually set the values to 20-255-20. Once the red and blue values hit 50, the color was noticeably blue-purple.

Not sure what project you're doing, not sure what LED you're using. However, most RGB LEDs have different forward voltage values, so you can't use the same size resistors on all three channels. And if you are using a single resistor on the cathode, you're doing it wrong.

gustavjp:

void loop() {

r = analogRead(A0);
  g = analogRead(A1);
  b = analogRead(A2);
  analogWrite(11, (r/4));
  analogWrite(9, (g/4));
  analogWrite(10, (b/4));
}

I really dont get why we have to divide here by 4 :~

BTW thanks for your help dean_1230 and KirAsh4!

Tzyn, sorry for not replying sooner but yes I just did it by eye. I measured with a multimeter and there wasn't anything wrong with the circuits so I just figured the LEDs had different brightnesses, so adjusted accordingly.

Tzyn:

nass:
I have the same, I suspect it's either a weak 'green' component in the LED or our eyes are just much more responsive to blue and red, so they drown out the green. In the end I ended up dividing red and blue by 10 and green by 3 to make the different colours visible

Sorry about my noob question but, how do you know for how much you have to "divide" the colors, or you just tryed different values till you get the led glow white?

Thank you Nass! x)

KirAsh4:
Not sure what project you're doing, not sure what LED you're using. However, most RGB LEDs have different forward voltage values, so you can't use the same size resistors on all three channels. And if you are using a single resistor on the cathode, you're doing it wrong.

Here's the spec sheet for the RGB LED:

Forward voltages are 1.95/3.3/3.3 respectively.

As for the number and size of the resistors, going from memory (don't have the book in front of me at the moment) I think the
resistor strengths are all the same. Maybe someone that has the book handy can check.

The resistor strengths are all the same, ill check the value as soon as i get home and post here!

dean_1230:
Forward voltages are 1.95/3.3/3.3 respectively.

As for the number and size of the resistors, going from memory (don't have the book in front of me at the moment) I think the
resistor strengths are all the same. Maybe someone that has the book handy can check.

So therein lies (part of) the issue. With different voltages, you really need different resistor sizes as well. Assume you're supplying 5V to them, and assuming you want to drive the LEDs at 20mA, you should be using:

(5V - 1.95)/20mA = 152.5 Ohm, round it up to the nearest size and you get 180 Ohm, which when recalculated means the LED is being driven at 16.9mA
(5V - 3.3)/20mA = 85mA, round it up to the nearest size and you get 100 Ohm, recalculated gets you 17mA on the LED

You can use exact sizes, if you can find them, however going to the nearest common size is usually cheaper and the human eye will have a hard time noticing the difference anyway.

Hello, i cant get my LED to change color at all, i've already checked my circuit/code twice and cant find any error.
when i wave my hand over the photoresistors i get different values in the serial monitor but the led never changes color... any help?

thanks

Hi guys,

Try this code if you have completed project 4 and want to try something 'extra'. I've added a 10 sec calibration and set constraint for output between 0 to 255 to prevent flickering. The effect is fantastic. You can also set brightness in the code, so that it won't blind you in dark environment.

For those of you who experienced problems, you might want to check your resistors. I used 10M Ohm instead of 10k Ohm in the beginning. :astonished:
I was stumped for 2 days because of wrong resistors because I had no multimeter that time.

/*
  Arduino Starter Kit example
 Project 4  - Color Mixing Lamp (Extra added)
 
 This sketch is written to accompany Project 3 in the
 Arduino Starter Kit
 
 Parts required:
 1 RGB LED 
 three 10 kilohm resistors
 3 220 ohm resistors
 3 photoresistors
 red green and blue colored gels
 
 Created 13 September 2012
 Modified 14 November 2012
 by Scott Fitzgerald
 Thanks to Federico Vanzati for improvements
 
 Extra features added by Lam Kwok Leong, 8 July 2013

 http://arduino.cc/starterKit
 
 This example code is part of the public domain 
 */

const int Brightness = 20;  // set overall brightness, use lower value in darkroom to protect your eyes (0-255) 

const int greenLEDPin = 9;    // LED connected to digital pin 9
const int redLEDPin = 10;     // LED connected to digital pin 10
const int blueLEDPin = 11;    // LED connected to digital pin 11

const int redSensorPin = A0;  // pin with the photoresistor with the red gel 
const int greenSensorPin = A1;   // pin with the photoresistor with the green gel 
const int blueSensorPin = A2;   // pin with the photoresistor with the blue gel 

int redSensorValue = 0; // variable to hold the value from the red sensor 
int greenSensorValue = 0; // variable to hold the value from the green sensor 
int blueSensorValue = 0; // variable to hold the value from the blue sensor 

int redSensorHigh = 0; //High and low value for mapping. Notice that 'High' value were set low at 0, so that during calibration, its value would be be pushed up. See project 6.
int greenSensorHigh = 0;
int blueSensorHigh = 0;
int redSensorLow = 1023; // the opposite is true for 'Low' value
int greenSensorLow = 1023;
int blueSensorLow = 1023;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 

  // set the digital pins as outputs
  pinMode(greenLEDPin,OUTPUT);
  pinMode(redLEDPin,OUTPUT);
  pinMode(blueLEDPin,OUTPUT);
  
  //switching on your lamp before calibration, as it would affect calibration too, especially in dark environment
  analogWrite(redLEDPin, Brightness); 
  analogWrite(greenLEDPin, Brightness);
  analogWrite(blueLEDPin, Brightness);
  
  //actual calibration which takes 10 seconds. During calibration, LED would shine at constant brightness. After that, it'll start to respond.
  while (millis() < 10000) {
  redSensorValue = analogRead(redSensorPin);  //read pin
  if (redSensorValue > redSensorHigh) {  //push 'High' value up based on sensor value. At the end of calibration, 'High' value would take the highest 'SensorValue' during the 10 seconds.
  redSensorHigh = redSensorValue;
  }
  if (redSensorValue < redSensorLow) { //opposite is true for 'Low' value
  redSensorLow = redSensorValue;
  }
  delay(5);
  
  greenSensorValue = analogRead(greenSensorPin);  
  if (greenSensorValue > greenSensorHigh) {
  greenSensorHigh = greenSensorValue;
  }
  if (greenSensorValue < greenSensorLow) {
  greenSensorLow = greenSensorValue;
  }
  delay(5);
  blueSensorValue = analogRead(blueSensorPin);  
  if (blueSensorValue > blueSensorHigh) {
  blueSensorHigh = blueSensorValue;
  }
  if (blueSensorValue < blueSensorLow) {
  blueSensorLow = blueSensorValue;
  }
  delay(5);
  }
}

void loop() {
  // Read the sensors first:
  
  // read the value from the red-filtered photoresistor:
  redSensorValue = analogRead(redSensorPin);
  
  // give the ADC a moment to settle
  delay(5);
  // read the value from the green-filtered photoresistor:
  greenSensorValue = analogRead(greenSensorPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the blue-filtered photoresistor:
  blueSensorValue = analogRead(blueSensorPin);  

  // print out the values to the serial monitor  
  Serial.print("raw sensor Values \t red: ");
  Serial.print(redSensorValue);
  Serial.print("\t green: ");
  Serial.print(greenSensorValue);
  Serial.print("\t Blue: ");
  Serial.println(blueSensorValue);

  /*
  In order to use the values from the sensor for the LED, 
  you need to do some math. The ADC provides a 10-bit number, 
  but analogWrite() uses 8 bits. You'll want to divide your 
  sensor readings by 4 to keep them in range of the output. 
  */
  /* 
  Changed from a mere division by 4 in the original code to mapping. 
  Lowest value recorded at calibration would be mapped to 0, and highest value would be mapped to brightness set earlier.
  The constraint function is set to prevent flickering, when value are negative or exceeds 255.
  */
  

  int redValue = map(redSensorValue,redSensorLow,redSensorHigh,0,Brightness);
  redValue = constrain(redValue,0,255);
  int greenValue = map(greenSensorValue,greenSensorLow,greenSensorHigh,0,Brightness);
  greenValue = constrain(greenValue,0,255);
  int blueValue = map(blueSensorValue,blueSensorLow,blueSensorHigh,0,Brightness);  
  blueValue = constrain(blueValue,0,255);

  //  print out the mapped values  
  Serial.print("Mapped sensor Values \t red: ");
  Serial.print(redValue);
  Serial.print("\t green: ");
  Serial.print(greenValue);
  Serial.print("\t Blue: ");
  Serial.println(blueValue); 

  /*
  Now that you have a usable value, it's time to PWM the LED.
  */
  analogWrite(redLEDPin, redValue);
  analogWrite(greenLEDPin, greenValue);
  analogWrite(blueLEDPin, blueValue);
}

you guys dont have any problem with code or anything else i think. for making color green you need to mix yellow and blue and RGB LED dont have yellow lamp, besides you are controling it with light what makes it even less precise, instead of LDR's or after them mount potenciometers (1 for each) and you will see the diference, i got awesome colors that controling with light were impossible to have like very intense orange.very nice!! cumps

I had an issue with LED not emitting one color while doing this project. I realized the wire portions of two resistors were touching, causing cross talk. Check the physical components!

Hello,

I was just playing around after finishing this project and see if I was able to make some random color mixing.
It took me some time to google, re-calculate and some trials and errors but i guess it is working fine now.
Just curious what you think about it.
Probably there is still room for improvement.

Regards,
Ruud

/*Begining of Auto generated code by Atmel studio */
#include <Arduino.h>
const int start_stop_button = 5;
unsigned long millis();
//unsigned long previous_time = 0;
int red = 0;
int green = 0;
int blue = 0;
int start_stop_button_status = 0;
int start_stop_button_previousscan = 0;
int period_time = 0;
boolean start = false;
float redfact = 0;
float greenfact = 0;
float bluefact = 0;
float radsec =(57.29577951*2.777777778);// 360/(2*pi)*(1000/360) from millisec to sec and degrees to radians

void setup() {
	// put your setup code here, to run once:
	pinMode(start_stop_button,INPUT);
	Serial.begin(9600);
}
void loop() {
	// put your main code here, to run repeatedly:

	int start_stop_button_status = digitalRead(start_stop_button);
	int brightness = analogRead(A0)/4;
	int period_time=1+analogRead(A5)/17; //from 1 to 61 sec per sinus wave for red
	
	if(start_stop_button_status != start_stop_button_previousscan)
	if (start_stop_button_status == HIGH)
	{start = !start ; }//previous_time = millis();}
	if(start==true){
		//previous_time=100+previous_time;
		redfact=float	((1+sin((millis())/(radsec*period_time)))/2);		//scaling from -1/1 sinus to 0/1
		greenfact=float	((1+sin((millis())/(radsec*period_time*1.1)))/2);  	//create some offset for green and blue
		bluefact=float	((1+sin((millis())/(radsec*period_time*0.85)))/2);
		//	red=random(0,brightness);green=random(0,brightness);blue=random(0,brightness);}
		red=redfact*brightness; red=constrain(red,0,255);
		green=greenfact*brightness; green=constrain(green,0,255);
	blue=bluefact*brightness; blue=constrain(blue,0,255);	}
	if(start==false){red=0;green=0;blue=0;}
	
	analogWrite(9,red);analogWrite(10,green);analogWrite(11,blue);
	{start_stop_button_previousscan=start_stop_button_status;}
		
if(start==true){
	Serial.print("millis = ");
	Serial.print(millis());
	Serial.print(" brightness % = ");
	Serial.print(float(analogRead(A0)/10.23));
	Serial.print(" ");
/*	Serial.print(millis())-previous_time);
	Serial.print(" ");
*/	Serial.print(" period_time = ");
	Serial.print(period_time);
	Serial.print(" ");
		Serial.print(redfact);
		Serial.print(" ");
		Serial.print(greenfact);
		Serial.print(" ");
		Serial.print(bluefact);
		Serial.print(" ");
		Serial.print(" red = ");
		Serial.print(red);
		Serial.print(" green = ");
		Serial.print(green);
		Serial.print(" blue = ");
	Serial.print(blue);
	Serial.println();}
	//delay(pow(period_time,2));
}

Thank you kwok_leong1985. Your code was very helpful! I had similar issues with project 03 where I had to reset the LED's so that the temp wouldn't go our of whack. Somehow not everyone seems to have this issue. Why? Could it be static in the winter or does my board have a ground loop?