Output 0V to 5V with Arduino

Hello, I am doing a project and I need to output 0V to 5V using Arduino. How I can do this with the analogWrite feature?

Thanks for the help

Use analogWrite(pin, val); and use a low pass filter to turn the PWM to a voltage.

The Arduino Uno has 6 pins (pins 3,5,6,9,10,11) that can provide pwm output (explained below - https://www.arduino.cc/en/Tutorial/Foundations/PWM)

For true analog output you will need to you use an DAC ( Digital to Analog Converter)...
The Arduino Due has two DACs but it runs at 3.3v... They are at Due pins DAC0 and DAC1

You cannot get pure analog signal from arduino output, only pulse-width modulated (PWM) signal. PWM gives an average voltage value from 0 to 5 V. This is enough to drive LEDs or DC motors. You may try use low pass filter as mentioned above by @missdrew.
If you want a clean analog signal, you should use an external DAC.

Sorry, what do you actually mean by that? :face_with_raised_eyebrow:

This is pretty much the "XY Problem"

analogWrite() can do 256 levels, from 0 to 255.
Here are 0,25,50,75,100,125,150,175,200,225,250 then back to 0, with each level left on for the same time period


Output was a simple RC filter, with no load after it.

Thanks this will work however I am not very expert how do I choose the resistance value and the capacitor value for the low pass filter?

Thanks, so will this work with every voltage.? For instance, if I use analogWrite(PIN, 255) will it output a constant and usable 5v? And for instance, if I use analogWrite(PIN, 155) will it output a constant and usable 3v?

Thanks

Yes, for loads up to around 20mA

It outputs 1/256 of the MCU supply voltage for every A/D step.
So if it will output 0-5volt depends on which Arduino, and how you power it.

If you load that output (draw current from it), then the voltage drops.
Because the load and the 10k resistor creates a voltage divider.
So if you add a 10k resistor, that 0-5volt becomes 0-2.5volt.

If you don't want that, then you should buffer that voltage with an opamp.
Leo..

You can also try a smaller value resistor, like 1K, if you have a 'scope to look at the waveform and see if it still meets your needs.

You can try any online calculator, for example
Low Pass Filter Calculator
RC Low-pass Filter Design Tool
Keep in mind, that getting constant voltage from Arduino by PWM is not so trivial.
You didn't even say what is your requirements to output DC voltage?
What will be the load?
What allowable voltage ripple level you need?

Thanks, I still confused about two things: For Arduino what is the "Cutoff Frequency Desired"?

Basically, this voltage needs to be read by the ECU of my car. I searched online and I found a complete schematic that uses a two-stage low pass filter. However, I am doing this project to learn and I would like to learn the electronics behind this.

In fact, the second thing that confuses me is what is a two-stage low pass filter?

By the way, I will include a photo of the schematic I found, I will appreciate anyone how can share how this works and why there are two low pass filters in series.

Thanks

image

Any electronics textbook will explain how passive filters work, and of course there are countless tutorials on line. Top hit for "low pass filter tutorial" on Google.

Thanks, I am still confused about what is and why a two-stage low pass filter. I can not find this online.

Ask the author of the article where you found that schematic why they chose that one.

As for how it works, now would be a good time to actually click on the link in reply #14, and scroll down to the heading Second-order Low Pass Filter, where that exact circuit is explained in detail.

Of course, it would be helpful to read the information in the article preceding that heading.

2 Likes

You still haven't clarified the requirements.
What is the input impedance of the electronic car unit?
What ripple of DC voltage is allowed?
How quickly do you need to change the DC voltage?
The PWM output voltage can have high ripple. If you suppress ripple with a low-pass filter, you limit the rate of change in the signal.
contenteetimes-images-01mdunn-di-di5541f1_cr

I simulated a similar task for you, but with slightly worse conditions (PWM frequency 50Hz, Rload = 50kΩ) to better see the ripple. The filter capacitor is 10 uF, the filter resistor is 4.7 kΩ and 10 kΩ. PWM duty cycle 80%, the output should be 4V.
It can be seen from the graphs that at 4.7 kΩ the voltage rise rate is higher, the error is less, but the ripple is greater; at 10 kΩ, the ripple is less, but the voltage rise rate is less and the error is larger.

P.S. If all of this not for you, I would suggest to use an external DAC like the 12-bit MCP4725. It has significantly higher accuracy and a high slew rate (6 μs!).