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
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | 10 kΩ resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | 10 µF electrolytic capacitor | A large, polarised capacitor for smoothing supplies: the stripe marks − . Reverse it, or exceed its voltage rating, and it fails. |
| 1 | Potentiometer | VCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023. |
| 1 | Oscilloscope | Two 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
| # | From | To |
|---|---|---|
| 1 | Arduino Uno pin 9 | 10 kΩ resistor leg 1 |
| 2 | 10 kΩ resistor leg 2 | 10 µF electrolytic capacitor + |
| 3 | Arduino Uno GND | 10 µF electrolytic capacitor - |
| 4 | Arduino Uno A0 | 10 kΩ resistor leg 2 |
| 5 | Arduino Uno 5V | Potentiometer VCC |
| 6 | Arduino Uno GND | Potentiometer GND |
| 7 | Arduino Uno A1 | Potentiometer SIG |
| 8 | Arduino Uno pin 9 | Oscilloscope CH1 |
| 9 | 10 kΩ resistor leg 2 | Oscilloscope CH2 |
| 10 | Arduino Uno GND | Oscilloscope GND |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add a 10 kΩ resistor, a 10 µF electrolytic capacitor, a potentiometer and an oscilloscope from the parts palette.
- Wire 1. Connect Arduino Uno pin 9 to 10 kΩ resistor leg 1.
- Wire 2. Connect 10 kΩ resistor leg 2 to 10 µF electrolytic capacitor +.
- Wire 3. Connect Arduino Uno GND to 10 µF electrolytic capacitor -.
- Wire 4. Connect Arduino Uno A0 to 10 kΩ resistor leg 2.
- Wire 5. Connect Arduino Uno 5V to Potentiometer VCC.
- Wire 6. Connect Arduino Uno GND to Potentiometer GND.
- Wire 7. Connect Arduino Uno A1 to Potentiometer SIG.
- Wire 8. Connect Arduino Uno pin 9 to Oscilloscope CH1.
- Wire 9. Connect 10 kΩ resistor leg 2 to Oscilloscope CH2.
- Wire 10. Connect Arduino Uno GND to Oscilloscope GND.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- 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
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator
- Arduino Push Button LED: Wiring, Code and Simulator