Pro Mini: Fade, PWM and an RC filter
6 parts
analogWrite on D9 fades an LED up and down, and a 10 kΩ / 2.2µF filter turns the same PWM into a smooth voltage. D9 is a Timer1 pin: analogWrite gives it a 490.2 Hz square wave (16 MHz ÷ 64 ÷ 510), 2.04 ms a period. The sketch steps the duty by 5/255 every 30 ms, from 0 to 255 and back, so one fade takes 3.06 s. On the scope, D9 switches between 0 V and 4.78 V (zoom in to see each period). The filter's output follows the duty: about 2.38 V at half duty with about 0.11 V of ripple, 4.76 V at the top of each fade and almost 0 V at the bottom. A larger capacitor would ripple less and follow the fade more slowly; the filter's time constant here is 22 ms.
- U1 s3.hex
- V1 5V
- R1 330Ω
- LED1
- R2 10kΩ
- C1 2.2µF