Introduction: 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
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);
}
}
Attachments
Step 3: Connect It!
Connect as it is on the image.
Step 4: Upload 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.

Participated in the
LED Contest with Elemental LED
12 People Made This Project!
- tliace made it!
- LiuLiu0112 made it!
- Debbie Chien made it!
- ajaninn made it!
- __dan__ made it!
- TogzhanK made it!
- StevenY20 made it!
- raiortiz made it!
- browe21 made it!
See 3 More
65 Comments
7 years ago on Introduction
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);
}
}
}
}
Reply 5 months ago
7 Years, and yet I still found this comment very useful...
Thank you very much
Question 3 years ago
how can I fix the LED keeps on blinking so fast?
5 years ago
Don't you need to limit the current going throught the leds????
https://learn.adafruit.com/adafruit-arduino-lesson-3-rgb-leds/overview
5 years ago
Made it for my ICT class in 2017
6 years ago
quick question, how do i change the rate that it dims at, because i need to be faster
6 years ago
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!
6 years ago
how to use an another led on arduino neno
7 years ago
No schematic ?
7 years ago
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!
7 years ago on Introduction
Awesome project! Now wanting to add more LEDs to make it look even more awesome!
8 years ago
i made it too .i quite modified the project having more mode of operation plz say me hows it
8 years ago
I MADE IT! too bad that button isn't on the iPhone app-- where I'm actually able to upload pics
8 years ago on Step 3
--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!
8 years ago on Step 2
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
9 years ago on Introduction
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
}
}
}
9 years ago on Introduction
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)
9 years ago on Introduction
For some reason my light sensor doesnt work :(
9 years ago on Introduction
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
9 years ago on Introduction
Oi , montei esse projeto , mas o meu só fica piscando em azul os leds o que faço?