In this lesson, we will be combining what we learned about pushbuttons and LEDs to make a Red-Green Blue (RGB) LED produce different amounts of each color to produce different shades of other colors.
Parts:
∙ 3 Resistors
-1 RGB LED Module
∙ 3 pushbuttons
∙ 1 Arduino
∙ Assorted jumper wires
Wiring:
Code:
// define the pins that will output power to the LEDs
int redLEDPin = 9;
int greenLEDPin = 10;
int blueLEDPin = 11;
//define the pins that will receive input from the pushbuttons
int redSwitchPin = 7;
int greenSwitchPin = 6;
int blueSwitchPin = 5;
// preset the values of PWM for the red, blue and green LEDs
int red = 0;
int blue = 0;
int green = 0;
// the setup initializes Arduino’s pins
void setup()
{
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
pinMode(redSwitchPin, INPUT_PULLUP);
pinMode(greenSwitchPin, INPUT_PULLUP);
pinMode(blueSwitchPin, INPUT_PULLUP);
}
/* this loop checks to see which, if any, buttons are pushed. When a button is pushed, the PWM value increases for the color LED associated with the button
*/
void loop()
{
if (digitalRead(redSwitchPin) == LOW)
{
red ++;
if (red > 255) red = 0;
}
if (digitalRead(greenSwitchPin) == LOW)
{
green ++;
if (green > 255) green = 0;
}
if (digitalRead(blueSwitchPin) == LOW)
{
blue ++;
if (blue > 255) blue = 0;
}
//after checking for any increases in PWM, this code outputs the PWM signal to each of the pins
analogWrite(redLEDPin, red);
analogWrite(greenLEDPin, green);
analogWrite(blueLEDPin, blue);
delay(10);
}
The Theory of Pulse-Width-Modulation (PWM) –By: Simon Monk
Pulse Width Modulation (or PWM) is a technique for controlling power. We also use it here to control the brightness of each of the LEDs.
The diagram below shows the signal from one of the PWM pins on the Arduino when we use the analogWrite(pin) command.
PWM -12 – about 5%
PWM -128 – about 50%
PWM -230 – about 90%
Roughly every 1/500 of a second, the PWM output will produce a pulse. The length of this pulse is controlled by the 'analogWrite' function. So 'analogWrite(0)' will not produce any pulse at all and 'analogWrite(255)' will produce a pulse that lasts all the way until the next pulse is due, so that the output is actually on all the time.
If we specify a value in the analogWrite that is somewhere in between 0 and 255 then we will produce a pulse. If the output pulse is only high for 5% of the time then whatever we are driving will only receive 5% of full power.
If however the output is at 5V for 90% of the time then the load will get 90% of the power delivered to it. We cannot see the LEDs turning on and off at that speed, so to us, it just looks like the brightness is changing.
Extras:
Try this code to ditch the pushbuttons and simply automate the color output on your own…
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0); // yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255); // aqua
delay(1000);
}
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
Try combining different values of R, G, and B in the setColor(); function to make your own colors!