Questions on the PWM Command (not PWMOUT)

Revolvr

New Member
Hi all,

I am using an 08M to asynchronously fade on and off 2 sets of 9 LED's based on the state of some input sensors. With 2 sets to fade I want to use the PWM command, not the PWMOUT command. It isn't working well - LEDs blink or flash or come on almost instantly. So, couple questions:

Does PWM operate in the background like PWMOUT or does the code pause for the number of cycles specified?

Is a period defined as a single 5-ms cycle or if I specify 3 cycles, does the period change to 15 ms or execute 3 periods?

The Manual2 document says it returns the pin to input when done. I have the pins connected to the gate of 2 Mosfets so I can drive multiple LEDs. If returned to an input seems like I could get some unwanted feedback. Should I force the pin back to an output?

The code I have takes about 15ms to run each cycle with the PWM commands at the end of each cycle. Should I restructure the code to
force more frequent PWM command executions?

I am running overclocked to 8MHz, how does this affect PWM?

Any other hints on how to get expected behavior out of PWM?

TIA

-- Dan
 

BeanieBots

Moderator
The PWM command does NOT run as background task like PWMout.
It only runs for as many complete cycles as you specify in the command.
The next line of code will not execute until the number of cycles has completed.

The pin will become an input when the command has finished.
This is because the command is designed around using PWM to generate an analog voltage. The pin feeds a capacitor via a resistor. At the end of the command, the capacitor voltage will be the average of the digital signal. In other words, the duty cycle times the PICAXE supply voltage.
If the pin did not become an input, that voltage would quickly drain away or would be driven to the supply voltage depending on if the pin was high or low.

In your case, the FET gate will effectively be left floating. This is not good and could quickly destroy the FET. You should fit a pulldown resistor to prevent that from happening.

There are many code examples for fading LEDs on this forum. Do a search for RGB.

If you double the clock speed, then the PWM frequency will also double. The duty will remain the same.
 

wilf_nv

Senior Member
To BeanieBot's outline, I would add the following:

The PWM signal generates a burst of pulses with a variable dutycycle and duration on the picaxe pin which, when connected to a simple R/C low pass filter e.g. 1M and 1uf, generates an analog voltage on the capacitor.

At the end of the PWM pulse burst, the picaxe pin reverts to input mode and if connected to a high impedance load like a mosfet gate, the voltage on the capacitor will vary little between refresh cycles.

So the capacitor acts like a short term memory which in a sense make the PWM command operate in the background.

The analog voltage on the cap generated by the PWM command determines the FET gate voltage, the transconductance of the FET, the current through the FET and the brightness of the LEDs.

The mosfet should be a logic level type that typically starts to turn on at 2V.

Above that turn on threshold, the FET drain current will change rapidly from fully OFF to ON with a small increase in gate voltage.

For practical control of the LED current, a current sensing resistor is connected between the source pin and ground to make the FET a voltage controlled current source.

The LED(s) are connected between the drain of the FET and Vcc or a higher V+ supply.

The picaxe is powered from a regulated Vcc of 5V.

Now the PWM control voltage can be adjusted between the FET turn-on threshold of about 2V and Vcc. The LED currrent will then be proportional to a control voltage of 0-3V across the source current sensing resistor.

The source resistor value will depend on the required LED current. A 30ohm 1/2W resistor will set the maximum LED current with 100% PWM to about (Vcc-2V)/30ohm=100ma.
For example if you have 5 LEDs connected in series, each of which has a forward voltage of 2V, then the total forward voltage drop of the 5 LEDs is about 10V and a LED power supply voltage of about 16V would be required. This LED supply voltage can be unregulated and vary a few volts with no effect on the LED brighness.

The FET will operate in the linear mode and power disipation must be considered.

With the above example of 5 series LEDs and a 16V supply and 100mA LED current, the FET dissipates slightly more than 1/4W at full current. (3V across the resistor and 10V across the LEDs leaves 3V drain to source across the FET which will dissipate 300mW)

A T0-220 type FET can have a power dissipation of 1W without heat sinking. For high power applications using higher drain current and/or higher drain to source voltage drop, some heat sinking may be required to avoid thermal effects from the positive temperature coefficient of the FET that would reduce LED brightness.

You will need to determine experimentally what the required PWM value range is that gives the desired visual effect.

wilf


Edited by - wilf_nv on 25/02/2007 00:54:19
 

premelec

Senior Member
I gave up using PWM with caps and op amps to
hold last setting - now I use serial controlled DACs 4 channel 8 bit and 8 channel 8 bit from TI.... to modulate the current to LEDs... they hold until told to change...
 

wilf_nv

Senior Member
Premelec, you share your experience why you gave up the simple PWM solution and which TI chips you now use?

 

toxicmouse

Senior Member
are there any nasty surprises when PWMout and serin commands are used? i am thinking of the problems with using the servo command and serin. does this use the same internal timer?
 

inglewoodpete

Senior Member
wilf asked about what chip can be used with a PICAXE as a DAC. TI make a magic little 14-pin 4 channel DAC (type TLC5620C). This is ideally suited to the PICAXE: 5v supply & has its own power-on-reset.

I have some code I wrote to drive it but that's at home and I'm at work. Can post it later if requested.
-Peter
 

wilf_nv

Senior Member
Quad DACs are perhaps a bit overkill for this application and would still need more circuitry for voltage to current conversion and output transistors to control the approximately 500ma maximum current required for each group of 9 LEDs.

By comparison, the 08M can use bit banged delta modulation and two R/C networks to generate analog voltages to control the gates of two MOSFETs each controlling a group of 9 LEDs. The LED currents are adjusted by incrementing or decrementing (delta modulation) the gate voltage with short pulses and measuring current through the shunt resistors with ADC inputs until the LED currents match the setpoints.

<A href='http://www.user.dccnet.com/wrigter/DualLEDFader.gif' Target=_Blank>External Web Link</a>

The LED current can be reduced by connecting the LEDs in a 3x3 series/parallel arrangement and using a 9V-12V supply for the LEDs.

Some primitive code to drive this circuit follows:

<code><pre><font size=2 face='Courier'>
;DUAL LED FADER routine - feb 2007 - wilf rigter
;http://www.user.dccnet.com/wrigter/DualLEDFader.gif

setfreq m8 ;double clock speed
input 0,1,2,3,4 ;intially all inputs

main:

for b8=10 to 245 ;limit current range to avoid adc rollover
b6=255-b8 ;ramp b6 (LED1 current) down from 245 to 10
b7-b8 ;ramp b7 (LED2 current) up from 10 to 245
gosub adjust1 ;make it so
pause 100
next b8

for b8=10 to 245 ;limit current range to avoid adc rollover
b6=b8 ;ramp b7 (LED2 current) from 10 to 245
b7=255-b8 ;ramp b6 (LED1 current) down from 245 to 10
gosub adjust2 ;make it so
pause 100
next b8
got main

adjust1:
readadc10 1,w1 ;read LED1 current on pin ADC1
if b3&lt;&gt;0 then LEDsoff
if b2=b6 then adjust2 ;skip if LED1 current=b6
if b2&lt;b6 then increase1 ;increase LED1 current

decrease1:
low 0 ;set pin0 low to decrease LED1 current
input 0 ;tri-state pin0 and hold LED1 current
goto adjust0 ;repeat

increase1:
high 0 ;set pin0 high to increase LED1 current
input 0 ;tri-state pin0 and hold LED1 current
goto adjust0 ;repeat

adjust2:
readadc10 4,w1 ;read LED2 current on pin ADC4
if b3&lt;&gt; then LEDsoff
if b2=b7 then goback ;return to main if LED2 current=b7
if b2&lt;b7 then increase2 ;increase LED2 current

decrease2:
low 2 ;set pin2 high to increase LED2 current
input 2 ;tri-state pin2 and hold LED2 current
goto adjust2 ;repeat

increase2:
high 2 ;set pin2 high to increase LED2 current
input 2 ;tri-state pin2 and hold LED2 current
goto adjust2 ;repeat

LEDsoff:
low 0,2 ;LED current &gt;500ma so turn off both FETs
pause 1000 ;for 1 second
input 0,2

goback:
return
</font></pre></code>

Corrected gosub labels and added 100ms pauses


Edited by - wilf_nv on 26/02/2007 15:25:54
 

astein

New Member
There are many examples of fade-in/fade-out LED routines floating around, but I figured I'd contribute yet one more. My code at http://www.medcosm.com/picaxe_blendlight.htm uses PWM explicitly control light intensity. In this example, an LED blends between various intensities of Red and Green. A few other picaxe projects also listed. The picaxe drives the LEDs well directly for my use, but others might want a small driver circuit.

I calculate a few things on the fly during the fade, so overclocking is necessary to reduce flickering. Now that Tri-color LEDs are available, I have a tri-color blend version to post at some point when I find time.
 

wilf_nv

Senior Member
astein - the schematic shows the LEDs connected to pin0 (leg7) and pin1 (leg6) but the code controls pin1 and pin2.

With the 08M clock x2, the pair of single cycle PWM commands generate pulses with a maximum ON time for each LEDs of 2.5ms. With 5ms devoted to display time, what would be the maximum refresh time between LED pulses before there is noticable flicker?

Digital PWM is efficient but peak currents may be high.

The analog method has the advantage that refresh time can be 100ms or more leaving plenty of time for executing other code.

 

premelec

Senior Member
Sorry I've been off with guests for a while - the DACs I use are TI TLC5628 8x8 bit and TLC5620 4x8 Bit. I make up an address &amp; value etc into a 12 bit data word in W0 then clock and shift bits by x 2 each round to load the DAC.

Additionally the reference to the DAC can be modualted by another DAC etc... The main advantage is that once you set the DAC ouput it stays set - it's like a 255 tap resistor.
In my case I use this voltage for constant current drive to LEDs. The voltage can go to the base of a bipolar transistor where it's emitter is going to a resistor to ground and the collector goes to LED to + voltage.


I must admit I don't know how to paste the code here - when I highlight it in the PICAXE editor and select copy and then try 'paste' here nothing happens... anyhow it's not very complicated and if you have plenty of code space [18X instead of 08M I used] you don't have to shift the bits and it out loads a lot faster... hope this helps and maybe I can figure out the code copy routine sometime!

Note that the TI TLC5922 has 16 chanels of LED cc drive but a rather long address if you want to try that...
 

Revolvr

New Member
Thanks for the replies everyone. This has helped me understand the PWM command - got a lot to think about. The code utilizing the current feedback is very interesting.

Cheers,

-- Dan
 
Top