I have a very strange problem, which I wonder if someone might be able to offer some insight into.
I wrote a program to control the color of an RGB LED depending on the status of a variable. The anodes are connected to digital outputs 3, 5 & 6 on my Uno, the cathode is connected to digital output 9. My code sets 3, 5 & 6 to HIGH, while 9 is set to LOW.
Using analogwrite on these four pins allows me to control the mix of RGB to set a desired colour mix, and control the overall brightness of the LED by writing a value to the cathode on pin 9. (When one particular status is set, I want the LED brightness to fade up and down quite quickly, and using a PWM pin as a sink seemed like a sensible approach.)
This all worked beautifully.
However, today I added a RF receiver to the project, as I want to control the state of the LED remotely, and I'm using the VirtualWire library. Again, this all works beautifully, unless I analogwrite anything but zero to pin 9 (ie. full LED brightness), in which case the Uno crashes instantly. If I stick to writing zero to pin 9, I can mix the RGB values to my heart's content and the project functions perfectly over RF with no issues.
I've scraped through the VW library and can't find any clashes with anything in there which might be trying to use the same pin. The default pins are 10, 11 and 12 - I'm not touching those pins for anything other than the RF module.
ArduinJoe:
GoForSmoke - I think you could be right. The VW library does do all sorts of weird things with timers, I had no idea these used specific PWM pins.
The RF transmitters and receivers don't require PWM pins so I'll try moving these and drop the cathode onto one of those.
Thanks for your help.
I'm just guessing. Different PWM pins are tied to different timers as far as I remember so check!.
You'll run out of timers before you run out of PWM pins.
You might try soft-PWM where your code uses millis() to turn the pin HIGH and LOW without a dedicated timer but for all the cycles that would use it's probably better to just change the RGB values in a small fraction of a millisecond.
Using analogwrite on these four pins allows me to control the mix of RGB to set a desired colour mix, and control the overall brightness of the LED by writing a value to the cathode on pin 9. (When one particular status is set, I want the LED brightness to fade up and down quite quickly, and using a PWM pin as a sink seemed like a sensible approach.)
Are there any resistors in those LEDs.
This all worked beautifully.
No it didn't did you look at the signals on a scope.
You are using two lots of PWM and they are going to beat causing intermittent operation. While it might sound a good idea it is not.
I'm using the VirtualWire library.
Does this by any chance use one of the timers used by the PWM on pin 9?
Yes, I have a 220R between each of output pins and the anodes. I wanted three so I can change them around to improve white balance when RGB values are 255, 255, 255.
No it didn't did you look at the signals on a scope.
You are using two lots of PWM and they are going to beat causing intermittent operation. While it might sound a good idea it is not.
I used my eyes, which function pretty well! The LED was doing exactly what I wanted it to do, and it looked pretty sweet. I have left it running and switching between different colour modes, flashing, fading for many hours with no issue. What would a scope tell me that my eyes don't?
Does this by any chance use one of the timers used by the PWM on pin 9?
I believe it does use timer1, which defaults to pin 9.
No you have a lot to learn. Can you eyes spot ringing? Can they spot jagged rise times. Your eyes have 30HZ filter on them they will not see anything faster.
I assume you are of the school of if it looks fine it is fine. Well electronics dosn't work like that. A scope can see problems that you will miss and head them off at the pass. Have you tested all combinations of of duty cycle?
Well you haven't because there are over 65,000 of them.
timer0 - 1024us period - millis(), delay(), micros() and PWM pins 5 & 6
timer1 - 2040us period - PWM pins 9 & 10,
timer2 - 2040us period - PWM pins 11 & 3
Using timer1 and timer2 on the same LED will lead to dependencies
on the initial phase of the timers since they are clocked in unison. Bad plan,
you don't need to PWM both ends of an LED, and driving 3 LEDs from one
pin sounds like you may well be overloading it (40mA absolute max per pin)
No you have a lot to learn. Can you eyes spot ringing? Can they spot jagged rise times. Your eyes have 30HZ filter on them they will not see anything faster.
I assume you are of the school of if it looks fine it is fine. Well electronics dosn't work like that. A scope can see problems that you will miss and head them off at the pass. Have you tested all combinations of of duty cycle?
Well you haven't because there are over 65,000 of them.
Chuckle
If there's one thing I've learned in life, it's that we all have much to learn.
I'm more of the school of Move Fast And Break Things. I'll be dead long before I've investigated 65,000 combinations of duty cycle, so I'll carry on making lights flash in a colourful way. If I fry a few two-dollar microchips along the way, so be it!
MarkT:
Bad plan,
you don't need to PWM both ends of an LED, and driving 3 LEDs from one
pin sounds like you may well be overloading it (40mA absolute max per pin)
At a risk of contradicting myself on the fried chips, this sounds like advice worth heeding - I'll test the current being sunk into the pin and rethink if you're correct. There's a roundabout way to do the same thing with more complex code.
Again, thanks for your practical help folks - it's much appreciated.
I'll be dead long before I've investigated 65,000 combinations of duty cycle, so I'll carry on making lights flash in a colourful way.
That is my point, a scope would have told you this, your eyes would have not.
If you are not willing to take advice then why ask for it in the first place?