The new Raspberry Pi B Model B+ is here - Pick one up in the store and check our detailed guide!

Problems with trinket PWM on pin 4

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


I've created a simple RGB LED "blinky" project using an Adafruit Arduino Trinket (the 5V version) and it is working pretty well ( 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 ( 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 | TOGGLE FULL SIZE
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;
  for (int i=255; i>=0; i-=5) {
    rgb[index] = i;
  if (++index == 3) {
    index = 0;
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
Posts: 3819
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


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
Posts: 8581
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


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:
With Digispark 16MHz core:
avrdude -c usbtiny -p attiny85 -U flash:w:sketch_sep25a.cpp.hex
Results: PWM on 0, 1, 4 verified

Source: ...
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
Posts: 49
Joined: Tue Sep 10, 2013 8:15 pm