I've created a simple RGB LED "blinky" project using an Adafruit Arduino Trinket (the 5V version) and it is working pretty well (http://www.youtube.com/watch?v=Kz6zIT0sweI) but I haven't been able to get PWM working on pin #4 and could use some help. PWM works fine on pins #0 and #1 though.
I'm using common annode RGB LED from Adafruit also (http://www.adafruit.com/products/302). The annode pin is connected to the trinket's 5V out and the other three pins are connected to digital pins #0, #1, and #4 (each one via a 220 ohm resistor).
PIns #0 and #1 give the expected PWM glowing LED effect but pin #4 is just switching between ON and OFF. I have tried changing which color is connected to pin #4 and the same thing happens so the issue is specific to this pin. I am powering the trinket from a 9V battery when running the code and do not have USB connected (I know pin #4 is shared with USB).
I'd appreciate any pointers on where I am going wrong with this. Thanks in advance.
Here is the sketch that demonstrates the issue:
Code: Select all
int pinR = PB0; // Digital pin #0
int pinG = PB1; // Digital pin #1
int pinB = PB4; // Digital pin #4
void setup() {
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
}
void set(int rgb[]) {
set(rgb[0], rgb[1], rgb[2]);
}
void set(int r, int g, int b) {
// using common annode LED so low means ON!
analogWrite(pinR, 255-r);
analogWrite(pinG, 255-g);
analogWrite(pinB, 255-b);
}
int index = 0;
void loop() {
int rgb[3];
int d = 20;
rgb[0] = 0;
rgb[1] = 0;
rgb[2] = 0;
for (int i=0; i<255; i+=5) {
rgb[index] = i;
set(rgb);
delay(d);
}
for (int i=255; i>=0; i-=5) {
rgb[index] = i;
set(rgb);
delay(d);
}
if (++index == 3) {
index = 0;
}
}