Outputs

PWM dimming: fade an LED with Arduino

A digital pin can only be on or off. Pulse-width modulation switches it so quickly that your eye sees an average brightness instead of a flicker.

22 November 20266 min read
Arduino and breadboard showing an LED with a series resistor connected to a PWM-capable pin
An LED and its 220 Ω series resistor wired to a PWM output on the control board.

Bu projeyi gerçekten kurmak istersen parçaları hobidevre.com'dan temin edebilirsin →

Build it for real

To build this project for real, get the parts from hobidevre.com → hobidevre.com →

More build guides: hobidevre.com/blog

What duty cycle means

Imagine a switch turning on and off 490 times per second. If it stays on for half of each cycle and off for the other half, the load receives half the energy. That fraction is the duty cycle: 0% is always off, 100% is always on, and 50% is half on, half off.

Arduino's analogWrite(pin, value) accepts a value from 0 to 255. Internally, 255 maps to a fully on cycle and 0 to always off, so the duty cycle is simply value divided by 255. Writing 128 gives about 50%.

Which pins can dim an LED

On an Arduino Uno, only pins marked with a tilde support PWM: pins 3, 5, 6, 9, 10 and 11. Pins 5 and 6 run at roughly 980 Hz; the others run near 490 Hz. Calling analogWrite() on a non-PWM pin does not produce a fade — you will get either fully on or fully off depending on the value.

Other boards differ: an ESP32, for example, exposes PWM on many more pins through a different API. Always check the pinout of the board you are actually using.

The circuit

  • Connect the LED anode (long leg) to digital pin 9 through a 220 Ω resistor.
  • Connect the cathode (short leg) to GND.
  • Set pin 9 as an output in setup(), although analogWrite() handles this on most cores.

The resistor still limits peak current. PWM does not remove the need for it: during the on part of each cycle the full 5 V is applied to the LED-resistor pair.

A fade sketch

The loop below walks the duty cycle up and down. Changing the delay changes the fade speed without changing the electrical behaviour.

const int ledPin = 9;
int seviye = 0;
int yon = 5;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  analogWrite(ledPin, seviye);
  seviye += yon;
  if (seviye <= 0 || seviye >= 255) yon = -yon;
  delay(20);
}

The variable names are descriptive but arbitrary; the logic is what matters. At 20 ms per step, a full sweep from 0 to 255 takes about five seconds.

Why the fade looks uneven

Human brightness perception is roughly logarithmic. Increasing the duty cycle from 10% to 20% looks like a bigger change than going from 80% to 90%, even though both add the same electrical step. If you want a smooth visual fade, map a curve to the value instead of using a straight line: values such as a squared or lookup-based ramp spread the visible change more evenly.

Cameras can reveal the underlying pulses as bands, because their shutter samples a small slice of each cycle. That is a capture artefact, not a circuit fault.

Mistakes to avoid

  • Using a pin without PWM support and wondering why nothing fades.
  • Removing the resistor because "the voltage is lower" — the pulses are still full height.
  • Expecting analogWrite to produce a real analogue voltage; there is no digital-to-analogue converter involved.
  • Driving a motor or a high-current load directly from a pin; use a transistor or driver stage instead.

Get the parts

If you want to build this project in real life, you can get the parts from hobidevre.com.

Frequently asked questions

Is PWM the same as an analogue output?

No. It is a digital signal switched fast enough that the average looks analogue to a slow load such as an LED.

Why does my LED flicker on camera?

The camera shutter samples individual pulses. Slow the switching or use a longer exposure to reduce banding.

Can I dim a servo or a motor this way?

A servo expects a specific 50 Hz pulse width, not a generic duty cycle. A DC motor can be speed-controlled with PWM through a suitable driver.

Related reading

Practise in the browser

Open the same layout in the simulator and follow the steps. Sign in to save your work.