Does anyone on here know if the capacitor-resistor combo attached to a pwm output pin of an Arduino gives a decent analog output?
People replied a lot while I was sleeping. I''m not going to give the same response to everyone who was asking what its for, so I'll just say it here. I want to use an arduino to spoof the 0-5V analog throttle signal on this ebike i'm building.
@MLE_online for throttle? yeah homie you could throw just about any RC combination at a slow-moving signal like that. If it's too laggy, lower the RC constant.

@MLE_online

Oh! Then absolutely yes, you can RC filter that. The pwm frequency is 400hz on the old ones, and gou want something that moves at the speed of s human hand, like half a hertz max. A half second RC would hit the barn door.

10 uF and 47K ohms is a half second.

1000 Hz would be many many dB down. Its knowable.

@MLE_online

Oops I meant 2hz max and that RC reflects that.

@MLE_online
Might I suggest a dead-man switch of some sort?
@JohnS_AZ The dead man switch is when I fly off
@MLE_online @JohnS_AZ What's the airspeed velocity of an unladen Emily?
@farbenstau @MLE_online
I think impact velocity is more to the point. <g>
@MLE_online depends on the purpose... If you want is to be smooth it cant change fast.
@RueNahcMohr how fast or slow?

@MLE_online

Start with the R C time being 10X the frequency. You can't really get all the bumps out without turning it into DC.

How good does it need to be? What's the application?

R C time is microfarads times megohms. for time in seconds. Rule of thumb.

@RueNahcMohr

@MLE_online @RueNahcMohr what arduino do you have? They have different PWM speeds. And what is your acceptable ripple in your output?

Slowest pwm is 490hz. If you filter that with a 1k resistor and a 10uf capacitor, that’s a cutoff of about 16hz. So you could have a control signal of about that. It would have a ripple of about [edit: 0.25] volts on the output

Edit: I accidentally ran the calculation at the wrong PWM speed. The slower speed gives you .25 volts ripple, the faster gives you 0.12v

@MLE_online @RueNahcMohr additional info: you can software bit bang a faster pwm, there are arduino libraries

Also, you can use this calculator for PWM. It has been around for decades and I use it all of the time. Just plug in the pwm frequency, and the resistor and capacitor values and it will come back with cutoff frequency, and output ripple, and even graph it over time

http://sim.okawa-denshi.jp/en/PWMtool.php

(Sample)RC Low-pass Filter Design for PWM - Result -

@MLE_online thats set by the RC combination you use. Are you gonna tell what you want to use it for?

@RueNahcMohr @MLE_online

If the pwm is at 100kHz and you are trying to generate a sub 100hz tone, it might be fine. If you want full bandwidth audio to 20kHz, nope.
https://www.allaboutcircuits.com/technical-articles/low-pass-filter-a-pwm-signal-into-an-analog-voltage/

@poleguy @MLE_online

additionally, if you need steady more than resolution, an R-2R DAC is easy to make.

@MLE_online Low pass filtering PWM to get analog voltage always comes with 3 unavoidable problems. Whether you consider the result "decent" depends on how low your expectations are.
@MLE_online Problem 1 is output impedance. The signal is "weak" because of the resistor. If you will drive something very high impedance like an opamp input, maybe it's a non-issue. If you want a strong signal, you'll need to add an opamp or other way to buffer the output.
@MLE_online Problem 2 is noise on the power supply couples to your signal. With a proper DAC you would look for a PSRR (Power Supply Rejection Ratio) spec or graph to give an idea of how much noise can couple from power to signal. You might power the DAC with separate "clean" power. DACs use a reference voltage too, which is very stable. With low pass filtered PWM, any lower frequency noise on your "digital" power supply goes straight into your signal. Pretty much the worst PSRR scenario!
@MLE_online and of course Problem 3 is trade off between slow response versus some of the PWM frequency remaining. Here's documentation for Teensy to give a quick visual idea, but it applies to Arduino or any other PWM output.
@PaulStoffregen @MLE_online Very interesting! And if you chose to do PWM anyhow, maybe even for audio: What would best practices? Output buffer, ...?
@mixal @PaulStoffregen @MLE_online PWM is not terrible and when combined with high/low-side switching with power transistors can be used to very efficiently provide high-power analog signals to inductive or capacitive loads (look up Class D power amplifiers). I would pick a PWM frequency much higher than the signal bandwidth and do some proper filter calculations/filtering however. The resolution of the duty cycle in your microcontroller becomes (an upper bound on) your sample resolution
@mixal @MLE_online Normally the best practice is to use PWM to directly drive your lights or motor or loudspeaker or whatever other physical thing ultimately matters, which can naturally "ignore" the PWM's high carrier frequency. Perhaps use minimal filtering like ferrite beads, only for the sake of limiting radio noise you spew into the world. For audio driving a speaker, usually use a dedicated power supply to minimize the PSRR problem.

@MLE_online I'm gonna some assumptions and say hell yes.

Whatcha cooking up this time?

@ArchiteuthisFlux I have an idea I want to try on this new ebike project. The motor controllers for these things have nice throttle response, but the pedal assist sensor response is just on-off. I was thinking I would try using an arduino to read the speed the pedals are turning from the pedal assist sensor and then generate a 0-5V throttle signal.

I want to see if I can make it so the bike provides an amount of throttle that corresponds to how much pedaling you're doing

@MLE_online Oh sick. I always thought it was weird that ebikes didn't work that way by default.

Like @stargirl said, a throttle signal like that is the least demanding analog output ever and could be done on pure vibes. I would put a little pot on it that you multiply with the pedal signal in the analogWrite() and then just twiddle it while riding until it feels right.
Because doing math is for nerds.

@ArchiteuthisFlux @stargirl I was thinking exactly something like that!

I don't understand why they don't work that way. They clearly can provide a variable amount of throttle, but someone at some point decided that the pedal assist shouldn't work like that and then every factory in China that makes these controllers did it the exact same way.

@MLE_online @stargirl I wonder if that has something to do with the ridiculous number of laws around ebikes in some places.

"God forbid people just move the throttle and it goes, they must also be perfomatively moving their feet in circles for some reason."

@ArchiteuthisFlux @stargirl maybe? just pretend to be doing something with your feet and you get to go full speed
@MLE_online You'll get lots of answers since it depends on the application. However, I'd simply say no.
https://amzn.to/47B1jyf
Amazon.com: WWZMDiB 2 Pcs MCP4725 DAC Converter Module 12-bit I2C IIC Compatible with for Arduino Raspberry Pi ESP32 STM Digital to Analog Converter Board : Electronics

Amazon.com: WWZMDiB 2 Pcs MCP4725 DAC Converter Module 12-bit I2C IIC Compatible with for Arduino Raspberry Pi ESP32 STM Digital to Analog Converter Board : Electronics

@JohnS_AZ I don't buy from Amazon anymore, so I'll have to pass on that
Adafruit MCP4728 Quad DAC with EEPROM

If you've ever said to yourself, "Gee, I wish I had four 12-bit DACs that came in a single package with the ability to save their settings to an EEPROM", well I have good news. ...

@JohnS_AZ too expensive! Everyone is saying the RC network should be ok, so I'm going to try that first
@MLE_online For some value of decent, sure. This is a very common way of doing a crude DAC. As long as the other side isn’t sensitive to the switching noise, and you don’t need to switch it any faster than the filter will let you, you’ll be fine.
@MLE_online I've used it for various terrible purposes, including workable audio.