Introduction: Mood Lamp With Arduino

Picture of Mood Lamp With Arduino

Hi! In this tutorial you will learn to make a mood lamp with arduino.
I know that you may have seen a lot of mood lamp projects with arduino, but I wasn't very satisfied with them because they all change the color very abruptly. So, I decided to make a smooth mood lamp and I also made it to turn on only when the room is dark.
This project is good for those who are beginners in the arduino's world.

Step 1: Material

Picture of Material

For this instructable you are gonna need:
- Arduino
- Jumper wires
- 1 RGB LED or 1 red LED, 1 green LED and 1 blue LED
- LDR (Light Dependent Resistor)
- Protoboard
- Sheet of paper

Step 2: Time to Code!

Write the code above on the Arduino program.
Darker the ambient light is, the higher is the value read from the LDR.
I used PWM to change led's brightness.


// Smooth RGB mood lamp
// Changes an RGB LED's color smoothly that only turns on
// when it's dark around it.
// Author: Ricardo Ouvina
// Date: 19/07/2012
// Version: 2.0
// ---------------------------------------------------
// The brightness of the leds follows these equations:
// Red = sin(x)
// Green = sin(x + PI/3)
// Blue = sin(x + 2PI/3)
// for x from 0 to PI
// ---------------------------------------------------

float RGB[3];
int ldrPin = 0;     // LDR in Analog Input 0 to read the ambient light
int ambientLight;   // variable to store the value of the ambient light
int redLed   = 11;  // red LED in Digital Pin 11 (PWM)
int greenLed = 10;  // green LED in Digital Pin 10 (PWM)
int blueLed  = 9;   // blue LED in Digital Pin 9 (PWM)

void setup(){
  pinMode(redLed,OUTPUT);  // tell arduino it's an output
  pinMode(greenLed,OUTPUT);// tell arduino it's an output
  pinMode(blueLed,OUTPUT); // tell arduino it's an output
 
  // set all the outputs to low
  digitalWrite(redLed,LOW);
  digitalWrite(greenLed,LOW);
  digitalWrite(blueLed,LOW);
}

void loop(){
  for (float x=0;x<PI;x=x+0.00001){
    RGB[0]=255*abs(sin(x*(180/PI)));           // calculate the brightness for the red led
    RGB[1]=255*abs(sin((x+PI/3)*(180/PI)));    // calculate the brightness for the green led
    RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led
    ambientLight=analogRead(ldrPin); // read an store the ambient light
    if(ambientLight>600){ // start only if the ambient light is very low
      //  write the brightness on the leds
      analogWrite(redLed,RGB[0]);
      analogWrite(greenLed,RGB[1]);
      analogWrite(blueLed,RGB[2]);
    }
    else{
      digitalWrite(redLed,LOW);
      digitalWrite(greenLed,LOW);
      digitalWrite(blueLed,LOW);
    }
    for(int i=0;i<3;i++){
      if(RGB[i]<1){
        delay(100);
      }
      if(RGB[i]<5){
        delay(50);
      }
      if(RGB[i]<10){
        delay(10);
      }
      if(RGB[i]<100){
        delay(5);
      }
    }
    delay(1);
  }
}

Step 3: Connect It!

Picture of Connect It!

Connect as it is on the image.

Step 4: Upload and It's Done!

Picture of Upload and It's Done!
Upload the code to the arduino and it's done.
Try to add a sheet of paper around it to see it more smoothly, the paper acts as a diffuser.
Feel free to modify this project to your way, and tell me your progress. Comments are welcome.
Have a look at  the video I've made.

Comments

__dan__ made it! (author)2017-08-24

Great Instructable! Thank you so much!

codeyoneill113 (author)2017-07-17

Made it for my ICT class in 2017

MichaelR287 (author)2016-05-04

quick question, how do i change the rate that it dims at, because i need to be faster

Brandon15601 (author)2016-04-28

Hey, has anyone figured out a code utilizing this concept but with a temperature sensor to change the color of the LED with body temp? Preferably adnafie's code listed below in the comments. Thanks!

TogzhanK made it! (author)2016-04-24

Nice work!!! I dit it with 4 colours

// Smooth RGB mood lamp

// Changes an RGB LED's color smoothly that only turns on

// when it's dark around it.

// Author: Ricardo Ouvina

// Date: 19/07/2012

// Version: 2.0

// ---------------------------------------------------

// The brightness of the leds follows these equations:

// Red = sin(x)

// Green = sin(x + PI/3)

// Blue = sin(x + 2PI/3)

// for x from 0 to PI

// ---------------------------------------------------

float RGB[3];

int ldrPin = 0; // LDR in Analog Input 0 to read the ambient light

int ambientLight; // variable to store the value of the ambient light

int redLed = 11; // red LED in Digital Pin 11 (PWM)

int greenLed = 10; // green LED in Digital Pin 10 (PWM)

int blueLed = 9; // blue LED in Digital Pin 9 (PWM)

int yellowLed = 8; // yellow LED in Digital Pin 8 (PWM)

void setup(){

pinMode(redLed,OUTPUT); // tell arduino it's an output

pinMode(greenLed,OUTPUT);// tell arduino it's an output

pinMode(blueLed,OUTPUT); // tell arduino it's an output

pinMode(yellowLed,OUTPUT); // tell arduino it's an output

// set all the outputs to low

digitalWrite(redLed,LOW);

digitalWrite(greenLed,LOW);

digitalWrite(blueLed,LOW);

digitalWrite(yellowLed,LOW);

}

void loop(){

for (float x=0;x<PI;x=x+0.00001){

RGB[0]=255*abs(sin(x*(180/PI))); // calculate the brightness for the red led

RGB[1]=255*abs(sin((x+PI/3)*(180/PI))); // calculate the brightness for the green led

RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led

RGB[3]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the yellow led

ambientLight=analogRead(ldrPin); // read an store the ambient light

if(ambientLight>600){ // start only if the ambient light is very low

// write the brightness on the leds

analogWrite(redLed,RGB[0]);

analogWrite(greenLed,RGB[1]);

analogWrite(blueLed,RGB[2]);

analogWrite(yellowLed,RGB[3]);

}

else{

digitalWrite(redLed,LOW);

digitalWrite(greenLed,LOW);

digitalWrite(blueLed,LOW);

digitalWrite(yellowLed,LOW);

}

for(int i=0;i<3;i++){

if(RGB[i]<1){

delay(100);

}

if(RGB[i]<5){

delay(50);

}

if(RGB[i]<10){

delay(10);

}

if(RGB[i]<100){

delay(5);

}

}

delay(1);

}

}

StevenY20 made it! (author)2016-04-14

ambientLight>200 worked for me!

silkroute (author)2016-04-14

how to use an another led on arduino neno

AjiS10 (author)2016-03-27

No schematic ?

Gemm87 (author)2015-10-09

I made it but I'd love to know if there are any good resources out there to help me understand the code - I have learned a couple of basics but some of the terms still confuse me. Thanks for the project it's a great early confidence builder when it comes to the circuit!

jinstruct (author)2015-09-14

Very good idea! Pls. find my modification of the code that is also smooth and shows all colors in the spectrum with higher intensity:



float RGB[3];
int redLed = 11; // red LED in Digital Pin 11 (PWM)
int greenLed = 10; // green LED in Digital Pin 10 (PWM)
int blueLed = 9; // blue LED in Digital Pin 9 (PWM)
int delaymood = 100; // delay for mood light

void setup(){
pinMode(redLed,OUTPUT);
pinMode(greenLed,OUTPUT);
pinMode(blueLed,OUTPUT);
digitalWrite(redLed,HIGH);
digitalWrite(greenLed,LOW);
digitalWrite(blueLed,LOW);
}

void loop(){
for (int y=0; y<=3; y++) {
if (y=1){
for (int x=0;x<=255;x++){
RGB[0]=255-x;
RGB[1]=0;
RGB[2]=0+x;
analogWrite(redLed,RGB[0]);
analogWrite(greenLed,RGB[1]);
analogWrite(blueLed,RGB[2]);
delay(delaymood);
}
}
if (y=2){
for (int x=0;x<=255;x++){
RGB[0]=0;
RGB[1]=0+x;
RGB[2]=255-x;
analogWrite(redLed,RGB[0]);
analogWrite(greenLed,RGB[1]);
analogWrite(blueLed,RGB[2]);
delay(delaymood);
}
}
if (y=3){
for (int x=0;x<=255;x++){
RGB[0]=0+x;
RGB[1]=255-x;
RGB[2]=0;
analogWrite(redLed,RGB[0]);
analogWrite(greenLed,RGB[1]);
analogWrite(blueLed,RGB[2]);
delay(delaymood);
}
}
}
}

justinrains (author)2015-07-27

Awesome project! Now wanting to add more LEDs to make it look even more awesome!

raiortiz made it! (author)2015-07-22

Very easy to follow thanks.

browe21 made it! (author)2015-06-16

Awesome project quick and easy to follow!

adamantum made it! (author)2014-10-24

Simple and Easy Project. Made in an Hour. Thank You :)

adamantum made it! (author)2014-10-24

Simple and Easy Project. Made in an Hour! Thank You "ricadouvina".

geekrex (author)2014-09-26

i made it too .i quite modified the project having more mode of operation plz say me hows it

agrady1995 (author)2014-04-11

I MADE IT! too bad that button isn't on the iPhone app-- where I'm actually able to upload pics

agrady1995 (author)2014-04-11

--Made it in 10 minutes of opening the instructable. It was cool, but it would turn itself off with the LDR as soon as the light came on (I followed the breadboard exactally, and it was close to the LED).

So, about to move it, I took out the LDR, and it worked.... I'll just run it without it. Thank you!

agrady1995 (author)2014-04-10

hi there! I just got my arduino the other day, but i'm kind of quick to learning programming. can you explain the parts of your (equation) that made the lights change the way they did, and how it worked? Thanks!

-- Student and future Biomedical Engineer

Electric_LED made it! (author)2014-03-24

Thanks so much!!! I did this quickly and easily!!!

adnafie (author)2014-03-05

Hi! thank you for the tutorial. But, i'm have a little problem here. See, i tired to combine this circuit with lm35 temperature sensor so that the circuit can sense temperature and light at the same time. I use different led for different sensor. Now the problem is, when i combine the 2 source code, the mood lamp seems not working well. It lighted up but it dont changing colors. Can you help me with this? Here is my source code:

float RGB[3];

float tempC;

int ldrPin = 0; // LDR in Analog Input 0 to read the ambient light

int analog_tempPin1 = 1;

int ambientLight; // variable to store the value of the ambient light

int ledPin1 = 11; // red LED in Digital Pin 11 (PWM)

int ledPin2 = 10; // green LED in Digital Pin 10 (PWM)

int ledPin3 = 9; // blue LED in Digital Pin 9 (PWM)

int ledPin4 = 7; // LED connected to digital pin 7

int ledPin5 = 6;

void setup(){

pinMode(ledPin1,OUTPUT); // tell arduino it's an output

pinMode(ledPin2,OUTPUT);// tell arduino it's an output

pinMode(ledPin3,OUTPUT); // tell arduino it's an output

// set all the outputs to low

digitalWrite(ledPin1,LOW);

digitalWrite(ledPin2,LOW);

digitalWrite(ledPin3,LOW);

pinMode(ledPin4, OUTPUT);

pinMode(ledPin5, OUTPUT);

Serial.begin(9600);

}

void loop(){

for (float x=0;x<PI;x=x+0.00001){

RGB[0]=255*abs(sin(x*(180/PI))); // calculate the brightness for the red led

RGB[1]=255*abs(sin((x+PI/3)*(180/PI))); // calculate the brightness for the green led

RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led

ambientLight=analogRead(ldrPin); // read an store the ambient light

if(ambientLight>600){ // start only if the ambient light is very low

// write the brightness on the leds

analogWrite(ledPin1,RGB[0]);

analogWrite(ledPin2,RGB[1]);

analogWrite(ledPin3,RGB[2]);

}

else{

digitalWrite(ledPin1,LOW);

digitalWrite(ledPin2,LOW);

digitalWrite(ledPin3,LOW);

}

for(int i=0;i<3;i++){

if(RGB[i]<1){

delay(100);

}

if(RGB[i]<5){

delay(50);

}

if(RGB[i]<10){

delay(10);

}

if(RGB[i]<100){

delay(5);

}

}

delay(1);

tempC = analogRead(analog_tempPin1); //read the value from the sensor

tempC = (5.0 * tempC * 100.0)/1024.0; //convert the analog data to temperature

Serial.print("analog ");

Serial.print(analog_tempPin1);

Serial.print(" ");

Serial.println(tempC); //send the data to the computer

if (tempC < 40){

digitalWrite(ledPin4, HIGH); // set the LED on

digitalWrite(ledPin5, LOW); // set the LED off

delay(1000); // wait for a second

digitalWrite(ledPin4, LOW); // set the LED off

digitalWrite(ledPin5, HIGH); // set the LED on

delay(1000); // wait for a second

}

if (tempC > 40){

digitalWrite(ledPin4, HIGH); // set the LED on

digitalWrite(ledPin5, LOW); // set the LED off

delay(100); // wait for 0.1second

digitalWrite(ledPin4, LOW); // set the LED off

digitalWrite(ledPin5, HIGH); // set the LED on

delay(100); // wait for 0.1second

}

}

}

bsüataç (author)2014-01-23

Great instructions! I have a problem tough. I managed to make it change lights and everything. However , When it comes to certain colors it flickers with different frequencies. How can I solve this problem? (I have an Arduino UNO and I use a RGB LED)

Solistas (author)2014-01-20

For some reason my light sensor doesnt work :(

XxsonicxX (author)2013-08-31

While I was buying supplies at radioshack, I accidentally bought the wrong resistors, I bought 1/4 watt 220 ohm resistors instead of the 120k ohm resistors needed... Arduino FAIL

eliara (author)2013-06-04

Oi , montei esse projeto , mas o meu só fica piscando em azul os leds o que faço?

ricardouvina (author)eliara2013-06-09

Olá, verifique se todos os caminhos que você montou estão como no esquemático e para garantir baixe o arquivo ZIP ao invés de copiar direto da página. Se ainda não funcionar me avise, ficarei feliz em ajudar. Ah, e você é a primeira brasileira a comentar aqui! =)

d4ng (author)2013-06-04

Great Instructable! My first "real" Arduino project.

I am attempting to change/add the colors the my RGB LED cycles through.
I have attempted several things but am hitting a dead end..any ideas?

knobhill (author)2013-05-17

I like the project a lot - good work - I want to try putting one together, but if I wanted to modify things so that the leds work under any light (it doesn't have to be pitch black ), how would I go about doing that ?

ricardouvina (author)knobhill2013-05-18

Just remove the following lines:
if(ambientLight>600){ // start only if the ambient light is very low
// write the brightness on the leds
analogWrite(redLed,RGB[0]);
analogWrite(greenLed,RGB[1]);
analogWrite(blueLed,RGB[2]);
}
else{
digitalWrite(redLed,LOW);
digitalWrite(greenLed,LOW);
digitalWrite(blueLed,LOW);
}

and substitute them by:
analogWrite(redLed,RGB[0]);
analogWrite(greenLed,RGB[1]);
analogWrite(blueLed,RGB[2]);
without the if loop.
I haven't tried this but I think this should work.

louisbourdon (author)2013-05-01

I have tried of course to use a break inside the for loop, however it does not work!!! I have tried using an outside command via OSC, and also hit a key from the Serial Monitor to break the loop. Nothing works, it looks like it is stuck there - like it waits to finish the whole loop and then exit. But I want to make it happen instantly but so far nothing works! If anyone has a tip, that would be great!

louisbourdon (author)2013-04-30

Hi, nice project. I would like to ask you, how is it possible to escape the for loop? I want to use this as a feature in my project against other features (i.e. Mode 1 - All lights off, Mode 2 - All lights on, Mode 3 - Mood Lights). Using a control I want to go from one mode to the other, however, when I use this for loop everything gets stuck as it is a very long loop and takes for ever to complete. What do you suggest?

for (float x=0;x RGB[0]=255*abs(sin(x*(180/PI))); // calculate the brightness for the red led
RGB[1]=255*abs(sin((x+PI/3)*(180/PI))); // calculate the brightness for the green led
RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led
}

What you can do is use a "break" inside the "if" loop to stop it, but use it carefully, otherwise it can be very harmful.

pabhilash (author)2013-01-07

Hello again! never mind, it's working fine now! i misplaced a wire :P thanks so much! may i suggest something? when you define the ambient light settings, you should try changing it to 900 instead of 700. under 700, the LED's glow even in a fairly lit room! :)

ricardouvina (author)pabhilash2013-01-19

OK! That value is up to you. You choose what better works to you!

pabhilash (author)2012-12-23

Hello! great I'ble! There seems to be a small problem- uploading gets done fine, but immediately the LED(RGB) just glows light blue,even in a well lit area! and the color does not change! could you please help?(it's an Uno by the way..)

ricardouvina (author)pabhilash2012-12-23

Did you download the zip file, or did you copy the code from the step and paste it on arduino's program?

sarah91 (author)2012-12-12

What do I change when it is 1 red LED, 1 green LED and 1 yellow LED?

ricardouvina (author)sarah912012-12-12

Nothing. You will just get different colors than mine.

geoffb37 (author)2012-10-19

hi
me again, how can i get it to cycle thru green/ it does blue thru red/orange/purple but not green?

Vertec (author)2012-10-15

Hi , I really like your project , I was trying to build it , and i am stuck in a situation that i am using RGB LED , (one that you have shown in your project also first image) but I have common anode , it change the light effect exactly as you describe in your project , but my LDR is not working , means its not light sensitive,
1) For the common anode RGB LED , only the polarity of the wires will be change (if i follow your design) ? or any thin else

Vertec (author)Vertec2012-10-17

Hi , Thanks for your reply can you please tell me the value of the resistor you have used for it , or the minimum value of the resistor i can use with the LDR? (it must be start with 10k at least) if i am not wrong !

ricardouvina (author)Vertec2012-10-18

I used a 120k resistor, but anything between 10k and 1M should work just fine, but remember to adjust the ambient light threshold as I said before.
I hope you can make it! :)

ricardouvina (author)Vertec2012-10-16

Hi.
Yes, for the common anode RGB LED only the polarity changes, you must be having trouble with the LDR due to the value of the resistor that you're using with it, try to change the ambient light threshold, try to adjust the code at:
if(ambientLight>600){ // start only if the ambient light is very low

change the number 600 to a value that works for you, try to increase or decrease it.

Ozzie_G (author)2012-10-13

Nice little project, Thanks

What exactly are you trying to achieve with the delays depending on the current brightness of all three channels?

ricardouvina (author)Ozzie_G2012-10-14

Hi!
Show all the colors in a long period of time, otherwise the red, blue and green colors would be on for a very short period of time, and it would change very quickly and I wanted it to be very smooth.

Thanks for your comment!

ponynicker53 (author)2012-10-06

My RGB LED has the second lead as positive, and all the colors as negative. So when I wire it as described, with the long terminal on the LED hooked to ground, nothing happens. When I move that wire to the positive (5V), it lights up, but constantly and all colors are on.
Any ideas how to get it working?

Ok, I switched the positive and negative wires on the arduino, and now it rotates through the colors, however it does it all the time, the photosensor doesnt seem to turn it off. I tried switching the negative/positive orientation on it, but no change.

Also, how would you make it fade through the whole spectrum of light?

Try to adjust the code at:
if(ambientLight>600){ // start only if the ambient light is very low

change the number 600 to a value that works for you, try to increase or decrease it.

About the spectrum of light, I'm sorry but I don't know how to make. Try to search on the web, probably there is some kind of sine function that does that.

kartikrv (author)2012-09-26

Hey!

First of all, this is a pretty sweet project. I'm just getting myself used to some pre-done projects before jumping into my own stuff.

My question: how would the circuit change if you dont have an ldr?

Thanks!

ricardouvina (author)kartikrv2012-09-27

Hi!
It's very simple, just remove the "if" function and the ambientLight variable, like this:

float RGB[3];
int redLed = 11; // red LED in Digital Pin 11 (PWM)
int greenLed = 10; // green LED in Digital Pin 10 (PWM)
int blueLed = 9; // blue LED in Digital Pin 9 (PWM)

void setup(){
pinMode(redLed,OUTPUT); // tell arduino it's an output
pinMode(greenLed,OUTPUT);// tell arduino it's an output
pinMode(blueLed,OUTPUT); // tell arduino it's an output

// set all the outputs to low
digitalWrite(redLed,LOW);
digitalWrite(greenLed,LOW);
digitalWrite(blueLed,LOW);
}

void loop(){
for (float x=0;x RGB[0]=255*abs(sin(x*(180/PI))); // calculate the brightness for the red led
RGB[1]=255*abs(sin((x+PI/3)*(180/PI))); // calculate the brightness for the green led
RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led

// write the brightness on the leds
analogWrite(redLed,RGB[0]);
analogWrite(greenLed,RGB[1]);
analogWrite(blueLed,RGB[2]);

for(int i=0;i<3;i++){
if(RGB[i]<1){
delay(100);
}
if(RGB[i]<5){
delay(50);
}
if(RGB[i]<10){
delay(10);
}
if(RGB[i]<100){
delay(5);
}
}
delay(1);
}
}

I hope it helps, thank for the comment!

About This Instructable

102,751views

213favorites

License:

More by ricardouvina:Simple IR proximity sensor with ArduinoMood Lamp with Arduino
Add instructable to: