Latest posts

Popular posts

Blog categories

Arduino Lesson 2: RGB LED Control

Posted in: Arduino Courses
December 12th 2016
510 Views
Liked

Welcome Engineers.

Today we will do RGB LED control with Arduino.

Our required materials:

  1. Arduino UNO
  2. Breadboard
  3. 1 common anode or common cathode RGB LED
  4. 3 pieces of 220 Ω resistor
  5. Two male jumper cables

RGB LEDs, unlike normal LEDs, have 3 different colors (red, green and blue) LEDs in one package. The LEDs had anode and cathode ends to remember from our LED flashing course. For RGB LEDs, the anode has common anode or cathode connections according to the production pattern.

Our RGB LED, which we use on the kit, has a common anoda. So we will prepare for the Arduino code will work according to the common anonymous. If we want to use a common cathode LED, we need to make a slight change in our code.

PWM and Digital to Analog Conversion

As we know, the voltage used at the input / output pins of the Arduino is 5V. In the previous lesson we connected our LED to draw 20mA current at 5V voltage. When we connected it in this way, our LED lit up in the brightest possible way. What do we need to do if we want to change the brightness?

The answer is actually simple: lowering the tension. If we operate our LED operating at 5V with a lower voltage, for example 3V, the brightness will decrease. But this time you can ask: Was not the Arduino output voltage 5V? How can we get 3V output?

Here we have to use the pulse width modulation (PWM) technique. In this article, I will briefly refer to PWM instead of pulse width modulation. The PWM turns on and off the 5V voltage we receive on the output at a certain time (1 / 500th of a second in the Arduino), allowing us to get the voltage we want between 0 and 5V through the Arduino output pin. Let's say: if an LED blinks very quickly, our eyes will not fully perceive this flash and perceive the brightness to be lower.

As we can see in the picture above, 5V is 5% of 5V when we give the 2 milliseconds per second to be open only 5% of the time. That is, 0.25V. Likewise, if we set it to be open in half (50%) of 2 ms, we get 2.5V.

Not all pins of our Arduino UNO card have PWM output capability. On the digital pins of our card, there are some pins marked with ~ in front of the pin number. If we want to get PWM output, we have to use these pins. These pins are 3, 5, 6, 9, 10 and 11 for Arduino UNO.

In our first lesson we changed the example code that is available in the Arduino software. This time we write our code ourselves:

  Int redPin = 9;
 Int yesilPin = 10;
 Int bluePin = 11;

 Void setup ()
 {
  PinMode (redPin, OUTPUT);
  PinMode (yesPin, OUTPUT);
  PinMode (bluePin, OUTPUT);
 }

 Void loop ()
 {
  ColorAll (255, 0, 0);  //red
  Delay 1500;
  ColorArea (0, 255, 0);  //green
  Delay 1500;
  ColorAll (0, 0, 255);  //blue
  Delay 1500;
  ColorAll (255, 255, 0);  //yellow
  Delay 1500;
  Colorize (80, 0, 80);  //purple
  Delay 1500; 
  ColorAll (0, 255, 255);  //light blue
  Delay 1500;
  Colorize (255, 255, 255);  //white
  Delay 1500;
 }

 Void colorAll (int red, int green, int blue)

 {
  Red = 255 - red;
  Yes = 255 - yesil;
  Blue = 255 - blue;
  AnalogWrite (redPin, red);
  AnalogWrite (yesPin, yes);
  AnalogWrite (bluePin, blue);
 } 

We connect the red LED of our RGB LED with pine 9, the green pine with pine 10 and the blue pin 11 with pine 220 Ω. Since the LED we use has a common anode structure, we also connect the anode pad to the 5V pin of our card.

We use the code in the setup function to define the pins as output. In addition, the analogueWrite command in the color-coded function we wrote allows us to adjust the voltage level we will get from each PWM output pin. The analogWrite command is used as follows:

  AnalogWrite (PWM output pin number, numeric value between 0-255); 

In the analogWrite command, the value 255 represents the maximum output voltage, ie 5V. All values ​​from 0 to 255 correspond to voltage values ​​from 0 to 5V. For example, the command analogWrite (9, 80) allows us to output at a voltage of 5V x (80/255) = 1,57V from pin 9. It is possible on this occasion that we do not get the color light we want by mixing the red, blue and green light of different brightness.

If the LED we are using has a common cathode structure instead of a common anode, we have to connect the GND pins instead of the common sink + 5V,

  Red = 255 - red;
 Yes = 255 - yesil;
 Blue = 255 - blue; 

We need to delete the part.

In the loop function, our card makes it possible to write the values ​​we want to the outputs by repeatedly calling the color code we create. The delay function allows us to wait between each command without knowing what to do with our card. By changing the value of this function, we can switch between colors at the desired speed. You can get different colors by playing with different brightness values.

EVA Electronics Co. Wish You All The Success

Leave a comment

Log in to post comments

TOP