
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 PWM actually does
Pulse-width modulation, or PWM, is a way of producing an average effect with a digital output. The pin toggles between fully on and fully off many times per second. If it is on for a quarter of each cycle, the load receives roughly a quarter of the energy it would get from a constant high signal. The LED cannot follow the individual switches, so it appears dimmer.
Nothing here is truly analogue. There is no intermediate voltage being generated. The trick works because the load and your eye both respond to the average, provided the switching is fast enough.
Reading the 0–255 scale
On an Arduino Uno, analogWrite accepts an integer from 0 to 255. A value of 0 means always off, 255 means always on, and 128 is close to half duty cycle. The duty cycle is simply the value divided by 255, expressed as a percentage.
Because the scale is linear in time but brightness perception is not, equal steps in the number do not look equal to the eye. Treat the values as a control range to experiment with rather than as a direct brightness percentage.
Which pins support PWM
On an Uno-style board the PWM-capable digital pins are 3, 5, 6, 9, 10 and 11, usually marked with a tilde. Pins 5 and 6 are driven at a different frequency from the others. Calling analogWrite on a pin without PWM support does not create a fade: the output will simply be low or high depending on the value you write.
Other boards use different hardware and different pin mappings. Check the documentation for the board you are actually using instead of assuming the Uno pinout applies everywhere.
Wiring stays the same
The circuit is identical to a normal LED circuit: the anode connects to the pin through a current-limiting resistor, and the cathode returns to GND. PWM changes how the pin is driven in time, not how much current the pin can safely deliver.
During each on-pulse the full supply voltage appears across the resistor and LED. Removing the resistor because the average voltage is lower is a mistake that can destroy the LED or the output pin.
A fade loop you can read
A fade is a value that walks up and down between the ends of the scale. Keep the level and the direction in separate variables, reverse the direction at the boundaries, and write the level to the pin on every pass. Changing the delay changes how quickly the fade runs without changing the electrical behaviour.
This structure is easy to extend. You can start at a different level, fade only upward, or hold at a chosen value by adding a condition. The core idea never changes: one number controls the duty cycle.
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);
}
Why steps look uneven
Human brightness perception is roughly logarithmic. A change from a low duty cycle to a slightly higher one looks far more dramatic than the same numerical step near the top of the range. If you want a visually smooth fade, map a curve onto the value instead of using a straight ramp; a squared relationship spreads the visible change more evenly.
Cameras can show the pulses as bands because the shutter samples a slice of each switching cycle. That is a capture effect, not a fault in the circuit or the code.
A useful experiment is to write a short table of values and note the visual step between each pair. Most people find that the lower part of the range changes quickly while the upper part compresses. Building your own curve from that observation teaches more than copying a formula, and it works for any LED and any board.
Common mistakes
Using a pin without PWM support, dropping the resistor, and expecting a real analogue voltage are the three frequent errors. A fourth is trying to drive a motor or a strip of LEDs directly from the pin; those loads need a transistor or a proper driver stage sized for the current.
Once the LED fades as expected, you have a reliable building block for dimming, indicator effects and simple analogue-style control.
Get the parts
LEDs and resistors for this circuit are available from hobidevre.com.
Frequently asked questions
Is PWM the same as an analogue output?
No. It is a digital signal switched quickly enough that the average looks analogue to a slow load such as an LED.
Why do pins 5 and 6 behave differently?
On an Uno they run at a different PWM frequency. The duty-cycle idea is the same; only the switching rate changes.
Can I dim an RGB LED this way?
Yes, with one resistor and one PWM pin per channel. Common-anode modules need the logic inverted in code.