Project 2. Fade, pulse and multiple colors

In the last project, you cobbled together a very simple digital circuit that made a single LED blink. In this project, you will see how to make LEDs fade in and out.

In a traditional incandescent light bulb, the brightness of the bulb can be controlled simply by adjusting the voltage across the filament. This works because these bulbs operate at 120 volts, and a simple rheostat (that is, a variable resistor) is sufficient to increase or decrease the voltage, such that at 60 volts or 90 volts, it will glow slightly dimmer than at maximum voltage. (The values aren’t linear, so it’s not half as bright at 60 volts or whatever. The calculations and explanations are quite technical, and beyond the scope of this blog.) However, an LED requires an exact voltage to operate, with very little tolerance for variance, such that a 5 volt LED works on a voltage range from 4.5 to 5.5 volts (hypothetical numbers, purely to demonstrate the concept). So how can you make an LED “fade”, so that its brightness seems variable? The answer is a capability built into the Arduino, known as Pulse Width Modulation.

Pulse Width Modulation, hereafter referred to as PWM, is a technology used in many digital circuits. PWM means pulses that are on for some percentage of the time, and off for the remainder of the time. Arduino PWM pulses 255 times per second, and the “on” time (known as the duty cycle in technical circles) is how much of each of the pulse is High

512e869bce395fbc64000002
PWM example. Image source: https://learn.sparkfun.com/tutorials/pulse-width-modulation

If you look down the row of digital pins on the Arduino, you will notice that pins 3, 5, 6, 9, 10 and 11 are marked with a tilde (~). These are the pins that support PWM.
[[image]]
By connecting the LEDs to these, and writing the program to use PWM output, you can make it appear to be dimmer or brighter. In reality, they are just switching on and off, but it happens so quickly, much quicker than the eye can perceive, that it appears to be dimmer or brighter.

To make this happen, you will retain the board setup from project 1. The only difference is in the programming, and those differences are relatively trivial.

int ledPin = 10;
void setup() {
//nothing to see here

}

void loop() {
for(int k = 0; k <= 255; k += 5)
{
 analogWrite(ledPin, k);
 delay(30);
}
for(int k = 255; k >= 0; k -= 5)
{
 analogWrite(ledPin, k);
 delay(30);
}
}

In this program, you will notice two differences: the two “for” loops, and that the pins use analogWrite() instead of digitalWrite(). Part of the Arduino library includes functions that convert values written in analogWrite() into PWM values. In particular, it uses values from 0 to 255 to define the duty cycle (see above) of the pin. A value of 0 means 0% duty cycle, the same as digitalWrite(ledPin, LOW); a value of 255 means 100% duty cycle, the same as digitalWrite(ledPin, HIGH). The for loops are arranged in such a way that it increases or decreases the duty cycle by approximately 5% with each execution of the loop. The delay of 30 ms is to make the fade effect visible.

[[gif video of the fade effect taking place]]

By adjusting the values of the delay and/or the third statement defining the for loop [k +=/-= 5], you can adjust the time frame on which the fade takes place.

Finally, and for some added fun times, you can take an RGB LED and hook it up to fade the three colors in and out for mixed colors. The RGB LED used in this example is a common anode: you will connect the longest pin to the voltage source (+) and the other three will act as ground (-), in this case flowing into the Arduino itself. led-rgb1
Common anode RGB LED pinout diagram. Image source: http://www.hertaville.com/files/uploads/2012/09/LED-RGB1.png

The program, seen here, uses sequences of for loops to fade the three colors (red, green and blue, each connected to a PWM-enabled output) in and out, giving the appearance of “mixed” colors.

int redPin = 9; // Red cathode connected to digital pin 9
int greenPin = 10; // Green cathode connected to digital pin 10
int bluePin = 11; // Blue cathode connected to digital pen 11

void setup() {
 //nothing to see here, but gotta have this block of code anyway
 //that's just the way it is

}

void loop() {
int n = 0; 
for(int k = 0; k < 255; k++)
{
 n = 255-k;
 analogWrite(greenPin, k);
 analogWrite(bluePin, n);
 delay(10);
}

for(int k = 0; k < 255; k++)
{
 n = 255-k;
 analogWrite(greenPin, n);
 analogWrite(bluePin, k);

 delay(10);
}

for(int k = 0; k < 255; k++)
{
 n = 255-k;
 analogWrite(greenPin, k);
 analogWrite(redPin, n);

 delay(10);
}

for(int k = 0; k < 255; k++)
{
 n = 255-k;
 analogWrite(greenPin, n);
 analogWrite(redPin, k);

 delay(10);
}

for(int k = 0; k < 255; k++)
{
 n = 255-k;
 analogWrite(bluePin, k);
 analogWrite(redPin, n);

 delay(10);
}

for(int k = 0; k < 255; k++)
{
 n = 255-k;
 analogWrite(bluePin, n);
 analogWrite(redPin, k);

 delay(10);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s