loading
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

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
<p>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!</p>
<p>Very good idea! Pls. find my modification of the code that is also smooth and shows all colors in the spectrum with higher intensity: </p><p><br><br>float RGB[3];<br>int redLed = 11; // red LED in Digital Pin 11 (PWM)<br>int greenLed = 10; // green LED in Digital Pin 10 (PWM)<br>int blueLed = 9; // blue LED in Digital Pin 9 (PWM)<br>int delaymood = 100; // delay for mood light<br><br>void setup(){<br> pinMode(redLed,OUTPUT); <br> pinMode(greenLed,OUTPUT);<br> pinMode(blueLed,OUTPUT); <br> digitalWrite(redLed,HIGH);<br> digitalWrite(greenLed,LOW);<br> digitalWrite(blueLed,LOW);<br>}<br><br>void loop(){<br> for (int y=0; y&lt;=3; y++) {<br> if (y=1){<br> for (int x=0;x&lt;=255;x++){<br> RGB[0]=255-x; <br> RGB[1]=0; <br> RGB[2]=0+x;<br> analogWrite(redLed,RGB[0]);<br> analogWrite(greenLed,RGB[1]);<br> analogWrite(blueLed,RGB[2]); <br> delay(delaymood);<br> }<br> }<br> if (y=2){<br> for (int x=0;x&lt;=255;x++){<br> RGB[0]=0; <br> RGB[1]=0+x; <br> RGB[2]=255-x;<br> analogWrite(redLed,RGB[0]);<br> analogWrite(greenLed,RGB[1]);<br> analogWrite(blueLed,RGB[2]); <br> delay(delaymood);<br> }<br> }<br> if (y=3){<br> for (int x=0;x&lt;=255;x++){<br> RGB[0]=0+x; <br> RGB[1]=255-x; <br> RGB[2]=0;<br> analogWrite(redLed,RGB[0]);<br> analogWrite(greenLed,RGB[1]);<br> analogWrite(blueLed,RGB[2]); <br> delay(delaymood);<br> }<br> }<br> }<br>}</p>
<p>Awesome project! Now wanting to add more LEDs to make it look even more awesome!</p>
<p>Very easy to follow thanks.</p>
<p>Awesome project quick and easy to follow!</p>
<p>Simple and Easy Project. Made in an Hour. Thank You :)</p>
<p>Simple and Easy Project. Made in an Hour! Thank You &quot;ricadouvina&quot;.</p>
i made it too .i quite modified the project having more mode of operation plz say me hows it
I MADE IT! too bad that button isn't on the iPhone app-- where I'm actually able to upload pics
<p>--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).</p><p>So, about to move it, I took out the LDR, and it worked.... I'll just run it without it. Thank you!</p>
<p>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!</p><p>-- Student and future Biomedical Engineer</p>
<p>Thanks so much!!! I did this quickly and easily!!! </p>
<p>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:</p><p>float RGB[3];</p><p>float tempC;</p><p>int ldrPin = 0; // LDR in Analog Input 0 to read the ambient light</p><p>int analog_tempPin1 = 1;</p><p>int ambientLight; // variable to store the value of the ambient light</p><p>int ledPin1 = 11; // red LED in Digital Pin 11 (PWM)</p><p>int ledPin2 = 10; // green LED in Digital Pin 10 (PWM)</p><p>int ledPin3 = 9; // blue LED in Digital Pin 9 (PWM)</p><p>int ledPin4 = 7; // LED connected to digital pin 7</p><p>int ledPin5 = 6;</p><p>void setup(){</p><p> pinMode(ledPin1,OUTPUT); // tell arduino it's an output</p><p> pinMode(ledPin2,OUTPUT);// tell arduino it's an output</p><p> pinMode(ledPin3,OUTPUT); // tell arduino it's an output</p><p> // set all the outputs to low </p><p> digitalWrite(ledPin1,LOW);</p><p> digitalWrite(ledPin2,LOW);</p><p> digitalWrite(ledPin3,LOW);</p><p> pinMode(ledPin4, OUTPUT);</p><p> pinMode(ledPin5, OUTPUT);</p><p> Serial.begin(9600);</p><p>}</p><p>void loop(){</p><p> for (float x=0;x&lt;PI;x=x+0.00001){</p><p> RGB[0]=255*abs(sin(x*(180/PI))); // calculate the brightness for the red led</p><p> RGB[1]=255*abs(sin((x+PI/3)*(180/PI))); // calculate the brightness for the green led</p><p> RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led</p><p> ambientLight=analogRead(ldrPin); // read an store the ambient light</p><p> if(ambientLight&gt;600){ // start only if the ambient light is very low</p><p> // write the brightness on the leds</p><p> analogWrite(ledPin1,RGB[0]); </p><p> analogWrite(ledPin2,RGB[1]);</p><p> analogWrite(ledPin3,RGB[2]);</p><p> }</p><p> else{</p><p> digitalWrite(ledPin1,LOW);</p><p> digitalWrite(ledPin2,LOW);</p><p> digitalWrite(ledPin3,LOW);</p><p> }</p><p> for(int i=0;i&lt;3;i++){</p><p> if(RGB[i]&lt;1){</p><p> delay(100);</p><p> }</p><p> if(RGB[i]&lt;5){</p><p> delay(50);</p><p> }</p><p> if(RGB[i]&lt;10){</p><p> delay(10);</p><p> }</p><p> if(RGB[i]&lt;100){</p><p> delay(5);</p><p> }</p><p> }</p><p> delay(1);</p><p> tempC = analogRead(analog_tempPin1); //read the value from the sensor</p><p> tempC = (5.0 * tempC * 100.0)/1024.0; //convert the analog data to temperature</p><p> Serial.print(&quot;analog &quot;);</p><p> Serial.print(analog_tempPin1);</p><p> Serial.print(&quot; &quot;);</p><p> Serial.println(tempC); //send the data to the computer </p><p> if (tempC &lt; 40){</p><p> digitalWrite(ledPin4, HIGH); // set the LED on</p><p> digitalWrite(ledPin5, LOW); // set the LED off</p><p> delay(1000); // wait for a second</p><p> digitalWrite(ledPin4, LOW); // set the LED off</p><p> digitalWrite(ledPin5, HIGH); // set the LED on</p><p> delay(1000); // wait for a second</p><p> }</p><p> if (tempC &gt; 40){</p><p> digitalWrite(ledPin4, HIGH); // set the LED on</p><p> digitalWrite(ledPin5, LOW); // set the LED off</p><p> delay(100); // wait for 0.1second</p><p> digitalWrite(ledPin4, LOW); // set the LED off</p><p> digitalWrite(ledPin5, HIGH); // set the LED on</p><p> delay(100); // wait for 0.1second</p><p> }</p><p> }</p><p>}</p>
<p>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)</p>
<p>For some reason my light sensor doesnt work :(</p>
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
Oi , montei esse projeto , mas o meu s&oacute; fica piscando em azul os leds o que fa&ccedil;o?
Ol&aacute;, verifique se todos os caminhos que voc&ecirc; montou est&atilde;o como no esquem&aacute;tico e para garantir baixe o arquivo ZIP ao inv&eacute;s de copiar direto da p&aacute;gina. Se ainda n&atilde;o funcionar me avise, ficarei feliz em ajudar. Ah, e voc&ecirc; &eacute; a primeira brasileira a comentar aqui! =)
Great Instructable! My first &quot;real&quot; Arduino project. <br> <br>I am attempting to change/add the colors the my RGB LED cycles through. <br>I have attempted several things but am hitting a dead end..any ideas?
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 ?
Just remove the following lines: <br> if(ambientLight&gt;600){ // start only if the ambient light is very low <br> // write the brightness on the leds <br> analogWrite(redLed,RGB[0]); <br> analogWrite(greenLed,RGB[1]); <br> analogWrite(blueLed,RGB[2]); <br> } <br> else{ <br> digitalWrite(redLed,LOW); <br> digitalWrite(greenLed,LOW); <br> digitalWrite(blueLed,LOW); <br> } <br> <br>and substitute them by: <br> analogWrite(redLed,RGB[0]); <br> analogWrite(greenLed,RGB[1]); <br> analogWrite(blueLed,RGB[2]); <br>without the if loop. <br>I haven't tried this but I think this should work.
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!
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? <br> <br>for (float x=0;x RGB[0]=255*abs(sin(x*(180/PI))); // calculate the brightness for the red led <br> RGB[1]=255*abs(sin((x+PI/3)*(180/PI))); // calculate the brightness for the green led <br> RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led <br>}
What you can do is use a &quot;break&quot; inside the &quot;if&quot; loop to stop it, but use it carefully, otherwise it can be very harmful.
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! :)
OK! That value is up to you. You choose what better works to you!
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..)
Did you download the zip file, or did you copy the code from the step and paste it on arduino's program?
What do I change when it is 1 red LED, 1 green LED and 1 yellow LED?
Nothing. You will just get different colors than mine.
hi <br>me again, how can i get it to cycle thru green/ it does blue thru red/orange/purple but not green?
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, <br> 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
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 !
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. <br>I hope you can make it! :)
Hi. <br>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: <br> if(ambientLight&gt;600){ // start only if the ambient light is very low <br> <br>change the number 600 to a value that works for you, try to increase or decrease it.
Nice little project, Thanks <br> <br>What exactly are you trying to achieve with the delays depending on the current brightness of all three channels? <br> <br>
Hi! <br>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. <br> <br>Thanks for your comment!
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. <br>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. <br> <br>Also, how would you make it fade through the whole spectrum of light?
Try to adjust the code at: <br> if(ambientLight&gt;600){ // start only if the ambient light is very low <br> <br>change the number 600 to a value that works for you, try to increase or decrease it. <br> <br>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.
Hey! <br> <br>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. <br> <br>My question: how would the circuit change if you dont have an ldr? <br> <br>Thanks!
Hi! <br>It's very simple, just remove the &quot;if&quot; function and the ambientLight variable, like this: <br> <br>float RGB[3]; <br>int redLed = 11; // red LED in Digital Pin 11 (PWM) <br>int greenLed = 10; // green LED in Digital Pin 10 (PWM) <br>int blueLed = 9; // blue LED in Digital Pin 9 (PWM) <br> <br>void setup(){ <br> pinMode(redLed,OUTPUT); // tell arduino it's an output <br> pinMode(greenLed,OUTPUT);// tell arduino it's an output <br> pinMode(blueLed,OUTPUT); // tell arduino it's an output <br> <br> // set all the outputs to low <br> digitalWrite(redLed,LOW); <br> digitalWrite(greenLed,LOW); <br> digitalWrite(blueLed,LOW); <br>} <br> <br>void loop(){ <br> for (float x=0;x RGB[0]=255*abs(sin(x*(180/PI))); // calculate the brightness for the red led <br> RGB[1]=255*abs(sin((x+PI/3)*(180/PI))); // calculate the brightness for the green led <br> RGB[2]=255*abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the brightness for the blue led <br> <br> // write the brightness on the leds <br> analogWrite(redLed,RGB[0]); <br> analogWrite(greenLed,RGB[1]); <br> analogWrite(blueLed,RGB[2]); <br> <br> for(int i=0;i&lt;3;i++){ <br> if(RGB[i]&lt;1){ <br> delay(100); <br> } <br> if(RGB[i]&lt;5){ <br> delay(50); <br> } <br> if(RGB[i]&lt;10){ <br> delay(10); <br> } <br> if(RGB[i]&lt;100){ <br> delay(5); <br> } <br> } <br> delay(1); <br> } <br>} <br> <br>I hope it helps, thank for the comment!
So nice ! I was going to use it for my good friend.
I hope he likes!
Excellent first project for myself with the limited things that came with my uno kit. <br>How do you think we could say put a strobe effect in there via a switch? <br>Many thanks for the post !! <br>On all the time as i havent found near a 120K resistor yet lol. <br>
Hi! I think that it's possible to add a strobe effect, you just have to write a function for that, and when the switch is activated you just have to call that function, if is that what you mean. <br>It would be something like that: <br>if(switch = ON){ <br> strobe(); <br>} <br>else{ <br> moodLamp(); <br>} <br> <br> <br>About the resistor, any other near value should work, you just have to adjust the value of the ambient light. <br>I hope you can do it and make an instructable. <br>
Great project, I will try it out tonight and let you know how it goes! <br>
ok!
It works perfectly, thanks a lot!
you helped so much. I have been wanting to do this! Now with your code it is possible.

About This Instructable

78,220 views

184 favorites

License:

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