10 led bar graph and PWM

wapo54001

Senior Member
I was reading the 10 led bar graph thread, and the discussion of using pwm to output an analog value to be measured by an led stack.

I started to post a response saying that of course the method used in the "picaxe controlled variable power supply" was superior to pwm. I was going to suggest replacing the LM317 circuitry with a single 300~10K pullup resistor to +5 (as I have been doing for the past two years), and the mosfet would be able to set any output voltage between zero and +5V with 0.005V resolution by working against the pullup resistor while using feedback via a readADC10 to exactly set the output. The 08M has plenty of room to do the requisite math so that practically any input can be translated and the result output as a voltage to be directly read on a 3.5 digit panel meter with great accuracy. For example, 100psi could be output as 1.000V, and by moving the decimal place, can be read as 100.0psi on a dpm.

Then, I realized that I didn't know what I was talking about because I had never used pwm to achieve this particular goal. I just *think* I'm right, and for me that is always a formula for disaster.

So, three questions:

1. Does pwm, when used alone, deliver the kind of accuracy and resolution that is available by using a mosfet to control output with .005V resolution?

2. If the answer to 1) is "no," has anyone ever used readADC10 feedback with pwmout to achieve the kind of accuracy and resolution that is available using the mosfet?

3. If the answers to 1) and 2) are not favorable, is it time to give up pwm as a means to set analog levels when a much more accurate method is available with a trivial increase in complexity?

Just wondering . . .
 

BeanieBots

Moderator
The problem with ANY power supply design which uses a slow micro (eg PICAXE) as part of the feedback loop is response time.
When the output is loaded, there is a finite time before the volt drop is noticed and correction is applied. Similarly, when the load is removed, the output voltage will shoot up until the controller "notices" and then reduces whatever in the drive circuit.
A good power supply needs to be ULTRA fast in its response or brown-outs and/or surges will occur.
Saying that, I've made countless PICAXE based variable power supplies, mainly constant current types for battery charging. The trick, is to have hardware do the required fast feedback and let the PICAXE control the DEMAND voltage.
This can be done easily using PWM and WILL give you its full resolution of 10-bit accuracy.
To get the accuracy, you need to filter the PWM signal with at least second order or more filtering to prevent ripple. The filtered signal must then be buffered to prevent subsequent stages from loading it. An op-amp buffer can of course also provide significant filtering itself.

So, yes, a PICAXE can be used to control a variable power supply to 10-bit resoltion but NOT if it is used in the feedback loop. Only if it is used to provide the demand signal. As with ReadADC10, it should also be noted that the final accuracy also depends on the PICAXE supply accuracy so avoid any LEDs etc being used on the PICAXE supply lines and be careful with layout.
 

wapo54001

Senior Member
Wait, wait! I'm not talking about a power supply -- I'm talking about taking a sensor input and creating an output voltage which can be read by a high impedance meter or (for rough measurements) an led stack. I'm talking about using an 08M to do precision translation from an input range to a different output range, not to respond to varying loads a la power supply regulation.

For example, a standard voltage output sensor delivers .5v at zero and 4.5V at max output. That could represent a 100psi range, a 15psi range, a 50psi range. That .5~4.5 must be translated to output 0~xxx.x so that it can be output on a digital meter so that the reading is a direct representation of the input, whatever it is. My question is whether pwm is the best way to do this, or is the mosfet output the superior method.

Furthermore, speaking of power supplies, the design that BCJ and I worked on does use the LM317 for fast response, the 08M is not involved in regulation, but only in setting the voltage level of the output.
 

BeanieBots

Moderator
The resolution of the ADC will be the limiting factor. Not the PWM signal.
If the PWM signal is filtered and buffered then it will give 10-bit resolution.
The problem will be in conditioning the input signal to use the FULL range of the ADC input. This could be done with op-amps but accuracy could easily be lost with slightly off gain settings.

Quite simply, try it.
Set up a PICAXE with a GOOD SOLID power supply and no switching supply loads. Then feed the PWM into 100k/100nF - 100k/100nF - hi input Z op-amp buffer with a DVM on the output.
Measure the volts at 100%. Then CALCULATE what the voltage should be at some other duty. Set that duty and measure again.
It really does work to 10 bit.

Your input scaling from the sensor is another matter altogether!
 

mholguin

New Member
shushhhhhhhhhhh!!!!! (as in "way over my head")

I've been following this discussions with interest, but have to admit it went waaaaaay over my head (PWM usage, I mean).

I think I begining to understand, but, I'd rather sit back and continue reading... Is there a PWM "primer" some where ?
 

wapo54001

Senior Member
I think the level of filtering that is required with pwm puts it to bed for me -- too much work (and that's before you use feedback to assure accuracy). The mosfet requires only a 1M and a 10K and a .47uF for very smooth output.

I agree that the input resolution must be considered and is the limiting factor in most real-world instances.

I just wanted to know how the pwm accuracy and simplicity compared to the mosfet soltuion. I think you're saying that the resolution is equivalent as long as the filtering is in place. That's what I didn't know and could not find the answer. Thanks for explaining it.
 

BeanieBots

Moderator
@mholguin
PWM is VERY simple.
All it does is set an output high and low at a fixed rate. The amount of time it is high compared to the amount of time it is low is called the duty. If it is high for same amount of time as it is low, it is said to have 50% duty. If it is high all the time, it is 100% duty. The average voltage is simply the "high" voltage multiplied by the duty. To average the pulses, just filter it.
The PWM duty can be set to 10 bit accuracy. So, if the high output is 5v, then the average voltage can be set to any voltage between 0v and 5v in steps of 5/1024.

@wapo54001.
Two res + two caps + op-amp. Not very complex.
Scaling 0.5v - 4.5v into 0v - 5v MUCH more complex.
You will need to generate a reference voltage and build a differential amplifier. Very simple in theory but getting the right resistor values will be a nightmare! You could use pots but unless they are high quality they will drift and then of course you still need to calibrate it.
Just using the raw signal and doing maths on it will of course lose you significant resolution. Besides, once you've scaled the raw signal, exactly what would the PICAXE be needed for anyway? ADC -> PWM:rolleyes:
 

BeanieBots

Moderator
Yes, it really is that simple. In fact, if you don't mind a bit of ripple, just feed it into a simple RC to get the average. The second order filter and op-amp is only required if you want to get the maximum resolution the PICAXE can offer out of it without having to worry about what load is connected to it. If it's just a DVM, then the op-amp probably isn't needed.
 

wapo54001

Senior Member
@wapo54001.
Two res + two caps + op-amp. Not very complex.
Scaling 0.5v - 4.5v into 0v - 5v MUCH more complex.
You will need to generate a reference voltage and build a differential amplifier. Very simple in theory but getting the right resistor values will be a nightmare! You could use pots but unless they are high quality they will drift and then of course you still need to calibrate it.
Just using the raw signal and doing maths on it will of course lose you significant resolution. Besides, once you've scaled the raw signal, exactly what would the PICAXE be needed for anyway? ADC -> PWM:rolleyes:
BeanieBots, I don't use a preamp op-amp to scale -- I scale my sensors in the Picaxe. First math routine is to subtract the .5 and ensure that it doesn't wrap around. Then take the 0~820 count (4 volt range) and use a straight line formula to make it read an output voltage that is direct readable on a dpm 0~full scale, whatever that is. An 820 count is good resolution -- easily handles most sensors and applications.

Further, as I think back to when I was designing this circuit, I needed the mosfet only because one of the instruments I was driving had a 300 ohm pullup resistor on its input, and I needed something to pull that down effectively (the 08M wouldn't do it by itself).

If all you need is to drive a high impedance dpm, you should be able to do it with just three passive components -- the 1M, 10K, and .47uF cap, and have very little ripple. Still much less trouble than the pwm solution.
 

BeanieBots

Moderator
If all you want to drive is a DPM, then PWM -> RC -> DPM is all you need and its resolution will exceed that of your input.
Most DPMs are high input Z so you can make the RC large and avoid the need for second order filtering. Besides, many DPMs do quite a good averaging themselves.
Try it. Just set a 50% duty and connect the DPM via an RC. You might not even need the C. Many have FSD of 1v, so you might need some potential division.

I don't understand your last paragraph. Your suggestion IS the PWM solution!
 

wapo54001

Senior Member
If my last para IS the pwm solution, sounds like both solutions use the same hardware. I was just responding to your earlier description of what was required for a filtered pwm output:

Two res + two caps + op-amp. Not very complex.
But if the hardware is the same and delivers the same smoothed output and is equally easy to understand and program and the pwm parameters can be modifed smoothly 'on the fly,' then the answer to my original question is that both will work equally well. Thanks for answering my question.
 

boriz

Senior Member
I was sceptical about 10bit PWM range. But after having a little play in the PWM wizard, it works of course. You need to set the frequency to it’s lowest possible value, 3910Hz. You get PWMOUT pin,255,n where n=0 to 1023, 1023 being 100% PWM. The lower frequency might require beefier filtering, though doubling the AXE frequency to 8MHz will help.

Using a quality op-amp buffer as stated, should provide a stable repeatable voltage. No feedback required. Changing the gain of the op-amp will give you a larger/smaller output voltage range. Note: The op-amp needs to be a single rail type, with input/output capable of approaching the rails. REF: http://www.swarthmore.edu/NatSci/echeeve1/Ref/SingleSupply/SingleSupply.html

Op-amps have high stability and low tempco. Operational Amplifiers were in fact originally designed as analogue computation devices, each providing a fixed multiplier/adder of the input signal(s), the input and output values being represented by voltage levels. You can do some quite complex maths almost instantly using a couple of op-amps. One of the first ever applications of this kind of analogue computer was artillery aiming. You provide the distance, direction and windage etc. and the op-amps instantly tell you how to point the gun. It was very successful.

SEE:
http://www.play-hookey.com/analog/
http://dcoward.best.vwh.net/analog/
http://dcoward.best.vwh.net/analog/template.htm
http://en.wikipedia.org/wiki/Operational_amplifier_applications
 
Last edited:
Top