Working with LED Lamps

In this article by Samarth Shah and Utsav Shah, the authors of Arduino BLINK Blueprints, you will learn some cool stuff to do with controlling LEDs, such as creating a mood lamp and developing an LED night lamp.

(For more resources related to this topic, see here.)

Creating a mood lamp

Lighting is one of the biggest opportunities for homeowners to effectively influence the ambience of their home, whether for comfort and convenience or to set the mood for guests. In this section, we will make a simple yet effective mood lamp using our own Arduino. We will be using an RGB LED for creating our mood lamp. An RGB (Red, Green, and Blue) LED has all three LEDs in one single package, so we don't need to use three different LEDs for getting different colors. Also, by mixing the values, we can simulate many colors using some sophisticated programming. It is said that, we can produce 16.7 million different colors.

Using an RGB LED

An RGB LED is simply three LEDs crammed into a single package. An RGB LED has four pins. Out of these four pins, one pin is the cathode (ground). As an RGB LED has all other three pins shorted with each other, it is also called a Common Anode RGB LED:

 Arduino BLINK Blueprints

Here, the longer head is the cathode, which is connected with ground, and the other three pins are connected with the power supply. Be sure to use a current-limiting resistor to protect the LED from burning out. Here, we will mix colors as we mix paint on a palette or mix audio with a mixing board. But to get a different color, we will have to write a different analog voltage to the pins of the LED.

Why do RGB LEDs change color?

As your eye has three types of light interceptor (red, green, and blue), you can mix any color you like by varying the quantities of red, green, and blue light. Your eyes and brain process the amounts of red, green, and blue, and convert them into a color of the spectrum:

 Arduino BLINK Blueprints

If we set the brightness of all our LEDs the same, the overall color of the light will be white. If we turn off the red LED, then only the green and blue LEDs will be on, which will make a cyan color. We can control the brightness of all three LEDs, making it possible to make any color. Also, the three different LEDs inside a single RGB LED might have different voltage and current levels; you can find out about them in a datasheet. For example, a red LED typically needs 2 V, while green and blue LEDs may drop up to 3-4 V.

Designing a mood lamp

Now, we are all set to use our RGB LED in our mood lamp. We will start by designing the circuit for our mood lamp. In our mood lamp, we will make a smooth transition between multiple colors.

For that, we will need following components:

  • An RGB LED
  • 270 Ω resistors (for limiting the current supplied to the LED)
  • Breadboard

As we did earlier, we need one pin to control one LED. Here, our RGB LED consists of three LEDs. So, we need three different control pins to control three LEDs. Similarly, three current-limiting resistors are required for each LED. Usually, this resistor's value can be between 100 Ω and 1000 Ω. If we use a resistor with a value higher than 1000 Ω, minimal current will flow through the circuit, resulting in negligible light emission from our LED. So, it is advisable to use a resistor having suitable resistance. Usually, a resistor of 220 Ω or 470 Ω is preferred as a current-limiting resistor.

As discussed in the earlier section, we want to control the voltage applied to each pin, so we will have to use PWM pins (3, 5, 6, 9, 10, and 11). The following schematic controls the red LED from pin 11, the blue LED from pin 10, and the green LED from pin 9. Hook the following circuit using resistors, breadboard, and your RGB LED:

 Arduino BLINK Blueprints

Once you have made the connection, write the following code in the editor window of Arduino IDE:

int redLed = 11;
int blueLed = 10;
int greenLed = 9;

void setup()
{
  pinMode(redLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
}
void loop()
{
  setColor(255, 0, 0); // Red
  delay(500);
  setColor(255, 0, 255); // Magenta
  delay(500);
  setColor(0, 0, 255); // Blue
  delay(500);
  setColor(0, 255, 255); // Cyan
  delay(500);
  setColor(0, 255, 0); // Green
  delay(500);
  setColor(255, 255, 0); // Yellow
  delay(500);
  setColor(255, 255, 255); // White
  delay(500);
}
void setColor(int red, int green, int blue)
{
  // For common anode LED, we need to substract value from 255.
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
  analogWrite(redLed, red);
  analogWrite(greenLed, green);
  analogWrite(blueLed, blue);
}

We are using very simple code for changing the color of the LED every one second interval. Here, we are setting the color every second. So, this code won't give you a smooth transition between colors. But with this code, you will be able to run the RGB LED. Now we will modify this code to smoothly transition between colors. For a smooth transition between colors, we will use the following code:

int redLed = 11;
int greenLed = 10;
int blueLed = 9;

int redValue   = 0;
int greenValue = 0;
int blueValue  = 0;

void setup(){
  randomSeed(analogRead(0));
}

void loop() {
  redValue = random(0,256); // Randomly generate 1 to 255
  greenValue = random(0,256); // Randomly generate 1 to 255
  blueValue = random(0,256); // Randomly generate 1 to 255
  
  analogWrite(redLed,redValue);
  analogWrite(greenLed,greenValue);
  analogWrite(blueLed,blueValue);

// Incrementing all the values one by one after setting the random values.
  for(redValue = 0; redValue < 255; redValue++){
    analogWrite(redLed,redValue);
    analogWrite(greenLed,greenValue);
    analogWrite(blueLed,blueValue);
    delay(10);
  }  
  for(greenValue = 0; greenValue < 255; greenValue++){
    analogWrite(redLed,redValue);
    analogWrite(greenLed,greenValue);
    analogWrite(blueLed,blueValue);
    delay(10);
  }
  for(blueValue = 0; blueValue < 255; blueValue++){
    analogWrite(redLed,redValue);
    analogWrite(greenLed,greenValue);
    analogWrite(blueLed,blueValue);
    delay(10);
  }

  //Decrementing all the values one by one for turning off all the LEDs.
  for(redValue = 255; redValue > 0; redValue--){
    analogWrite(redLed,redValue);
    analogWrite(greenLed,greenValue);
    analogWrite(blueLed,blueValue);
    delay(10);
  }
  for(greenValue = 255; greenValue > 0; greenValue--){
    analogWrite(redLed,redValue);
    analogWrite(greenLed,greenValue);
    analogWrite(blueLed,blueValue);
    delay(10);
  }
  for(blueValue = 255; blueValue > 0; blueValue--){
    analogWrite(redLed,redValue);
    analogWrite(greenLed,greenValue);
    analogWrite(blueLed,blueValue);
    delay(10);
  }
}

We want our mood lamp to repeat the same sequence of colors again and again. So, we are using the randomSeed() function. The randomSeed() function initializes the pseudo random number generator, which will start at an arbitrary point and will repeat in the same sequence again and again. This sequence is very long and random, but will always be the same. Here, pin 0 is unconnected. So, when we start our sequence using analogRead(0), it will give some random number, which is useful in initializing the random number generator with a pretty fair random number. The random(min,max) function generates the random number between min and max values provided as parameters. In the analogWrite() function, the number should be between 0 and 255. So, we are setting min and max as 0 and 255 respectively. We are setting the random value to redPulse, greenPulse, and bluePulse, which we are setting to the pins. Once a random number is generated, we increment or decrement the value generated with a step of 1, which will smooth the transition between colors.

Now we are all set to use this as mood lamp in our home. But before that we need to design the outer body of our lamp. We can use white paper (folded in a cube shape) to put around our RGB LED. White paper acts as a diffuser, which will make sure that the light is mixed together. Alternatively, you can use anything which diffuses light and make things looks beautiful! If you want to make the smaller version of the mood lamp, make a hole in a ping pong ball. Extend the RGB LED with jump wires and put that LED in the ball and you are ready to make your home look beautiful.

Developing an LED night lamp

So now we have developed our mood lamp, but it will turn on only and only when we connect a power supply to Arduino. It won't turn on or off depending on the darkness of the environment. Also, to turn it off, we have to disconnect our power supply from the Arduino. In this section, we will learn how to use switches with Arduino.

Introduction to switch

Switch is one of the most elementary and easy-to-overlook components. Switches do only one thing: either they open a circuit or short circuit. Mainly, there are two types of switches:

  • Momentary switch: Momentary switches are those switches which require continuous actuation—like a keyboard switch and reset button on Arduino board.
  • Maintained Switch: Maintained switches are those switches which, once actuated, remain actuated—like a wall switch.

Normally, all the switches are NO (Normally Opened) type switches. So, when the switch is actuated, it closes the path and acts as a perfect piece of conducting wire. Apart from this, based on their working, many switches are out there in the world, such as toggle, rotary, DIP, rocker, membrane, and so on.

Here, we will use a normal push button switch with four pins:

 Arduino BLINK Blueprints

In our push button switch, contacts A-D and B-C are short. We will connect our circuit between A and C. So, whenever you press the switch, the circuit will be complete and current will flow through the circuit. We will read the input from the button using the digitalRead() function. We will connect one pin (pin A) to the 5 V, and the other pin (pin C) to Arduino's digital input pin (pin 2). So whenever the key is pressed, it will send a 5 V signal to pin 2.

Pixar lamp

We will add a few more things in the mood lamp we discussed to make it more robust and easy to use. Along with the switch, we will add some kind of light-sensing circuit to make it automatic. We will use a Light Dependent Resistor (LDR) for sensing the light and controlling the lamp.

Basically, LDR is a resistor whose resistance changes as the light intensity changes. Mostly, the resistance of LDRs drops as light increases. For getting the value changes as per the light levels, we need to connect our LDR as per the following circuit:

 Arduino BLINK Blueprints

Here, we are using a voltage divider circuit for measuring the light intensity change. As light intensity changes, the resistance of the LDR changes, which in turn changes the voltage across the resistor. We can read the voltage from any analog pin using analogRead().

Once you have connected the circuit as shown, write the following code in the editor:

int LDR = 0; //will be getting input from pin A0
int LDRValue = 0;
int light_sensitivity = 400;    //This is the approx value of light surrounding your LDR
int LED = 13;

void setup()
{
  Serial.begin(9600);          //start the serial monitor with 9600 buad
  pinMode(LED, OUTPUT);
  
}
 
void loop()
{
  LDRValue = analogRead(LDR);     //Read the LDR's value through LDR pin A0 
  Serial.println(LDRValue);       //Print the LDR values to serial monitor
  
  if (LDRValue < light_sensitivity) 
  {
    digitalWrite(LED, HIGH);
  }
  else
  {
    digitalWrite(LED, LOW);
  }
  delay(50);        //Delay before LDR value is read again
}

In the preceding code, we are reading the value from our LDR at pin analog A0. Whenever the value read from pin A0 is certain threshold value, we are turning on the LED. So whenever the light (lux value) around the LDR drops, then the set value, it will turn on the LED, or in our case, mood lamp.

Similarly, we will add a switch in our mood lamp to make it fully functional as a Pixar lamp.

Connect one pin of the push button at 5 V and the other pin to digital pin 2. We will turn on the lamp only, and only when the room is dark and the switch is on. So we will make the following changes in the previous code.

In the setup function, initialize pin 2 as input, and in the loop add the following code:

buttonState = digitalRead(pushSwitch); //pushSwitch is initialized as 2.
If (buttonState == HIGH){

//Turn on the lamp
} 
Else {
//Turn off the lamp.
//Turn off all LEDs one by one for smoothly turning off the lamp.
  for(redValue = 255; redValue > 0; redValue--){
    analogWrite(redLed,redValue);
    analogWrite(greenLed,greenValue);
    analogWrite(blueLed,blueValue);
    delay(10);
  }
  for(greenValue = 255; greenValue > 0; greenValue--){
    analogWrite(redLed,redValue);
    analogWrite(greenLed,greenValue);
    analogWrite(blueLed,blueValue);
    delay(10);
  }
  for(blueValue = 255; blueValue > 0; blueValue--){
    analogWrite(redLed,redValue);
    analogWrite(greenLed,greenValue);
    analogWrite(blueLed,blueValue);
    delay(10);
  }
}

So, now we have incorporated an LDR and switch in our lamp to use it as a normal lamp.

Summary

In this article, we created a mood lamp with RGB LED and Pixar lamp along with developing an LED night lamp

Resources for Article:


Further resources on this subject:


You've been reading an excerpt of:

Arduino BLINK Blueprints

Explore Title