Arduino Development for OSX and iOS (2015)
2. Sketch Programming
2.7 Analog I/O
In this scenario we build a Sketch application to control RGB LED color using Arduino Analog output (PWM). RGB LED has 4 pins that you can see it on Figure below.
To understand these pins, you can see the following Figure.
Note:
· Pin 1: Red
· Pin 2: Common pin
· Pin 3: Green
· Pin 4: Blue
Now we can start to build a Sketch application and hardware implementation.
2.7.1 Arduino Analog output (PWM)
Please be careful if you want to work with Arduino PWM. If you have Arduino Mega, you will see PWM label so you obtain PWM pins easily but if you have Arduino Uno, it writes DIGITAL (PWM ~). It means your PWM pins can be found on DIGITAL pins which pin with ~, for instance, ~3,~5,~6,~9, ~10, ~11.
For Arduino Mega 2560, you can see PWM pins on picture below (see red arrow).
For Arduino Uno R3, you can see PWM pins as below.
2.7.2 Controlling RGB LED Color
Firstly we implement RGB LED hardware. The following is a hardware schema.
For our testing, we configure the following PWM pins.
Arduino Mega 2560:
· RGB LED pin 1 (red) is connected to Arduino PWM pin 4
· RGB LED pin 2 is connected to Arduino VCC 5V
· RGB LED pin 3 (green) is connected to Arduino PWM pin 3
· RGB LED pin 4 (blue) is connected to Arduino PWM pin 2
Arduino Uno R3:
· RGB LED pin 1 (red) is connected to Arduino PWM pin 9
· RGB LED pin 2 is connected to Arduino VCC 5V
· RGB LED pin 3 (green) is connected to Arduino PWM pin 10
· RGB LED pin 4 (blue) is connected to Arduino PWM pin 11
Here is a sample implementation with Arduino Uno R3.
2.7.3 Arduino Implementation
Now we implement our RGB LED controller in Arduino. This is for testing. Open Firstly, we define our RGB LED pins. The following is RGB LED pins for Arduino Mega 2560.
int redPin = 4;
int greenPin = 3;
int bluePin = 2;
We can define RGB LED pins for Arduino Uno R3.
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
Now we initialize pins on setup().
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
We define a function, called setColor(). This function aims to write RGB values on PWM pins.
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
Now we control RGB values on RGB LED, for instance, Red, Green, Blue, Yellow, Purple, Aqua.
void loop()
{
setColor(255, 0, 0); // red
Serial.println("red");
delay(1000);
setColor(0, 255, 0); // green
Serial.println("green");
delay(1000);
setColor(0, 0, 255); // blue
Serial.println("blue");
delay(1000);
setColor(255, 255, 0); // yellow
Serial.println("yellow");
delay(1000);
setColor(80, 0, 80); // purple
Serial.println("purple");
delay(1000);
setColor(0, 255, 255); // aqua
Serial.println("aqua");
delay(1000);
}
Save this code, called test_rgb_arduino.ino.
Compile and verify this code. If success, you can upload it to Arduino board.
If success, you can see RGB LED blinking with different colors. Here is a sample output of RGB LED with Arduino Mega 2560.