RGB LED on Analog pins A0, A1, A2

Hi
I would like to use three RGB LEDs in a project. The idea is to have the RGB LEDs cycle though different colors and each of the LED to cycle through the colors at a different rate so that each one is on with a different color at any given moment.

The RGB LEDs are the common cathode type. I have connected the first two LEDs to PWM pins 3, 5, 6 and PWN pins 9, 10, 11. They work fine and give me all the in-between colors as I am expecting.

I connected the other LED to analog pins A0, A1, A2. However when I run the same sequence of colors on this LED it does not produce the in-between colors. It only seems to work correctly for the RGB value combinations of 0 or 255. For example it produces the correct color for (255, 255, 0), but not for (255, 127, 0).

Why does it not work correctly for the analog pins? It looks like it acts as digital pins by if the value is below 127 it becomes 0 (LOW) and if the value is above 127 it becomes 255 (HIGH). For this forum post I have used the "Adafruit Arduino - Lesson 3. RGB LED" code:

/*
Adafruit Arduino - Lesson 3. RGB LED
*/
//int redPin = 11;
//int greenPin = 10;
//int bluePin = 9;
int redPin = A0;
int greenPin = A1;
int bluePin = A2;

//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE

int delayTime = 2000;



void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}



void loop()
{
  setColor(255, 0, 0); // red       //Works for Analog pins
  delay(delayTime);
  setColor(0, 255, 0); // green     //Works for Analog pins
  delay(delayTime);
  setColor(0, 0, 255); // blue      //Works for Analog pins
  delay(delayTime);
  setColor(255, 255, 0); // yellow  //Works for Analog pins
  delay(delayTime);
  setColor(80, 0, 80); // purple    //Does not work for analog pins. LED off
  delay(delayTime);
  setColor(0, 255, 255); // aqua    //Works for Analog pins
  delay(delayTime);
  setColor(255, 127, 0); // orange  //Does not work for analog pins. Displays red
  delay(delayTime);
  setColor(255, 255, 255); // white      
  delay(delayTime);
  setColor(255, 0, 255); //         //Works for Analog pins
  delay(delayTime);
  setColor(255, 0, 100); //         //Does not work for analog pins. Displays red
  delay(delayTime);
}



void setColor(int red, int green, int blue)
{
  #ifdef COMMON_ANODE
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
  #endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}
1 Like

Why does it not work correctly for the analog pins?

Because the analogue pins are analogue input only. They can be used as digital outputs or digital inputs but not analogue outputs.

The so called analog write should be called PWM write and on a Uno there are only 6 pins capable of producing PWM.

Thank you, Grumpy_Mike.
I shall have to make another plan then. Can this be accomplished by multiplexing the PWM pins?

Can this be accomplished by multiplexing the PWM pins?

No.
You can't multiplex PWM pins, it would not work because you would need to synchronise the multiplexing switching to the PWM signals.
Multiplexing as we know it here is more properly known a time division multiplexing, when a multiplexed pin is not having "its turn" at PWMing it will be off and therefore not PWMing.

You could try and look for a software PWM program libiary if you like.

Thanks Grumpy_Mike

I have done some searching and it seems like people are doing the following things to add more components to PWM. If valid which would be the most appropriate for what I would like to achieve?

  1. Connecting a second ATmega328P to the Arduino.
  2. Using a TLC5940 (16-Channel LED Driver w/EEprom DOT Correction & Grayscale PWM Control)
  3. Using a 74HC595 Shift Register

I am very much a novice with electronics and have more experience in normal programming.

Thanks.

OK
Well do not do 1)
Doing 2) is fine
Doing 3) is only the on / off control you have at the moment unless you use the shift pwm library

There is this:- Arduino Software PWM with millis() - Bald Engineer

And there is this:- http://forum.arduino.cc/index.php?topic=19451.0

You missed an important option from your list; the category of LEDs known loosely as "neopixels". They all have a chip built into the led. This would be the simplest way to achieve your goal.