Can I use Atmega8 pwm as a variable voltage regulator?

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
I am planning to replace a potentiometer using PWM.
The potentiometer varies the DC voltage from 0V to 5V.
Can I substitute this with a PWM output from atmega8?
:?:

This topic has a solution.

Regards,
Frederic Philips

Last Edited: Wed. Sep 5, 2018 - 01:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can use PWM to generate an DC voltage but I don't know if it suites what you want to do or not.
Where does that 0-5v go to?

How-to use PWM to Generate Analog (or Analogue) Voltage in Digital Circuits:
http://dev.emcelettronica.com/ho...

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Alex,
The pot controls the current[Amps] in a welding inverter.As far as I remember the DAC implementation using PWM produces sine wave and not a DC.The pot just varies the voltage and does not source or sink any current. So is it possible for the PWM to maintain a constant level of DC? :?:

Regards,
Frederic Philips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://provideyourown.com/2011/a... seems to answer "yes"; Elektor in august 2011 showed a arduino+pwm based regulator, too.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you can use it but a digital pot would probably be more be more accurate.
Take a look http://www.microchip.com/ParamCh...
They can be controlled with I2C or SPI.

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Another solution -the easies from a software point of view- would be a R2R network, though it might need 8 avrs pins (PWM eats one pin, I2C/TWI ... two, SPI four pins): if your AVR is fed through a stabilized supply, perhaps it might work (and it does not need a filter to hide the PWM frequency)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes I've seen an R-2R ladder be used http://www.tuxgraphics.org/elect....

It could also be done with a shift register (74HC595) having R-2R in the outputs instead of directly in the AVR port.

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I had a look at the same seller and the same manufacturer selling presoldered cards : digital potentiometer http://www.lextronic.fr/P1898-mo... with MCP4010 were 13E$, DAC (8 bits) http://www.lextronic.fr/P5327-mo... were 10E$.
Your solution with 74HC595 (or cheaper 4094 : only three wires http://senster.com/blog/wp-conte...) is even cheaper, and allows to chose the number of used bits (at the expense of soldering).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to the Forum.

Yes, you can use PWM to generate a voltage from almost 0 to almost 5V.

No, PWM does not produce a sin wave. It produces a series of (square) pulses.

One can set up a Timer/Counter to generate the PWM signal for you, using the hardware module, instead of generating the signal all in software. In this case the micro just sends a value to the T/C to set the output level from 0-100%.

The rate of the pulses is something you set up, and in your application you would then leave it alone.

The width of each pulse in the stream of pulses you vary to get your variable output voltage.

To convert the stream of pulses to a DC voltage you need to pass the signal through a Low Pass Filter. A string of on and off pulses then becomes a steady dc voltage. (Almost)

For example:
If the pulse is on 0 % of the time, (i.e. always off), then the output is 0 V.
If the pulse is on 100 % of the time, (i.e. always on), then the output is 5 V.
If the output is on 50 % of the time, (a true "square" wave), then the output will be 2.5 V, (50 % of v+).
If the pulse is on for 33 % of the time then the output will be 0.33 * 5 V = 1.65 V.

The theory is easy, but there are always some details in practice...

The output driver on the micro's pin probably doesn't go all the way to 0 V, or all the way to +5 V. If just gets close to those values.

This means that 0 - 100 % will actually span a range from a bit above 0 V to a bit below 5 V.

Next, the output isn't a smooth DC, it has some "ripple" in it, from the capacitor in the LPF charging up and discharging with each pulse.

The frequency of the pulse train and the cutoff frequency of the filter impact the amount of ripple seen. Adding several stages of low pass filtering also reduces the ripple seen. (i.e. using a higher order filter.)

Your output device may or may not care about a little ripple. In practice one usually follows the micro with a op-amp or two to provide the filtering and to scale the signal to the desired output voltage range. The op-amp also provides a good drive source for the output device receiving the signal, and prevents that device from "loading" the signal, (effecting the filter and output voltage).

In practice PWM is often used to generate a DC control signal.

Note that using an R-2R ladder on a micro will not solve all of the above issues. The output still won't go all the way to 0 V, or all the way to +5 V. It, also, generally needs an op-amp to buffer the ladder from the device it is driving.

Jim, KA7EHK, has an excellant artical on Filtering PWM Signals , which provides a better explanation and much more detail.

If you really need 0.00 V and 5.00 V then one would typically use a bi-polar power supply to get to 0 V and to get to 5 V. (+/- 10 V, or -1/+6 V, etc.)

Lastly, as it sounds like you are controling a high energy device, look carefully at your start up conditions and your fail safe conditions.

At start up does the welder's output have power before the micro has started up and is in control of the welder?

If the welder's EMI disrupts the micro and it resets or goes off processing garbage is there a back-up fail-safe system?

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone,
I have grabbed a digital potentiometer from Microchip(MCP4131). It uses SPI interface and it solves the purpose. Thanks for everyone. I was awestruck by your speedy responses.

Regards,
Frederic Philips

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
I just now completed the MCP4131 interfacing.
Please have a look here:
http://fredericphilips.wordpress.com/projects/welders-aid/mcp4131-interface/
And once again thanks to everyone for helping me out.

Regards,
Frederic Philips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you considered creating a spi_send function instead of using multiple times the following?

SPDR  = a;  // send Character
while (!(SPSR & (1<<SPIF)));  // wait until Char is sent 

and while at it you can also add a return SPDR; to return the received data in case you want to use it.

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

alexan_e wrote:
Have you considered creating a spi_send function instead of using multiple times the following?
Alex

Hi Alex,
Yes, I could add a separate function for that. Since this is a part of a big project, I didn't concentrate on the code. The final firmware will be solid.

Regards,
Frederic Philips

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:
Welcome to the Forum.

Yes, you can use PWM to generate a voltage from almost 0 to almost 5V.

No, PWM does not produce a sin wave. It produces a series of (square) pulses.

One can set up a Timer/Counter to generate the PWM signal for you, using the hardware module, instead of generating the signal all in software. In this case the micro just sends a value to the T/C to set the output level from 0-100%.

The rate of the pulses is something you set up, and in your application you would then leave it alone.

The width of each pulse in the stream of pulses you vary to get your variable output voltage.

To convert the stream of pulses to a DC voltage you need to pass the signal through a Low Pass Filter. A string of on and off pulses then becomes a steady dc voltage. (Almost)

For example:
If the pulse is on 0 % of the time, (i.e. always off), then the output is 0 V.
If the pulse is on 100 % of the time, (i.e. always on), then the output is 5 V.
If the output is on 50 % of the time, (a true "square" wave), then the output will be 2.5 V, (50 % of v+).
If the pulse is on for 33 % of the time then the output will be 0.33 * 5 V = 1.65 V.

The theory is easy, but there are always some details in practice...

The output driver on the micro's pin probably doesn't go all the way to 0 V, or all the way to +5 V. If just gets close to those values.

This means that 0 - 100 % will actually span a range from a bit above 0 V to a bit below 5 V.

Next, the output isn't a smooth DC, it has some "ripple" in it, from the capacitor in the LPF charging up and discharging with each pulse.

The frequency of the pulse train and the cutoff frequency of the filter impact the amount of ripple seen. Adding several stages of low pass filtering also reduces the ripple seen. (i.e. using a higher order filter.)

Your output device may or may not care about a little ripple. In practice one usually follows the micro with a op-amp or two to provide the filtering and to scale the signal to the desired output voltage range. The op-amp also provides a good drive source for the output device receiving the signal, and prevents that device from "loading" the signal, (effecting the filter and output voltage).

In practice PWM is often used to generate a DC control signal.

Note that using an R-2R ladder on a micro will not solve all of the above issues. The output still won't go all the way to 0 V, or all the way to +5 V. It, also, generally needs an op-amp to buffer the ladder from the device it is driving.

Jim, KA7EHK, has an excellant artical on Filtering PWM Signals , which provides a better explanation and much more detail.

If you really need 0.00 V and 5.00 V then one would typically use a bi-polar power supply to get to 0 V and to get to 5 V. (+/- 10 V, or -1/+6 V, etc.)

Lastly, as it sounds like you are controling a high energy device, look carefully at your start up conditions and your fail safe conditions.

At start up does the welder's output have power before the micro has started up and is in control of the welder?

If the welder's EMI disrupts the micro and it resets or goes off processing garbage is there a back-up fail-safe system?

JC