PWM to analog 10v. What have I done wrong?  - Page 1
Forum section: electronics / beginners

author: sofa-king
author_rank: Contributor
timestamp: September 29, 2021, 03:28:12 am
content: 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.

author: timenutgoblin
author_rank: Regular Contributor
timestamp: September 29, 2021, 04:31:31 am
content: 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.

author: timenutgoblin
author_rank: Regular Contributor
timestamp: September 29, 2021, 05:36:11 am
content: A better solution would be to use a CMOS inverter such as CD4069.https://www.ti.com/lit/ds/symlink/cd4069ub.pdfPower 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.

author: perieanuo
author_rank: Frequent Contributor
timestamp: September 29, 2021, 06:18:44 am
content: hi,have you seen thishttps://forum.arduino.cc/t/0-10v-pwm-2-solution/349017/17?the pwm logic is kinda inverted, but you'll figure out in software easilythe opamp approach ishttps://arduinodiy.wordpress.com/2019/09/28/getting-i2c-regulated-0-10volt-out-of-an-arduino-or-esp8266/or betterhttps://electronics.stackexchange.com/questions/432844/i-want-to-create-a-stable-0-10-v-dc-output-from-a-3-3v-pwm-signal-arduino-esp82just some observation in your schematic: i rarely put pull-down when i control bjt from ucontrollers, in fact never

author: Kleinstein
author_rank: Super Contributor
timestamp: September 29, 2021, 07:37:59 am
content: Quote from: perieanuo on September 29, 2021, 06:18:44 amThe 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.

author: dietert1
author_rank: Super Contributor
timestamp: September 29, 2021, 08:27:50 am
content: 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

author: perieanuo
author_rank: Frequent Contributor
timestamp: October 01, 2021, 07:48:34 am
content: Quote from: Kleinstein on September 29, 2021, 07:37:59 amof 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

author: Badwolf
author_rank: Contributor
timestamp: October 01, 2021, 05:46:56 pm
content: Quote from: Kleinstein on September 29, 2021, 07:37:59 amThese 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

author: Zero999
author_rank: Super Contributor
timestamp: October 01, 2021, 09:38:50 pm
content: 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.

author: sofa-king
author_rank: Contributor
timestamp: October 13, 2021, 10:14:12 pm
content: 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.

author: Capernicus
author_rank: Frequent Contributor
timestamp: October 13, 2021, 10:25:07 pm
content: 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.

author: Zero999
author_rank: Super Contributor
timestamp: October 14, 2021, 08:25:28 am
content: Quote from: sofa-king on October 13, 2021, 10:14:12 pmYou 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.

author: Capernicus
author_rank: Frequent Contributor
timestamp: October 14, 2021, 01:11:30 pm
content: 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.

author: Capernicus
author_rank: Frequent Contributor
timestamp: October 14, 2021, 02:06:45 pm
content: 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.

author: Zero999
author_rank: Super Contributor
timestamp: October 15, 2021, 08:31:42 am
content: Quote from: Capernicus on October 14, 2021, 01:11:30 pmQuote from: Capernicus on October 14, 2021, 02:06:45 pmPlease post a schematic.

author: Ian.M
author_rank: Super Contributor
timestamp: October 15, 2021, 09:57:15 am
content: Quote from: Zero999 on October 15, 2021, 08:31:42 amYou asked *Capernicus* for a schematic?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.Might as well ask an 'over unity' wonk for 'free energy' generator plans, which at least will be more entertaining to debunk . . .

author: Zero999
author_rank: Super Contributor
timestamp: October 15, 2021, 10:39:15 am
content: Quote from: Ian.M on October 15, 2021, 09:57:15 amCome 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.

author: sofa-king
author_rank: Contributor
timestamp: October 18, 2021, 09:14:33 pm
content: 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?

author: dietert1
author_rank: Super Contributor
timestamp: October 18, 2021, 10:01:13 pm
content: 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

author: Zero999
author_rank: Super Contributor
timestamp: October 19, 2021, 07:05:34 pm
content: 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.

author: sofa-king
author_rank: Contributor
timestamp: October 20, 2021, 08:19:37 pm
content: 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.

author: Zero999
author_rank: Super Contributor
timestamp: October 20, 2021, 08:47:03 pm
content: Here's a list of the resistor calculator websites I often use:https://jansson.us/resistors.htmlhttp://sim.okawa-denshi.jp/en/teikokeisan.htmhttps://www.random-science-tools.com/electronics/divider.htmhttp://earmark.net/gesr/opamp/

author: ledtester
author_rank: Super Contributor
timestamp: October 23, 2021, 01:13:59 am
content: 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

author: Zero999
author_rank: Super Contributor
timestamp: October 23, 2021, 08:04:34 pm
content: Quote from: ledtester on October 23, 2021, 01:13:59 amUnfortunately, 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 = 4FC= 100Hzζ = 1.414http://sim.okawa-denshi.jp/en/OPstool.phpThe 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.1uFC2 = 0.15uFTo 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 = 33kR2 = 7.5kΩ ✕ 1.5 = 11k25, the nearest E24 value is 11kC1 = 0.1uF / 1.5 = 66⅔nF, the nearest E6 value is 68nFC2 = 0.15uF / 1.5 = 100nF

author: sofa-king
author_rank: Contributor
timestamp: October 29, 2021, 10:01:13 am
content: 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.

author: Zero999
author_rank: Super Contributor
timestamp: October 29, 2021, 04:48:42 pm
content: Quote from: sofa-king on October 29, 2021, 10:01:13 amBeware, simulators can lie. Some models aren't 100% accurate, especially things such as the power consumption and output stage of op-amps. It's often very easy to tweak a circuit so it works in the simulator, but fails in real life. It's always a good idea to build a prototype first.

author: sofa-king
author_rank: Contributor
timestamp: December 03, 2021, 11:45:20 am
content: Zero999, I just wanted to thank you again for your help! I ended up running into an issue with the last circuit you provided. It worked great but wouldn't go below .25v. My uneducated guess is there was some load causing it not to be able to go to the low rail. None the less, I basically removed some parts and added some jumpers so that it (almost) matched the circuit you gave before the last one and it works 100% perfectly. Let me know if there is some way I can buy you a beer my friend!

author: Zero999
author_rank: Super Contributor
timestamp: December 04, 2021, 10:18:09 pm
content: Quote from: sofa-king on December 03, 2021, 11:45:20 amThe LM358 can't really sink any current and stay near the negative rail, but none of the circuits I posted get the op-amp to sink and current, unless a load is connected between the output and +VIn what way did the circuit you build differ with the schematic I posted?

author: sofa-king
author_rank: Contributor
timestamp: December 06, 2021, 07:13:45 am
content: The op-amp I used was a MC33202DR2G.From the last schematic you provided, I basically jumpered R1 and R2 and removed C1 and C2. So that the second amp is just a follower. It's exceeded my expectations. Though I figure I'd add in the 2nd and 3rd order filters in the next attempt.

author: Zero999
author_rank: Super Contributor
timestamp: December 06, 2021, 09:24:24 am
content: Quote from: sofa-king on December 06, 2021, 07:13:45 amThe filter was probably oscillating. Fortunately a simple passive RC filter was good enough, in this case.