Problems with trinket PWM on pin 4

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
andygrove
 
Posts: 10
Joined: Wed Oct 03, 2012 11:50 pm

Problems with trinket PWM on pin 4

Post by andygrove »

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;
  }
}

User avatar
Franklin97355
 
Posts: 23903
Joined: Mon Apr 21, 2008 2:33 pm

Re: Problems with trinket PWM on pin 4

Post by Franklin97355 »

You really should post Trinket questions in the Trinket forum, that's what it was created for.

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: Problems with trinket PWM on pin 4

Post by adafruit_support_mike »

Moved.

Actually we have another thread about exactly the same issue currently running: http://forums.adafruit.com/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.

User avatar
mrburnette
 
Posts: 50
Joined: Tue Sep 10, 2013 9:15 pm

Re: Problems with trinket PWM on pin 4

Post by mrburnette »

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

anayar
 
Posts: 2
Joined: Fri Jun 13, 2014 10:50 pm

Re: Problems with trinket PWM on pin 4

Post by anayar »

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.

User avatar
adafruit2
 
Posts: 22111
Joined: Fri Mar 11, 2005 7:36 pm

Re: Problems with trinket PWM on pin 4

Post by adafruit2 »


anayar
 
Posts: 2
Joined: Fri Jun 13, 2014 10:50 pm

Re: Problems with trinket PWM on pin 4

Post by anayar »

Great success! Thank you!

User avatar
hiduino
 
Posts: 862
Joined: Sat Sep 01, 2012 7:05 pm

Re: Problems with trinket PWM on pin 4

Post by hiduino »

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

#define TCCR1A GTCCR
#define WGM10  PWM1B
Update the line to include TIMER1B for pin 4:

Code: Select all

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.)

User avatar
adafruit2
 
Posts: 22111
Joined: Fri Mar 11, 2005 7:36 pm

Re: Problems with trinket PWM on pin 4

Post by adafruit2 »

oooo thank you! that's a good idea, we'll check it out!

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Problems with trinket PWM on pin 4

Post by tdicola »

Gave it a quick test and it works great, thanks for suggesting the change!

User avatar
adafruit2
 
Posts: 22111
Joined: Fri Mar 11, 2005 7:36 pm

Re: Problems with trinket PWM on pin 4

Post by adafruit2 »

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
hiduino
 
Posts: 862
Joined: Sat Sep 01, 2012 7:05 pm

Re: Problems with trinket PWM on pin 4

Post by hiduino »

Just to note, the Introducing Trinket tutorial still says pin#4 is not supported for PWM in the programming-with-arduino-ide section.

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Problems with trinket PWM on pin 4

Post by tdicola »

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
ja450n
 
Posts: 21
Joined: Mon Jun 17, 2013 6:13 pm

Re: Problems with trinket PWM on pin 4

Post by ja450n »

I was working on a project using PWM on pin 4 to drive a motor and had a common problem of the motor whining and in finding a solution for my problem I may have found a potential bug in the current implementation.


Normally changing the PWM frequency, by doing something like below works:

TCCR0B = TCCR0B & 0b11111101 | 0x01;

The problem is Pin4 doesn't seem to be tied to the available options (TCCR0B, TCCR0A, TCCR1A)

I found that TCCR1B works, but you have to change the pins_arduino.h file to define this.

User avatar
hiduino
 
Posts: 862
Joined: Sat Sep 01, 2012 7:05 pm

Re: Problems with trinket PWM on pin 4

Post by hiduino »

PWM #4 is using timer1 counter. It uses register TCCR1 (not TCCR1B) for adjusting prescaler frequency. You don't need to define it. It's already defined at part of the ATtiny85 support.

You can use it like:

Code: Select all

void setup()  { 
  pinMode(4, OUTPUT);
  TCCR1 = TCCR1 & 0b11110000 | ((0<<CS13) | (1<<CS12) | (1<<CS11) | (0<<CS10));
} 
Adjust the bits as needed for the prescale clock you want.

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”