PWM Issue with 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.
Locked
jkarve
 
Posts: 2
Joined: Sat Sep 14, 2013 2:11 pm

PWM Issue with PIN 4

Post by jkarve »

Hi, I was wondering if I'm doing anything wrong when trying to use PWM with pin #4.

In the Pinouts section of the tutorial, it states that pin #4 can be used for PWM, and I'd like this to be the case so I can utilize all 3 PWM pins.

I'm using the following code, ripped from the Trinket tutorial.

Code: Select all

int led = 4; 

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}
 
// the loop routine runs over and over again forever:
void loop() {
  for (int i=0; i<256; i++) {
    analogWrite(led, i); // PWM the LED from 0 to 255 (max)
    delay(5);
  }
  for (int i=255; i>=0; i--) {
    analogWrite(led, i); // PWM the LED from 255 (max) to 0
    delay(5);
  }
}
This works perfectly with pins #0 and #1, but not #4. With pin #4 I just get a blink.

I've tried leaving the LED disconnected until after uploading, and only using an external power supply to try and get it working, but I'm having no luck so far.

Am I doing something wrong, or does something else need to be tweaked here? Thanks!

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm

Re: PWM Issue with PIN 4

Post by adafruit »

The pin itself does support it but the arduino core may not yet support pin 4 PWM. We'll add it to our list to check out!

jkarve
 
Posts: 2
Joined: Sat Sep 14, 2013 2:11 pm

Re: PWM Issue with PIN 4

Post by jkarve »

I'm glad to know I wasn't going crazy. I'll keep my eye out for an update!

Thank you!

bananana
 
Posts: 1
Joined: Wed Sep 25, 2013 1:12 am

Re: PWM Issue with PIN 4

Post by bananana »

I was having this problem today too. Any estimate as to when a fix will become available? Thanks!

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

Re: PWM Issue with PIN 4

Post by adafruit_support_mike »

AFAIK, the issue is still on our "find out what's wrong" list, and there are a few critical steps that have to occur before it gets to the "plan a solution", "implement a solution", "test the solution", "check with the other stakeholders to make sure we haven't broken anything important to them", "document the solution", and "plan an ETA for publication of the solution" lists.

You're welcome to dive into the code and see what you can find in the meantime though: https://github.com/arduino/Arduino/tree/master/core

The more eyes we have looking at the problem, the faster we can push a solution through all the lists.

User avatar
AnneBarela
Learn User Page
 
Posts: 757
Joined: Sat Mar 24, 2012 8:56 pm

Re: PWM Issue with PIN 4

Post by AnneBarela »

For those who would like PWM on pin 4 (assuming you are not otherwise using Timer 1 in some code or library, for example generating sounds like in the piezo tutorial), here is an initialization (for setup) and an analogWrite type function (in your loop) for PWM on pin 4:

Code: Select all

void PWM4_init() {
  // Set up PWM on Trinket GPIO #4 (PB4, pin 3) using Timer 1
  TCCR1 = _BV (CS10);           // no prescaler
  GTCCR = _BV (COM1B1) | _BV (PWM1B);  //  clear OC1B on compare
  OCR1B = 127;                  // duty cycle initialize to 50% (half of OCR1C)
  OCR1C = 255;                  // frequency
}

// Function to allow analogWrite on Trinket GPIO #4 
void analogWrite4(uint8_t duty_value) {  
  OCR1B = duty_value;  // duty may be 0 to 255 (0 to 100%)
}
The analogWrite4 function changes the duty cycle just like usual analogWrite. If you need different frequencies, you can change OCR1C.

You can see this in action soon but I wanted to get the code out since it has been requested. This is not the official Adafruit "fix", just my attempt to get the functionality so this is user supported code. I hope it helps you all.

User avatar
cashen
 
Posts: 6
Joined: Thu Mar 20, 2014 10:41 am

Re: PWM Issue with PIN 4

Post by cashen »

I'm a little bit ... "confused." I bought the Trinket for a simple RGB LED "thingy" to make. And as it's known, there are only two PWM pins that "naturally" work on the Trinket, with one that "can" work, but ...

I bought two of the Trinkets in 2014, just a couple weeks ago. And I figured it'd be simple to get Pin 4 to do PWM. Yet, doesn't seem so simple. Another person gave some code and functions to fix that, but I'm having trouble using them, doesn't like that "duty_val is not declared." Ok. That tells me, uh, NOTHING. But then I'm relatively new to Arduino, but I've been a professional eng for three decades so it's not like I can't figure it out, but my point is, WH do I, and others, have to "figure it out?" The last post on this issue from adafruit was in 2010. Said they were working hard on it, or some-such.

You can do the math on that. It's 2014. Four years? I must be missing something simple, and that'd be great because it SHOULD be simple when you sell a product that customers are hoping to use.

There's gotta be a simple workaround, and even more practical, there, by now, has GOT to be NO NEED for a workaround.

Adafruit? What's going on? Either I'm just blind, or this product just isn't what I expected. Any help would be appreciated. I really thought this would be simple. Heck, it's just an Arduino-like thing, and they're simple.

User avatar
AnneBarela
Learn User Page
 
Posts: 757
Joined: Sat Mar 24, 2012 8:56 pm

Re: PWM Issue with PIN 4

Post by AnneBarela »

Welcome to the Arduino world, where not everything is perfect but the users strive to make it better.

It sounds like you should have duty_value, not duty_val, that is probably a typo on your part - the error says you are trying to use a variable without declaring what kind of variable it is as required by c. I suggest you take advantage of the intro to Arduino tutorials including on learn.adafruit.com to sharpen the programming skills. And skill building is what it is all about - we all strive as Makers to learn and do better.

The functions provided are not a workaround. Products are listed as "capable" which does not necessarily mean there is code waiting to use such functions. You have to crack the lid every now and then.

User avatar
cashen
 
Posts: 6
Joined: Thu Mar 20, 2014 10:41 am

Re: PWM Issue with PIN 4

Post by cashen »

Thanks for your response. Actually, the typo was when I posted here, I copied and pasted the code you supplied here and that was where the error came from. I saw no reason for the error however.

But, good (albeit "odd") news! I've been hacking away at my code in some other ways, including trying other methods to get pin to work in PWM mode, without luck. However, I decided to try your code again and see if I could hack it to get it working but ... didn't need to, it just WORKS now! (Therefore the "odd" part, it should've had this same problem.) So thank you! It works just great!

And yeah, I know the Arduino isn't exactly and "enterprise class" CPU (Which actually was my specialty) so this is pretty much my first foray in small boards like this. So I admit I find this environment alien to me in many ways (For example, I have decades of experience in "C" and "C++", among other languages, so the Ardunio's use of their "C-like" tools is taking some time to get used to because.) So sorry if my "newness" shows!

So, thanks to you I can continue with my little project here, I really appreciated finding your sketch snippet! Did the trick! :)

User avatar
monkeym
 
Posts: 4
Joined: Sun Dec 22, 2013 8:39 am

Re: PWM Issue with PIN 4

Post by monkeym »

I've been wrestling with this one all day with the only conclusion I could come to being that the "for" statement wouldn't work with "analogWrite4" but it has started working all of a sudden (not quite sure what I have done differently this time!). For others who have this issue in the future, here is the code that finally worked to fade an LED up (in 7 seconds @8MHz)).

Thanks to the posters here and elsewhere for the help.

Code: Select all

int outLed = 4;       // using LED on pin 4 to PWM
int  G;

void setup (){
  PWM4_init();
  pinMode(outLed, OUTPUT);    //LED as output
}

void loop() {
for (int G=0; G < 255; G++) {
  analogWrite4(G);
  delay(30);
}
G=0;
delay(50);
}

void PWM4_init() {
  // Set up PWM on Trinket GPIO #4 (PB4, pin 3) using Timer 1
  TCCR1 = _BV (CS10);           // no prescaler
  GTCCR = _BV (COM1B1) | _BV (PWM1B);  //  clear OC1B on compare
  OCR1B = 0;                  // duty cycle initialize to 50%
  OCR1C = 255;                  // frequency
}

// Function to allow analogWrite on Trinket GPIO #4 
void analogWrite4(uint8_t duty_value) {  
  OCR1B = duty_value;  // duty may be 0 to 255 (0 to 100%)
}

User avatar
AnneBarela
Learn User Page
 
Posts: 757
Joined: Sat Mar 24, 2012 8:56 pm

Re: PWM Issue with PIN 4

Post by AnneBarela »

It is always good to declare variables with an explicit size. For an "int" it looks like it is 8 bits. If you declare G to be "int16_t" or "uint16_t" then it can be much bigger.

User avatar
jackv
 
Posts: 25
Joined: Sat Dec 22, 2012 3:57 pm

Re: PWM Issue with PIN 4

Post by jackv »

Very nice code Kitty, thank you.

User avatar
AnneBarela
Learn User Page
 
Posts: 757
Joined: Sat Mar 24, 2012 8:56 pm

Re: PWM Issue with PIN 4

Post by AnneBarela »

You're welcome, glad the code is working for your project. Always good to share.

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

Return to “Trinket ATTiny, Trinket M0”