Problems with trinket PWM on pin 4

by andygrove on Tue Sep 24, 2013 10:22 pm

Hi,

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;
  }
}
andygrove
 
Posts: 5
Joined: Wed Oct 03, 2012 10:50 pm

Re: Problems with trinket PWM on pin 4

by franklin97355 on Tue Sep 24, 2013 11:37 pm

You really should post Trinket questions in the Trinket forum, that's what it was created for.
User avatar
franklin97355
 
Posts: 2662
Joined: Mon Apr 21, 2008 1:33 pm

Re: Problems with trinket PWM on pin 4

by adafruit_support_mike on Wed Sep 25, 2013 12:51 am

Moved.

Actually we have another thread about exactly the same issue currently running: viewtopic.php?f=52&t=43572

The short version is that this is a known glitch. The hardware supports PWM on pin4 but we're not sure if the Arduino core has the necessary code. It's on our "things to look at" list.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 6983
Joined: Thu Feb 11, 2010 1:51 pm

Re: Problems with trinket PWM on pin 4

by mrburnette on Wed Sep 25, 2013 11:25 am

EDITED:

I use several tiny cores and I compiled the Op's code under the following two cores as noted and then used my scope to double-check the PWM on the three pins. As shown, both compiles when uploaded to the Trinket were successful.
Screen cut&paste of actual command line in bold:
Source: DigisparkArduino-Win32-1.0.4-May19.zip
With Digispark 16MHz core:
avrdude -c usbtiny -p attiny85 -U flash:w:sketch_sep25a.cpp.hex
Results: PWM on 0, 1, 4 verified


Source: https://code.google.com/p/arduino-tiny/ ... 0-0015.zip
ATtiny85 @ 16 MHz (internal PLL; 4.3 V BOD):
avrdude -c usbtiny -p attiny85 -U flash:w:sketch_sep25a.cpp.hex
Results: PWM on 0, 1, 4 verified
mrburnette
 
Posts: 49
Joined: Tue Sep 10, 2013 8:15 pm