Here we will discuss the operation of pulse-width-modulation to DC conversion and use the idea to construct a variable Arduino based DC power supply.
Fig. 1 illustrates using a LM358 in conjunction with a low-pass
filter (10uF cap, 4.7K resistor) to produce a 0-5V output proportional to the duty cycle from DP11. The 10K potentiometer connected analog pin 0 is read, divided by 4, then written with analogWrite() to digital pin 11. The reason we divided the ADC value by 4 is because the PWM as used with Arduino is 8-bit and not 10-bit. This same circuit works with a Microchip PIC as 10-bit with better resolution. The resolution is 5V / 255 = 19.61mV per step.
Fig. 2 show using the other half of the LM358 as a voltage amplifier whose gain is based in 1 + R2/R1. With the 10K potentiometer connected between output pin 7 and fed back to the inverted input we can adjust for a gain of 1 to 2. The output with a 12-volt supply is 0-10V or 2-volts below Vcc.
In Fig. 3 we take the same circuit in Fig 2 but use a single LM358 as a voltage amplifier, boost Vcc to 24-volts, and change the 10K to 50K giving a gain of 1 to 6. We have also added a TIP41 NPN power transistor to boost output current. First adjust the 10K pot on ADC0 for 5-volts at TP1, then adjust R2 for a maximum voltage out between 5 and 20-volts. While this is showing an Arduino NANO it will work with any Arduino.
Note the YouTube video. Arduino code can be found at http://www.bristolwatch.com/ele2/dac.htm