
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 digitalWrite does
digitalWrite puts a pin into one of two states. HIGH connects the pin close to the supply voltage; LOW connects it close to ground. There is no halfway setting. A pin used this way is ideal for switching a load on or off, reading a logic level elsewhere, or generating a simple square wave with delays.
The pin must first be configured as an output with pinMode. Writing to an input pin does not drive the load; it only changes whether the internal pull-up is enabled. This is a small detail that produces confusing results when a sketch is edited quickly.
What analogWrite does
analogWrite does not create an analogue voltage. It switches a supported pin between HIGH and LOW very quickly and changes the fraction of time spent high. That fraction is the duty cycle, and a slow load such as an LED responds to the average, so it appears to change brightness.
The value ranges from zero, always off, to 255, always on. A value near the middle means the pin is high about half of each cycle. Because the switching is faster than the eye, the LED looks steadily lit at a level rather than flickering.
Which pins support each function
digitalWrite works on any digital pin configured as an output. analogWrite works only on pins with PWM hardware. On an Uno those are pins three, five, six, nine, ten and eleven, usually marked with a tilde. Calling analogWrite on another pin does not produce a fade; the output stays low or high.
Other boards have different PWM pin maps. Always check the board documentation rather than memorising the Uno layout. A sketch that fades correctly on one board can appear broken when moved to another, even though the code is unchanged.
Voltage versus duty cycle
A digital HIGH is close to the supply voltage, and a LOW is close to zero. With PWM the pin is still only at one of those two levels at any instant. The apparent intermediate voltage is an average measured over time, which is why a multimeter on a PWM pin reports a value between the extremes.
This distinction matters for loads that need a steady supply. A sensor powered from a PWM pin receives pulses, not a smooth rail, and can produce unstable readings. Use a real regulated output for power and reserve PWM for loads that respond to average power, such as LEDs and motors through a driver.
Choosing the right function
Use digitalWrite when a device has two meaningful states: a lamp on or off, a relay engaged or released, a chip select line active or inactive. Use analogWrite when you need a variable average level: dimming, a slower motor or a simple tone-like effect on a resistive load.
If you need a true analogue voltage, neither function is enough. Use a digital-to-analogue converter or a filtered PWM output with an appropriate buffer. Repeating analogWrite at a pin that is not PWM-capable will never create that voltage, no matter how the code is structured.
Why a servo is a different case
A hobby servo also reads a pulse, but it measures the width of each individual pulse rather than the average over a cycle. For that reason you should use a servo library instead of analogWrite. The library generates a pulse roughly every twenty milliseconds with a width that selects the angle.
This is the clearest example of why the two functions are not interchangeable. An LED cares about the average, so analogWrite works. A servo cares about one pulse width, so analogWrite on its signal pin gives jitter or no movement at all.
Common mistakes
The frequent errors are calling analogWrite on a non-PWM pin, removing the series resistor because the average voltage seems lower, and expecting a smooth analogue rail from either function. During each on-pulse the full supply appears across the load, so current limiting is still required.
A fourth mistake is driving a motor directly from the pin. The pin provides a control signal, not a motor supply. Use a transistor or a driver module sized for the current, and keep the control line separate from the power path.
Once you can explain the difference in one sentence, most output problems become obvious: check whether the load needs a state or an average level, then check whether the chosen pin can produce it.
- digitalWrite: two states on any output pin
- analogWrite: PWM on marked pins only
- 0 to 255 sets duty cycle, not voltage
- Use a driver for high-current loads
Get the parts
LEDs, resistors and driver modules for these experiments are available from hobidevre.com.
Frequently asked questions
Does analogWrite output a real analogue voltage?
No. It switches a pin quickly and changes the on-fraction. The average looks analogue to a slow load.
Why does my LED not fade on pin 7?
Pin 7 has no PWM hardware on an Uno. Use pins 3, 5, 6, 9, 10 or 11.
Can I power a sensor from a PWM pin?
Not reliably. A sensor needs a steady rail, so use a regulated supply output instead.