Arduino Tutorial #2 – Analog Inputs and Pulse Width Modulation (PWM) RGB LED

//Arduino Tutorial #2 – Analog Inputs and Pulse Width Modulation (PWM) RGB LED

Arduino Tutorial #2 – Analog Inputs and Pulse Width Modulation (PWM) RGB LED

In the second tutorial, I discuss the important concept of Analog inputs and outputs.

The first example takes us into the exploration of a simple temperature sensor which provides an analog output based on the ambient temperature. The code for this example is rather simple and you can follow along by using the code below. In addition to the analog read, I am also using a serial communication through the Tx pin which transmits any data from the microcontroller to the computer. This is extremely useful if you are debugging your project, looking to implement a function with the computer or simply want to see what is going on with your sensor in my case.
void setup()

void loop()
int sensor = analogRead(A0);
float voltage = sensor * 5.0 / 1023.0;
float temp = voltage *100; // 1C per 10mV

In the second example I am using an RGB LED which I am driving with a Pulse Width Modulated signal, or simply PWM. In this example, I am able to shift the LED through all the colors one by one. The code is slightly more complicated for this example, so if you have any questions, don’t hesitate!
int led_blue = 9;
int led_green = 10;
int led_red = 11;
int brightness = 0;

int fadeLED = 5;
int curr_pin = 9;

void setup() {
pinMode(led_blue, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(led_red, OUTPUT);

void loop() {
analogWrite(curr_pin, brightness);
brightness = brightness + fadeLED;

if (brightness == 0) {
fadeLED = -fadeLED ;
if ( brightness == 255 )
fadeLED = -fadeLED ;
if(curr_pin == 11)
curr_pin = 9;

I hope that you enjoyed the second Arduino tutorial and make sure to stay tuned for the Extended version in which I plan to go over the ADC and PWM concepts used in this tutorial.

Thank you for watching/reading and make sure to leave a comment!

By | 2014-02-10T01:25:04-07:00 February 10th, 2014|My Projects|0 Comments

About the Author: