PWM issues

Shafto

Senior Member
I'm using PWM to dim some LEDs, but I'm having a problem with flicker caused by the ADC input controlling the duty cycle. Here's how I came to this conclusion..

I'm using PWM output 2, and an 8% duty cycle, if I use this command:

pwmout 2,99,32 I get good results, but when I debug b2, the variable for the 10k input pot controlling the PWM duty cyle.. and set it to 32, and then use this command:

pwmout 2,99,b2 I get horrible flickering, it gets worse as the period gets shorter with the same duty cyle..

I'm not really sure what this is? some kind of EMI on the input? Should I try a 0.01uF on the input? That's just a guess.. I don't really have any idea what to do about this.
 

bgrabowski

Senior Member
If you are using the Basic command Debug in your code try omitting it. If not, please explain what you mean by debug B2.
 

Shafto

Senior Member
I want to have the PWM duty cycle adjustable, that's why it's hooked up to the pot, but if I use the number 32 in the command for PWM like I posted above everything is fine.

If I debug my input pot and set it to 32, so it should be the same as just writing "32" then I should get the exact same results... but I don't, the LEDs noticeably flicker on and off when using b2 in place of an actual value, even if b2 points to the same value.

I'm not using the debug command in my program for PWM... I think that's what you may have thought I was doing?
 

hippy

Ex-Staff (retired)
What you seem to be saying is that this works -

- PwmOut 2, 99, 32

But this doesn't -

- ReadAdc ADC_PIN, b2
- PwmOut 2, 99, b2

The most obvious reasoning would be that b2 doesn't contain 32 even though the tests you've done appear to indicate it does. You can confirm if it's the PWMOUT command causing problems ( it shouldn't be ) by using -

- ReadAdc ADC_PIN, b2
- b2 = 32
- PwmOut 2, 99, b2

There's no logical reason why your READADC and PWMOUT shouldn't work together, so perhaps you have a wiring error of some sort ?

The only other thought I have is that you are resetting the PWM by executing the PWMOUT command too quickly. You could try -

- ReadAdc ADC_PIN, b2
- PwmOut 2, 99, b2
- Pause 200

and see if that improves things.
 

Shafto

Senior Member
Yep.. when I use the variable provided by the input pot, even though it's the same, I get a lot of flicker. I'm sure the number I see the the debug is correct, the brightness is the same.. I just get a lot of flicker in LEDs... It's much more noticeable at higher frequency.. at 10,000hz at 8% it's not bad using b2, at 15,500hz 8% it's bad.. and worse from there... and the lower the duty cycle, worse it gets... though sometimes it will get better, and then worse again as I adjust lower.. depends on the period.

The pause command has no effect on the problem.. I tried using b2=32 and it's fine that way (which leads me to believe the wiring is fine) it's only when I'm using the ADC input value that I get this flickering problem.. even though it points to the same value... some kind of interference of some kind? the adc value doesn't bounce around in the debug.. I know it references the source voltage, but that should be steady enough, I'm using a computer power supply.. and there's a 0.01uF across the +V and ground of the picaxe. Should I try one from the ADC input to ground? or is that something stupid that will wreck my picaxe?

I'm still very new and haven't delved into many commands yet, but if this problem can't be solved, is there some other way to store this variable in memory so instead of being read on the fly, it could just be read at startup and then be constant? then I suppose I'd need to use another input so when it's high I could see the adjustment of b2, and then once low again it would just store it away as a constant so I wouldn't get this flickering nonsense.. I dunno.. I'm not sure if that's possible but I need to find a way to solve this.
 
Last edited:

Shafto

Senior Member
Upon trying a longer pause 500+ the flicker starts to go away, I suppose you were right hippy.. but what can I do about this? I think the best way would be to store the variable different so it's not reading from b2 all the time if I can.. otherwise there's too much of a delay for the signal lights to come on.. unless I made them interrupts I suppose.
 
Last edited:

Technical

Technical Support
Staff member
Your problem is that you are restarting pwmout all the time unnecessarily (ie every time you issue a pwmout command).

You only need to update pwmout when the adc value changes.

so instead of

Code:
main:
ReadAdc ADC_PIN, b2
PwmOut 2, 99, b2
goto main
use

Code:
main:
ReadAdc ADC_PIN, b2
if b2 <> b3 then
PwmOut 2, 99, b2
b3 = b2
end if
goto main
 

Shafto

Senior Member
Seems like it should work, but no improvement in the problem.. the flickering is slightly different, but still very noticeable.
 

BeanieBots

Moderator
That does strongly suggests that your input is not stable despite what was observed with debug.
What supplies that voltage?
Your idea of adding a cap on the ADC input might work but the value depends on what the source is.
 

Shafto

Senior Member
Yeah that's what I'm figuring.. it must be bouncing around somehow.. I'm using a computer power supply, I assume it's fairly stable, but I have no way to be sure... I will rig up the 5V reg I bought for it off the 12V line instead of just using the 5V line and see if that helps things..
 

BeanieBots

Moderator
You've still not said what produces the voltage that feeds the ADC input.
Does it meet the PIC input impedance criteria?

A PC power supply is NOT always particularly stable. That is, it's a switching supply and likely to have significant ripple. Also, if not correctly loaded, it could wander off all over the place. Never a good choice for a simple low current project. Your idea of a regulator (with all the required caps) from the 12v line would be much better. And safer!
 

Shafto

Senior Member
The computer power supply is the supply for all of the power.. including the ADC inputs.

I have the 5V line loaded properly so it's stable in that sense.. it never moves from 5.05 on the DMM, but that doesn't say much I suppose.

I'll rig up the regulator here shortly.
 

Shafto

Senior Member
Alright, got my SMT cap soldered right onto the leads of the reg, they said put it as close as possible, so I did.. hah.. though my main drive for that was the fact that the SMT version of the 47uF industrial grade low ESR tantalum caps were far cheaper than any suitable axial or radial version.. and had better characteristics as well... I also decided to go this route for all of my 9V regs for the LEDs that use a 0.33uF tantalum on the inputs, there's a pin in between though, so I modified the legs on the caps, I just bent the out from underneath and now they span the 1 pin gap... I took a pic, but photobucket is acting up.. I'll post it as soon as it starts working again.

As for the PWM I've been cured.. it was the power supply as suspected, I put together a suppressor very similar to the one suggested by Hippy in another thread seen here: http://www.picaxeforum.co.uk/attachment.php?attachmentid=173&d=1193139252

Thanks for that.. it consists of a 1amp fuse, blocking diode, 500uH ferrite choke, 0.22uF X2 cap (doesn't have 4k7 resistor to ground yet because I just didn't have the room to breadboard it, but it will be in the final version, is a standard 1/2 watt good for this?) then a 00.1uf X2 cap.. no varistor yet either.. because it's not in the car, but I have a heavy duty automotive varistor to use in the final version.

I now can only see some very minor flickering at certain levels, but a very slight adjustment takes care of it, the code I found best worked was what you suggested Technical, thanks.
 
Top