Problems with trinket PWM on pin 4
Moderators: adafruit_support_bill, adafruit

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 | 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;
    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: 9
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: 4805
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: 9477
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

Re: Problems with trinket PWM on pin 4

by anayar on Thu Sep 04, 2014 12:57 pm

Hello,

Has there been any update or resolution to this problem? I purchased a Trinket for the sole purpose of making small RGB night light as a gift for someone and it took me forever to realize that it pin 4 was the problem, not my code or wiring. Even if there won't be a solution, I feel like you should make this issue more prominent on the product page rather than buried in a random tutorial.
anayar
 
Posts: 2
Joined: Fri Jun 13, 2014 9:50 pm

Re: Problems with trinket PWM on pin 4

by adafruit2 on Fri Sep 05, 2014 3:52 pm

User avatar
adafruit2
Site Admin
 
Posts: 7858
Joined: Fri Mar 11, 2005 6:36 pm

Re: Problems with trinket PWM on pin 4

by anayar on Fri Sep 05, 2014 6:27 pm

Great success! Thank you!
anayar
 
Posts: 2
Joined: Fri Jun 13, 2014 9:50 pm

Re: Problems with trinket PWM on pin 4

by hiduino on Thu Sep 11, 2014 3:52 am

As an alternative to defining custom PWM4_init() and analogWrite4() functions you could just modify the \variants\tiny8\pins_arduino.h file.

Add the following to the pins_arduino.h file:
Code: Select all | TOGGLE FULL SIZE
#define TCCR1A GTCCR
#define WGM10  PWM1B

Update the line to include TIMER1B for pin 4:
Code: Select all | TOGGLE FULL SIZE
const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {
   TIMER0A, /* OC0A */
   TIMER0B,
   NOT_ON_TIMER,
   NOT_ON_TIMER,
   TIMER1B,
   NOT_ON_TIMER,
};

This will allow the Arduino standard core to work with the tiny85 and basically do the same thing as the custom functions but using the standard analogWrite() function call. (Adafruit you should consider making this change in your Trinket variants distribution instead of having users add the custom functions to their sketches. This will help simplify sketches to using standard calls.)
hiduino
 
Posts: 93
Joined: Sat Sep 01, 2012 6:05 pm

Re: Problems with trinket PWM on pin 4

by adafruit2 on Thu Sep 11, 2014 4:13 pm

oooo thank you! that's a good idea, we'll check it out!
User avatar
adafruit2
Site Admin
 
Posts: 7858
Joined: Fri Mar 11, 2005 6:36 pm

Re: Problems with trinket PWM on pin 4

by tdicola on Thu Sep 11, 2014 10:00 pm

Gave it a quick test and it works great, thanks for suggesting the change!
User avatar
tdicola
 
Posts: 377
Joined: Thu Oct 17, 2013 8:11 pm

Re: Problems with trinket PWM on pin 4

by adafruit2 on Mon Sep 15, 2014 11:26 am

Yay! thanks for the fix, we updated all the pre-install and hardwaresupport files. so going forward it should automagically work. you can also of course use the older style too. whew!
User avatar
adafruit2
Site Admin
 
Posts: 7858
Joined: Fri Mar 11, 2005 6:36 pm

Re: Problems with trinket PWM on pin 4

by hiduino on Thu Sep 18, 2014 4:10 pm

Just to note, the Introducing Trinket tutorial still says pin#4 is not supported for PWM in the programming-with-arduino-ide section.
hiduino
 
Posts: 93
Joined: Sat Sep 01, 2012 6:05 pm

Re: Problems with trinket PWM on pin 4

by tdicola on Fri Sep 19, 2014 2:26 pm

Oh thanks for catching that--just updated the page to mention that the latest IDE has support for analogWrite and pin #4. Thanks!
User avatar
tdicola
 
Posts: 377
Joined: Thu Oct 17, 2013 8:11 pm