Introduction: Arduino RC Circuit: PWM to Analog DC
Arduino is a platform that can be used to develop interactive objects. For this project we will use the the Arduino Mega 2560. It has 54 digital input/output pins, of which 15 can be used as pulse width modulation (PWM) outputs.
PWM allows the strength of the output to be varied. For example, to change the brightness of an LED. In this Instructable, an RC filter will be used to flatten the PWM signal. As we will see, the RC filter has some limitations due to a certain amount of residual "waviness" in the RC output.
Step 1: Pulse Width Modulation
PWM signals are rectangular waves, varying from zero to +5 V, with a frequency near 500 Hz. If the signal is high for 50% of the time, the voltage delivered will be 2.5 V.
But for some uses, it is preferable to have a flat analog DC signal at 2.5 volts, rather than a digital stream of square waves with a 50% duty cycle. One method to flatten the PWM is to use an RC (resistor/capacitor) filter.
Step 2: RC Filter
The RC filter is a simple circuit element used to convert a pulsed signal to a flatter signal. The key concept is that the capacitor shunts the varying voltage to ground, producing a DC voltage.
The RC filter has two values that determine how it will modulate the incoming PWM signal: R, the resistance in Ohms, and C, the capacitance in Farads. There are many tools online for exploring RC circuits. One that is useful is http://sim.okawa-denshi.jp/en/CRtool.php, from Okawa electronic design and manufacturing in Japan.
For the purposes of this project, I entered the values of 1 kohm for the resistor and 100 uF for the capacitor.
The RC analysis tool produces some key insights:
1) The cut-off frequency
2) The ripple
3) The settling time.
Step 4: Ripple and Time of Response to Change in Voltage
This image shows two things: the ripple in the RC output (the "waviness" of the line, which is small for these R and C values) and the response time. Note that the response is mostly complete by 0.2 seconds, which means that a change in voltage will result in a change in brightness in the LED in much less than a one second. You can put your own numbers in the RC calculator here: http://sim.okawa-denshi.jp/en/CRlowkeisan.htm.
Step 5: Frequency Analysis
The efficiency with which the RC filter blocks PWM signals depends on the frquency of the signal. For this RC filter (1 kohm/100uF), the top graph shows that the signal is reduced by 40 decibels* at 500 Hz.
*0.0001 power ratio; http://en.wikipedia.org/wiki/Decibel
Step 6: Arduino RC Circuit
The Arduino board produces a PWM signal from digital pin 10. The PWM signal passes through R1 and the capacitor (the RC components). The output from the RC filter passes through the second resistor (used to limit current to LED). The current flows through the LED to ground.
The signals before and after the RC filter can be seen by placing oscilloscope probes at the points indicated in the diagram.
This picture was made using Fritzing.
Example code to control the Arduino board and vary the PWM can be found on the Arduino website: http://arduino.cc/en/tutorial/fading. Change the digital input from pin 9 to pin 10.
Step 7: Scope: No Voltage
The scope shows two traces: the PWM input and the RC filter output. Here both are at baseline, 0 V.
Step 8: Scope: 50% Signal
The Arduino digital pin 10 is sending a PWM signal with 50% duty cycle (half time high, half time low). This is the rectangular wave.
The RC output is the blue flat line. Note the scale is set at 2 volts. Each horizontal line represents an increment of 2 volts. The PWM varies from 0 to 5 volts, whereas the RC output is steady near 2.5 volts.
Step 9: Scope: Full Power, 5 Volts
At full power of 5 volts, both the input and RC output are steady "on" at 5 V.
Step 10: Scope: a Closer Look at the RC Output
The scale was changed from 2 V to 0.2 V to zoom in on the RC output, Note the waviness of the blue line above the square wave - this is the RC output.
The values of R and C can be increased to further flatten the line, but then the response time increases. If the RC value (the product of the R and C values) is too high, there will be a noticeable lag in the change of the brightness of the LED as the voltage is changed. The values that produce the best compromise between response time and less ripple depend on the design goals.
Bottom line, the RC filter is not the answer if you need a clean DC signal. Alternatives: add second RC filter, digital-to-analog converter, digital potentiometer.