1

I'm converting a 0-5v PWM signal to an analog signal using an LM358D. I've connected the output to an oscilloscope to look at the signal but I'm not really sure what I'm looking at. The PWM looks ok but I'm unsure of the analog signal.

Here's some images, please let me know what you think.

Both pics are at 50% signal strength. The oscilloscope is set to defaults, simply connected the probe.

Thanks

Text

Text

EDIT:

Here's the schematic for the PWM to analog converter. The cap is a tantalum. The dip switch changes the analog from 5v to 10v, the signal looks the same on both and is correct voltage so seems to be ok?

Text

4
  • 2
    What are you expecting to see? That looks like a 50% PWM duty cycle so are you expecting 2.5V on the output (as a straight line)?
    – Nick Gammon
    Mar 16, 2021 at 4:28
  • 1
    The voltages check out but I'm not sure of the analog signal. When I see other images the line is smooth curve at top and bottom but I don't know if that's a setting on scope or I have a bad signal? If the triangles look good I'm happy.
    – tryingHard
    Mar 16, 2021 at 5:28
  • You need a resistor and capacitor to smooth the signal. Do you have those? Perhaps edit your post to show your schematic.
    – Nick Gammon
    Mar 16, 2021 at 6:16
  • What do you mean with "smooth"? the signal has an amplitude of 584mV according to the measurements of your scope. Is that smooth enough? If you want to compare the image of the signal, you need to set the same y-div setting as in the reference image
    – chrisl
    Mar 16, 2021 at 7:01

2 Answers 2

1

Something like this should work:

PWM to analog circuit

With this code on a Uno:

void setup () { }

void loop () {
  analogWrite (3, analogRead (A0) / 4);
}

For slightly more details see my post about doing that.

3
  • Thanks, that's actually the same I'm using and guess what, I got the original from you. ) I knew your name looked familiar. I'm using a tantalum 10uf cap but resistor is only 220 ohm and I have an extra resistors someone said to add, I'll add the schematic.
    – tryingHard
    Mar 18, 2021 at 12:17
  • You have a much lower resistor (220Ω vs 4700Ω) than I did. The higher value resistor will charge the capacitor more slowly, thus smoothing out the signal and effectively giving you an average, which is what I assume you are trying to do.
    – Nick Gammon
    Mar 19, 2021 at 10:24
  • My op-amp was configured for unity gain, I don't know why you need those extra components.
    – Nick Gammon
    Mar 19, 2021 at 10:25
1

The PWM looks ok but I'm unsure of the analog signal.

The “analog signal” looks like the PWM smoothed by a first-order low-pass filter. It still has some oscillations, 584 mV peak-to-peak. If you can tolerate that, then it's perfectly fine. If not, then increase the time constant of your filter.

A first-order low-pass filter behaves, in its stop band, like an integrator. Thus, a square signal is turned into a triangle. You are seeing exactly the expected behavior.

The oscilloscope is set to defaults

There are no “default” settings in the oscilloscope. You probably used the “autoset” button instead. This button tries to find some settings that let you see something on the screen. I mean, some kind of signal, not just straight line. If the signal is almost flat, the autoset will increase the gain until you can see it is not flat. In this case, it increased the gain by a factor 10 (200 mV / division instead of 2 V used previously). It also added a negative offset in order to avoid the trace going out of the screen. See the small pentagon on the far left with a “1” inside? On the first picture this tells you where the zero (the reference potential) is relative to your signal. On the second picture, the polygon pointing at the bottom of the chart tells you the zero is off-scale.

If you want to really appreciate the smoothing action of the filter, you have to look at its input (the PWM signal) and its output (the “analog” signal) using the exact same settings. Connect the input, hit “autoset”, and look. Then connect the output, do not change the settings (do not hit “autoset”) and look at the output.

Better yet: forget the autoset button, adjust the settings manually and mind their values.

Better yet: as you have a two channel scope, display both signals simultaneously, using the exact same settings on both channels.

5
  • Thank-you for the detailed reply, I appreciate it! I updated the original post with schematic. What's an "acceptable" peak-to-peak? If a resistor or cap would improve that would be good. You can see I have PWM freq at 1000 Khz, is there a "magic" number? I use pca9685. I couldn't see any difference on scope, only PWM. My scope does have a "default" button and "autoset", latter was useless as it zoomed out so I manually set to view, I did use same settings for both signals just had to change voltage scale so I could view better. Thanks!
    – tryingHard
    Mar 18, 2021 at 12:34
  • @tryingHard: 1. It is up to you to decide what you would consider “acceptable”. It's your application! 2. If you want less residual oscillations, increase the value of R78. 3. The PWM frequency is 1 kHz, not 1000 kHz. 4. There is no magic. Mar 18, 2021 at 13:42
  • Thanks, impossible for me to know what is acceptable when I don't know what the number means, not something you can understand after an hour of reading. Maybe 500mV isn't bad or maybe it's horrible. I will see what happens with R78 and thanks for clarifying frequency.
    – tryingHard
    Mar 19, 2021 at 0:15
  • @tryingHard: Re “acceptable”: it depends on your application, on what you want to do with that signal. “Acceptable” means “acceptable by whoever will be the consumer of the signal”. Mar 19, 2021 at 8:07
  • I hear you, it's for LED drivers and aquarium power heads.
    – tryingHard
    Mar 19, 2021 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.