0% found this document useful (0 votes)
7 views

Arduino and Genuino 101 Development Workshop - Agus Kurniawan Part 008

Arduino and Genuino 101 Development Workshop - Agus Kurniawan

Uploaded by

SANTOSH KHANAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arduino and Genuino 101 Development Workshop - Agus Kurniawan Part 008

Arduino and Genuino 101 Development Workshop - Agus Kurniawan

Uploaded by

SANTOSH KHANAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2.8.

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.

RGB LED pin 1 (red) is connected to Arduino PWM pin 9


RGB LED pin 2 is connected to Arduino VCC +3.3V or +5V (recommended +3.3V)
RGB LED pin 3 (green) is connected to Arduino PWM pin 6
RGB LED pin 4 (blue) is connected to Arduino PWM pin 5

Here is a sample implementation with Arduino/Genuino 101.


2.8.3 Arduino Implementation
Now we implement our RGB LED controller in Arduino. We can define RGB LED pins
for Arduino/Genuino 101.
int redPin = 9;
int greenPin = 6;
int bluePin = 5;

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 Genuino 101.
You also see output on Serial Monitor.

You might also like