How to do a mood lamp using Arduino

This was my first Arduino project so I decided to use only LEDs 🙂

Being Christmas I wanted to do something nice to look at night so I looked on my electronic parts box and found 3 different color LEDs : red, green and blue. I soldered their cathodes and then took 10 inches of common network cable and soldered its wires on LEDs anodes and their common cathode. This is how I build the “RGB LED”. It was a pretty ok solution though the colors aren’t perfect.


Below is the code for it (it has a lot of comments).
Basically the only settings you need to do are :
1) set the output ports on the “int leds[] = {11,10,9};” line
2) set the delay for the LEDs on the “int delaySec = 100;” line

//setup our used ports (where we have the LEDs), put them in the right order!!! Which is RGB!
//            R  G  B
int leds[] = {11,10,9};

//the values for our LEDs, in RGB order
//              R  G  B
int values[] ={255,0,0};
//we need this just to reset it to the current state
int defValues[] ={255,0,0};

//this is our counter
int cnt = -1;

//set here the delay (in milliseconds)
int delaySec = 100;

//setup function, runs only once when the board is initialized
void setup(){
  //set our ports for output (so we have power to light up the LEDs)
  for (int i=0;i<3;i++){
    pinMode(leds[i], OUTPUT);
  //call our test function

//function to test if all our LEDs are ok
void test(){
  //keep them a second on and then turn them off
  for (int i=0;i<3;i++){
    digitalWrite(leds[i], HIGH);
    digitalWrite(leds[i], LOW);
  //test them fading too
  for (int i=0;i<3;i++){
    for (int j=0;j<256;j++){
        analogWrite(leds[i], j);
    digitalWrite(leds[i], LOW);

//this function is looping for the whole execution
//the login go from Red->Green, Green->Blue, Blue->Red
void loop(){

  //increase our counter

  //take decisions
  if (cnt < 254){
    values[2] = 0;
  if ((cnt > 253) && (cnt < 509)){
    values[0] = 0;

  if ((cnt > 508) && (cnt < 763)){

  if (cnt > 762){
    values[0] = 255;
    values[1] = 0;
    values[2] = 0;
    cnt = -1;

  //change colors
  for (int i=0;i<3;i++){
    analogWrite(leds[i], values[i]);


... and a small demo (on super fast mode) of the lamp running the above code ...

Hope you enjoy this 15 minutes project 🙂

Categorized: Arduino

One comment on “How to do a mood lamp using Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.