Author Topic: PWM to analog 10v. What have I done wrong?  (Read 4706 times)

0 Members and 1 Guest are viewing this topic.

Offline sofa-kingTopic starter

  • Contributor
  • Posts: 11
  • Country: us
PWM to analog 10v. What have I done wrong?
« on: September 29, 2021, 03:28:12 am »
I have a circuit I'm working with. Basically the idea is to have IO2 output a PWM signal (5khz, 3.3v) and use that signal to output a 0-10v analog voltage signal. I've tried the attached circuit but I basically get either 0v or 10v. If it's anything but 0% duty it is at ~10v. Hoping to get some insight into what I'm doing wrong.

 

Offline timenutgoblin

  • Regular Contributor
  • *
  • Posts: 190
  • Country: au
Re: PWM to analog 10v. What have I done wrong?
« Reply #1 on: September 29, 2021, 04:31:31 am »
When IO2 goes high, Q3 turns on connecting the 10V rail to C8 through the resistor R10 charging C8 to 10V. When IO2 goes low, Q3 turns off disconnecting C8 from the 10V rail. C8 remains charged at 10V during this time because there isn't a parallel resistor to discharge into. C8 remains charged at 10V until IO2 goes high again. Try connecting a 1K resistor or a higher resistance across C8 so that C8 can then discharge when IO2 goes low.  :-+
 

Offline timenutgoblin

  • Regular Contributor
  • *
  • Posts: 190
  • Country: au
Re: PWM to analog 10v. What have I done wrong?
« Reply #2 on: September 29, 2021, 05:36:11 am »
A better solution would be to use a CMOS inverter such as CD4069.

https://www.ti.com/lit/ds/symlink/cd4069ub.pdf

Power the CD4069 from your 10V rail. Use your existing circuit to level-shift IO2 to produce 0V and 10V needed to drive the inverter. Remove R10 and C8. Insert a 1K0 or 2K2 resistor between the 10V rail and the emitter terminal of Q3. Connect the collector terminal of Q3 to ground (GND). Connect the emitter terminal of Q3 to the input of the inverter and then connect the output of the inverter to your LPF (C8 and R10).

The output of the inverter uses a push-pull topology which is better suited for your LPF.
 

Offline perieanuo

  • Frequent Contributor
  • **
  • Posts: 838
  • Country: fr
Re: PWM to analog 10v. What have I done wrong?
« Reply #3 on: September 29, 2021, 06:18:44 am »
hi,
have you seen this https://forum.arduino.cc/t/0-10v-pwm-2-solution/349017/17 ?
the pwm logic is kinda inverted, but you'll figure out in software easily

the opamp approach is https://arduinodiy.wordpress.com/2019/09/28/getting-i2c-regulated-0-10volt-out-of-an-arduino-or-esp8266/
or better https://electronics.stackexchange.com/questions/432844/i-want-to-create-a-stable-0-10-v-dc-output-from-a-3-3v-pwm-signal-arduino-esp82

just some observation in your schematic: i rarely put pull-down when i control bjt from ucontrollers, in fact never
« Last Edit: September 29, 2021, 06:34:22 am by perieanuo »
 

Offline Kleinstein

  • Super Contributor
  • ***
  • Posts: 14167
  • Country: de
Re: PWM to analog 10v. What have I done wrong?
« Reply #4 on: September 29, 2021, 07:37:59 am »
hi,
have you seen this https://forum.arduino.cc/t/0-10v-pwm-2-solution/349017/17 ?
the pwm logic is kinda inverted, but you'll figure out in software easily

the opamp approach is https://arduinodiy.wordpress.com/2019/09/28/getting-i2c-regulated-0-10volt-out-of-an-arduino-or-esp8266/
or better https://electronics.stackexchange.com/questions/432844/i-want-to-create-a-stable-0-10-v-dc-output-from-a-3-3v-pwm-signal-arduino-esp82

just some observation in your schematic: i rarely put pull-down when i control bjt from ucontrollers, in fact never
The two circuit have the same mistake as the original post: the PWM only woks as averaging if there is also a dicharge path for the capacitor. The first circuit may be OK to drive a load like a fan to provide that current.


There are mainly 2 options:
1) use some level shifting of the digital signal to 0/10 V level (e.g. with a CMOS switch like DG419) and than us filtering the 10 V PWM signal.
    A transistor based level shifter and than 4069 would be an option.
2) Use filtering of the PWM signal directly at the µC ouput level and than use an OP (e.g. TLC271 to amplify and maybe as part of an active filter) to bring the level to 10 V. In this case the µC supply would be the reference voltage.
 

Online dietert1

  • Super Contributor
  • ***
  • Posts: 2057
  • Country: br
    • CADT Homepage
Re: PWM to analog 10v. What have I done wrong?
« Reply #5 on: September 29, 2021, 08:27:50 am »
If you don't know what is the load or you want to test without load, the output stage needs to be totem-pole: implement pull up and pull down. If you want to do it with discrete parts, watch out for a minimum dead time between turning off one of the drivers and turning on the other one. Otherwise there may be unexpected cross conduction from the 10 V supply to Gnd.
A part i have been using is ISL84544. Its input is TTL and its output switches up to 12V.

Regards, Dieter
« Last Edit: September 29, 2021, 10:42:05 am by dietert1 »
 
The following users thanked this post: perieanuo

Offline perieanuo

  • Frequent Contributor
  • **
  • Posts: 838
  • Country: fr
Re: PWM to analog 10v. What have I done wrong?
« Reply #6 on: October 01, 2021, 07:48:34 am »
hi,
have you seen this https://forum.arduino.cc/t/0-10v-pwm-2-solution/349017/17 ?
the pwm logic is kinda inverted, but you'll figure out in software easily

the opamp approach is https://arduinodiy.wordpress.com/2019/09/28/getting-i2c-regulated-0-10volt-out-of-an-arduino-or-esp8266/
or better https://electronics.stackexchange.com/questions/432844/i-want-to-create-a-stable-0-10-v-dc-output-from-a-3-3v-pwm-signal-arduino-esp82

just some observation in your schematic: i rarely put pull-down when i control bjt from ucontrollers, in fact never
The two circuit have the same mistake as the original post: the PWM only woks as averaging if there is also a dicharge path for the capacitor. The first circuit may be OK to drive a load like a fan to provide that current.


There are mainly 2 options:
1) use some level shifting of the digital signal to 0/10 V level (e.g. with a CMOS switch like DG419) and than us filtering the 10 V PWM signal.
    A transistor based level shifter and than 4069 would be an option.
2) Use filtering of the PWM signal directly at the µC ouput level and than use an OP (e.g. TLC271 to amplify and maybe as part of an active filter) to bring the level to 10 V. In this case the µC supply would be the reference voltage.
of course you have a discharging path equivalent resistance, it's the input resistance of the driven module, it's there and it's real (in very rare cases you attack some high-impedance module, that's for special applications imho). i didn't want to complicate the scenario, but i bet coffee for a months on this one  :-* , i implemented some scenarios like this one (from esp8266 0-10V output to those dimmable light controllers on the market) and it works.
in theory you're correct, in practice in almost all cases the next module do have finite input R equivalent.
if reading those posts (that i shared to be studied) we observ those simple schematics just work. and also that discharge load paralel with C is also mentioned, so my conscience is clear
« Last Edit: October 01, 2021, 07:51:51 am by perieanuo »
 

Offline Badwolf

  • Contributor
  • Posts: 25
  • Country: fr
Re: PWM to analog 10v. What have I done wrong?
« Reply #7 on: October 01, 2021, 05:46:56 pm »
hi,
have you seen this https://forum.arduino.cc/t/0-10v-pwm-2-solution/349017/17 ?
the pwm logic is kinda inverted, but you'll figure out in software easily

the opamp approach is https://arduinodiy.wordpress.com/2019/09/28/getting-i2c-regulated-0-10volt-out-of-an-arduino-or-esp8266/
or better https://electronics.stackexchange.com/questions/432844/i-want-to-create-a-stable-0-10-v-dc-output-from-a-3-3v-pwm-signal-arduino-esp82

just some observation in your schematic: i rarely put pull-down when i control bjt from ucontrollers, in fact never
The two circuit have the same mistake as the original post: the PWM only woks as averaging if there is also a dicharge path for the capacitor. The first circuit may be OK to drive a load like a fan to provide that current.



These 2 circuits work fine without a load. The PWM is simply reversed. When the voltage at the base of the transistor is low, the transistor is blocked and the capacitor charges through the resistors (1k + 680 or 1k + 100k). When the voltage on the base of the transistor is high enough, the transistor goes into saturation mode (Vce close to 0v) the junction between the 2 resistors is therefore close to 0v and the capacitor is discharged via one of the resistors (680 or 100k ) and the transistor
The simplest explanation is almost always somebody screwed up (Dr. House)
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19491
  • Country: gb
  • 0999
Re: PWM to analog 10v. What have I done wrong?
« Reply #8 on: October 01, 2021, 09:38:50 pm »
Why not filter the signal and amplify it with an op-amp?

Here's a circuit which will give a nice smooth output. It needs to be a rail-to-rail output op-amp if you want to run it off 10V, but if you have 12V, you can use the old LM358.

If you're feeling more adventurous, then you could use a Salen-Key filter with gain, but it's a bit more tricky to get right, without it oscillating.
« Last Edit: October 03, 2021, 06:50:34 pm by Zero999 »
 

Offline sofa-kingTopic starter

  • Contributor
  • Posts: 11
  • Country: us
Re: PWM to analog 10v. What have I done wrong?
« Reply #9 on: October 13, 2021, 10:14:12 pm »
Thank you all for your replies. It's given me much to think about and to experiment with! I tried adding a resistor across  C8 and I did get a varying voltage but it wasn't linear. Different resistor values across C8 seem to influence this. My goal is to as good as I can convert the duty cycle from the esp32 output to a voltage between 0-10.0 volts. My first attempt was with an op-amp with 3x gain value and it tracks pretty well. The weak link is variation of the supply rail. If it's off from 3.3v the output is off by a magnitude of 3 as well.
 

Offline Capernicus

  • Frequent Contributor
  • **
  • !
  • Posts: 483
  • Country: au
Re: PWM to analog 10v. What have I done wrong?
« Reply #10 on: October 13, 2021, 10:25:07 pm »
Why not just take the raw PWM signal and call it analogue already,   I bet you can turn one of those 9g servos with an ordinary potentiometre,  its just pure current instead of switched current but its the exact same thing.
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19491
  • Country: gb
  • 0999
Re: PWM to analog 10v. What have I done wrong?
« Reply #11 on: October 14, 2021, 08:25:28 am »
My first attempt was with an op-amp with 3x gain value and it tracks pretty well. The weak link is variation of the supply rail. If it's off from 3.3v the output is off by a magnitude of 3 as well.
You need a stable voltage. If your power supplies are not tightly regulated, then you need an additional voltage reference.

A logic IC, with inputs which can be driven off 3.3V, such as the 74LVC2G14 can be powered off a voltage reference IC, i.e. the TL431, to create a PWM waveform with a stable voltage, which can be filtered and amplified, with an op-amp.
« Last Edit: October 14, 2021, 08:28:36 am by Zero999 »
 

Offline Capernicus

  • Frequent Contributor
  • **
  • !
  • Posts: 483
  • Country: au
Re: PWM to analog 10v. What have I done wrong?
« Reply #12 on: October 14, 2021, 01:11:30 pm »
if the volts of the PWM signal is 10v,   then you just need to charge a cap with the PWM current,  and it should become a varying voltage on the capacitor.
then you supply a small load and it should discharge out,  at the volts it charged to.

If its less than 10v,  you increase the volts of the signal, then you make the capacitance smaller so it maxes out the cap with less current, I presume.
« Last Edit: October 14, 2021, 01:27:33 pm by Capernicus »
 

Offline Capernicus

  • Frequent Contributor
  • **
  • !
  • Posts: 483
  • Country: au
Re: PWM to analog 10v. What have I done wrong?
« Reply #13 on: October 14, 2021, 02:06:45 pm »
Get the pwm signal, and the inverse of the pwm signal, then put it into a voltage divider,  that will get you a varying voltage, but it has to be less than or equal to 3.3v.
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19491
  • Country: gb
  • 0999
Re: PWM to analog 10v. What have I done wrong?
« Reply #14 on: October 15, 2021, 08:31:42 am »
if the volts of the PWM signal is 10v,   then you just need to charge a cap with the PWM current,  and it should become a varying voltage on the capacitor.
then you supply a small load and it should discharge out,  at the volts it charged to.

If its less than 10v,  you increase the volts of the signal, then you make the capacitance smaller so it maxes out the cap with less current, I presume.
Get the pwm signal, and the inverse of the pwm signal, then put it into a voltage divider,  that will get you a varying voltage, but it has to be less than or equal to 3.3v.
Please post a schematic.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: PWM to analog 10v. What have I done wrong?
« Reply #15 on: October 15, 2021, 09:57:15 am »
<drivel> capacitor <more_drivel>
<drivel> voltage divider <more_drivel>
Please post a schematic.
:-DD
You asked *Capernicus* for a schematic?  :popcorn:

The guy who's either a poor excuse for a troll, or at the very least has an extremely unhealthy obsession with RC circuits, and claims purely passive RC circuits can solve any problem (probably including world poverty)!  Unfortunately their 'designs' either cant be built with real-world parts, or if buildable, can't work as claimed in this universe.  :box:

Might as well ask an 'over unity' wonk for 'free energy' generator plans, which at least will be more entertaining to debunk . . .

« Last Edit: October 15, 2021, 10:02:55 am by Ian.M »
 
The following users thanked this post: tooki

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19491
  • Country: gb
  • 0999
Re: PWM to analog 10v. What have I done wrong?
« Reply #16 on: October 15, 2021, 10:39:15 am »
<drivel> capacitor <more_drivel>
<drivel> voltage divider <more_drivel>
Please post a schematic.
:-DD
You asked *Capernicus* for a schematic?  :popcorn:

The guy who's either a poor excuse for a troll, or at the very least has an extremely unhealthy obsession with RC circuits, and claims purely passive RC circuits can solve any problem (probably including world poverty)!  Unfortunately their 'designs' either cant be built with real-world parts, or if buildable, can't work as claimed in this universe.  :box:

Might as well ask an 'over unity' wonk for 'free energy' generator plans, which at least will be more entertaining to debunk . . .
Come on, don't ruin my fun!

Anyway, here's another idea: use a voltage reference with an analogue switch, controlled by the PWM, before the filter.
« Last Edit: October 16, 2021, 12:05:19 pm by Zero999 »
 
The following users thanked this post: sofa-king

Offline sofa-kingTopic starter

  • Contributor
  • Posts: 11
  • Country: us
Re: PWM to analog 10v. What have I done wrong?
« Reply #17 on: October 18, 2021, 09:14:33 pm »
Hi, thanks again for your replies. Zero999 thank you for the idea and the schematic. Am I correct that this would output a 0-5v signal? 2.5v from the TL341 is switched at the analog switch resulting in a 0-2.5v square wave, then filtered with the resistors and caps and finally amplified by x2?
 

Online dietert1

  • Super Contributor
  • ***
  • Posts: 2057
  • Country: br
    • CADT Homepage
Re: PWM to analog 10v. What have I done wrong?
« Reply #18 on: October 18, 2021, 10:01:13 pm »
How did you determine that x2 gain number? What gain would be appropriate for a 0..10 V range? How would you modify the circuit to achieve that gain?

Regards, Dieter
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19491
  • Country: gb
  • 0999
Re: PWM to analog 10v. What have I done wrong?
« Reply #19 on: October 19, 2021, 07:05:34 pm »
You're correct, the circuit converts the 3.3V PWM signal from the MCU to 2.5V set by the tl431. It's filtered to give 0V to 2.5V and amplified by a gain of 2, to give 0 to 5V out.

I had a brainfart. It should have had a gain of 4, to get 0 to 10V out. For a gain of 4, R5 needs to be 3 times the value of R4, so you could use 100k & 300k, 110k, & 330k or, 120k & 360k. To be picky, the TL431 gives slightly less than 2.5V, 2.495V, requiring a slightly higher gain than 4, so use 66k5 & 200k, or 56k2 & 169k if you want precision, but a gain of 4 will be good enough for most applications.
 
The following users thanked this post: sofa-king

Offline sofa-kingTopic starter

  • Contributor
  • Posts: 11
  • Country: us
Re: PWM to analog 10v. What have I done wrong?
« Reply #20 on: October 20, 2021, 08:19:37 pm »
Thanks Zero999! I appreciate your comments. After studying the tl431 I figured out that with the reference tied to the anode the unit outputs that ~2.5v. I really like this solution and have ordered up some pcb's to give it a try.

On a side note, I'm curious if you have any method or tools that help you to find resistors values for a particular gain value? I seem to remember there was an online tool that would give suggestions for standard resistor values for a particular gain. My current method for non-trivial gains is to just choose one value and then go searching (on digikey etc) for a resistor with a close to the needed value for the other resistor. And kind of repeat until I find something that will work.

Anyhow thanks again! I really appreciate all your help.
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19491
  • Country: gb
  • 0999
 
The following users thanked this post: sofa-king

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3035
  • Country: us
Re: PWM to analog 10v. What have I done wrong?
« Reply #22 on: October 23, 2021, 01:13:59 am »
Here is Microchip's app note on "Using PWM to Generate an Analog Output":

App Note TB3250:

http://ww1.microchip.com/downloads/en/Appnotes/90003250A.pdf
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 19491
  • Country: gb
  • 0999
Re: PWM to analog 10v. What have I done wrong?
« Reply #23 on: October 23, 2021, 08:04:34 pm »
Here is Microchip's app note on "Using PWM to Generate an Analog Output":

App Note TB3250:

http://ww1.microchip.com/downloads/en/Appnotes/90003250A.pdf
Unfortunately, it's not much help to the original poster. It relies on the MCU having a tightly regulated power supply and the OP requires 10V out. One of their examples shows a 40M resistor and 1pF capacitor, which are silly values, in most applications!

For completeness, here's the Sallen-Key filter I hinted on earlier. It was designed with the help of the tool linked below. It accepts a 2.5V waveform from the TL431 + analogue switch, shown above.

G = 4
FC = 100Hz
ζ = 1.414
http://sim.okawa-denshi.jp/en/OPstool.php

The value given for R1 is 24k, but R5  and C3 have been added as a pre-filter, to get rid of very high frequencies, as Sallen-Key filters fail at high frequencies, as the op-amp runs out of gain. C4 is also added to reduce the gain at high frequencies.

It might make more sense to use a gain of one and a separate gain stage, as the LM358 is a dual op-amp. I'd probably put the gain stage first, which will also perform some pre-filtering, due to the op-amp's roll-off. I just thought it would be fun to try a Sallen-Key filter with gain.

EDIT:
Whoops, the pre-filter in the above design has a lower cut-off than the active filter. Not that it matters in this application.

And here's the design with separate amplifier and filter stages. It looks more complicated, but it's not.

I used the same tool and numbers to design it, as above.

The calculator gave:
R1 = 22kΩ
R2 = 7.5kΩ
C1 = 0.1uF
C2 = 0.15uF

To reduce the number of resistor values in the circuit, thus optimising the parts list, I divided the capacitor values by 1.5 and multiplied the resistor values by 1.5.

R1 = 22kΩ ✕ 1.5 = 33k
R2 = 7.5kΩ ✕ 1.5 = 11k25, the nearest E24 value is 11k
C1 = 0.1uF / 1.5 = 66⅔nF, the nearest E6 value is 68nF
C2 = 0.15uF / 1.5 = 100nF

« Last Edit: October 24, 2021, 08:27:27 am by Zero999 »
 
The following users thanked this post: Ian.M, ledtester, sofa-king

Offline sofa-kingTopic starter

  • Contributor
  • Posts: 11
  • Country: us
Re: PWM to analog 10v. What have I done wrong?
« Reply #24 on: October 29, 2021, 10:01:13 am »
Thank you again Zero! I've had a bit of fun playing with your .asc file. I haven't really used spice successfully before but your file helped me to understand it. I've spend the last few days running various simulations for circuits I use like I'm addicted to a video game. LOL but it's been quite the learning experience. I'm going to give this latest circuit a go. I modeled both and I can see that there is much less ripple on the output of this latest circuit. Thank you so much for your help! You've helped me learn a bunch.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf