Arduino PWM to Analog RC Filter: Wiring, Code and Simulator

To build this Arduino PWM to Analog RC Filter project you need an Arduino Uno and a 10 kΩ resistor, a 10 µF electrolytic capacitor, a potentiometer and an oscilloscope. analogWrite on pin 9 through a 10 kΩ / 10 µF low-pass filter becomes a steady voltage; A0 reads it back, the scope shows the PWM and the smoothed result. Make the 10 connections in the wiring table, upload the Arduino C++ code, and turn the pot: the duty cycle changes, CH2 settles at duty × 5 V and A0 agrees within a few mV. Change C to 1 µ to see the ripple come back.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
110 kΩ resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
110 µF electrolytic capacitorA large, polarised capacitor for smoothing supplies: the stripe marks − . Reverse it, or exceed its voltage rating, and it fails.
1PotentiometerVCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023.
1OscilloscopeTwo channels against earth (GND). Turn TIME/DIV and VOLTS/DIV to fit the wave, set the trigger level to steady it, and read Vpp, RMS and frequency. X-Y mode draws Lissajous figures. Choose a digital (DSO) or phosphor (CRO) screen.

Wiring

#FromTo
1Arduino Uno pin 910 kΩ resistor leg 1
210 kΩ resistor leg 210 µF electrolytic capacitor +
3Arduino Uno GND10 µF electrolytic capacitor -
4Arduino Uno A010 kΩ resistor leg 2
5Arduino Uno 5VPotentiometer VCC
6Arduino Uno GNDPotentiometer GND
7Arduino Uno A1Potentiometer SIG
8Arduino Uno pin 9Oscilloscope CH1
910 kΩ resistor leg 2Oscilloscope CH2
10Arduino Uno GNDOscilloscope GND

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a 10 kΩ resistor, a 10 µF electrolytic capacitor, a potentiometer and an oscilloscope from the parts palette.
  3. Wire 1. Connect Arduino Uno pin 9 to 10 kΩ resistor leg 1.
  4. Wire 2. Connect 10 kΩ resistor leg 2 to 10 µF electrolytic capacitor +.
  5. Wire 3. Connect Arduino Uno GND to 10 µF electrolytic capacitor -.
  6. Wire 4. Connect Arduino Uno A0 to 10 kΩ resistor leg 2.
  7. Wire 5. Connect Arduino Uno 5V to Potentiometer VCC.
  8. Wire 6. Connect Arduino Uno GND to Potentiometer GND.
  9. Wire 7. Connect Arduino Uno A1 to Potentiometer SIG.
  10. Wire 8. Connect Arduino Uno pin 9 to Oscilloscope CH1.
  11. Wire 9. Connect 10 kΩ resistor leg 2 to Oscilloscope CH2.
  12. Wire 10. Connect Arduino Uno GND to Oscilloscope GND.
  13. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  14. Run it. Press Run. Turn the pot: the duty cycle changes, CH2 settles at duty × 5 V and A0 agrees within a few mV. Change C to 1 µ to see the ripple come back.

Arduino C++ code

// PWM to analog: an RC low-pass filter turns pin 9's PWM into a steady voltage.
//   pin 9 -- 10 kOhm --+-- A0
//                      |
//                    10 uF
//                      |
//                     GND
// The pot on A1 sets the duty cycle; A0 reads the filtered voltage back.
// Expect V(A0) = duty x 5 V, with a little ripple (tau = RC = 100 ms,
// far longer than the 2 ms PWM period).

void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop() {
  int knob = analogRead(A1);          // 0..1023
  int duty = knob / 4;                // 0..255
  analogWrite(9, duty);
  float expected = duty * 5.0 / 255.0;
  float measured = analogRead(A0) * 5.0 / 1023.0;
  Serial.print("duty ");
  Serial.print(duty * 100 / 255);
  Serial.print("%  expected ");
  Serial.print(expected, 2);
  Serial.print(" V  filtered A0 ");
  Serial.print(measured, 2);
  Serial.println(" V");
  delay(250);
}

What to try

Turn the pot: the duty cycle changes, CH2 settles at duty × 5 V and A0 agrees within a few mV. Change C to 1 µ to see the ripple come back.

Frequently asked questions

How do I wire the PWM to Analog RC Filter project on an Arduino Uno?

Make 10 connections: Arduino Uno pin 9 to 10 kΩ resistor leg 1; 10 kΩ resistor leg 2 to 10 µF electrolytic capacitor +; Arduino Uno GND to 10 µF electrolytic capacitor -; Arduino Uno A0 to 10 kΩ resistor leg 2; Arduino Uno 5V to Potentiometer VCC; Arduino Uno GND to Potentiometer GND; Arduino Uno A1 to Potentiometer SIG; Arduino Uno pin 9 to Oscilloscope CH1; 10 kΩ resistor leg 2 to Oscilloscope CH2; Arduino Uno GND to Oscilloscope GND.

What parts do I need for this Arduino project?

An Arduino Uno and a 10 kΩ resistor, a 10 µF electrolytic capacitor, a potentiometer and an oscilloscope.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

analogWrite on pin 9 through a 10 kΩ / 10 µF low-pass filter becomes a steady voltage; A0 reads it back, the scope shows the PWM and the smoothed result.

More Arduino Uno projects