Sub topic 1
Why a Digital Pin Can't Output a Steady In-Between Voltage
Every sketch you have written so far treats an output pin as a switch. digitalWrite(pin, HIGH) turns an LED on and digitalWrite(pin, LOW) turns it off. Fading an LED needs something in between, such as 40% brightness. This chapter explains why the pin cannot do that on its own. It also lists what you can do about it, which leads into pulse-width modulation (PWM) in the next chapter.
Prerequisites
This chapter assumes you already know the following:
- The structure of an Arduino sketch, with
setup()andloop(). - How to use
pinMode()anddigitalWrite(). - Basic breadboard wiring.
- Ohm's law at an introductory level: V = I × R.
The examples target an Arduino Uno (ATmega328P microcontroller, 5 V logic). Other boards differ in details, and the chapter says so where it matters.
What a Digital Output Actually Is
A digital output pin is not a voltage source you can set to any value. Inside the chip, each pin is connected to two electronic switches, built from transistors:
- A high-side switch connects the pin to the supply voltage (5 V on an Uno).
- A low-side switch connects the pin to ground (0 V).
This arrangement is called a push-pull output stage. The chip closes one switch at a time. With the high-side switch closed, the pin is pushed up to the supply and reads HIGH. With the low-side switch closed, the pin is pulled down to ground and reads LOW.
The control for these switches is one bit in a hardware register. On the ATmega328P, the DDR registers choose input or output for each pin. The PORT registers hold the output level. A bit has two values, so the pin has two output states. Nothing in this path can hold a fraction.
Why digitalWrite() Has Only Two Outcomes
digitalWrite() takes a value argument, which can mislead you into thinking larger numbers give more output. They do not. The function tests only whether the value is zero. Any nonzero value counts as HIGH.
const uint8_t LED_PIN = 8;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(LED_PIN, 1);
Serial.print("value 1 -> pin reads ");
Serial.println(digitalRead(LED_PIN));
delay(1000);
digitalWrite(LED_PIN, 128);
Serial.print("value 128 -> pin reads ");
Serial.println(digitalRead(LED_PIN));
delay(1000);
digitalWrite(LED_PIN, 0);
Serial.print("value 0 -> pin reads ");
Serial.println(digitalRead(LED_PIN));
delay(1000);
}Open the Serial Monitor at 9600 baud. Values 1 and 128 both print 1, and 0 prints 0. The LED on pin 8, if you wire one, is equally bright for 1 and 128. This is the most common beginner error in this topic: writing digitalWrite(pin, 128) and expecting half brightness.
The table summarizes what each value does.
| Call | Pin state | Approximate pin voltage (Uno, light load) |
|---|---|---|
| `digitalWrite(pin, LOW)` or `0` | LOW | Close to 0 V |
| `digitalWrite(pin, HIGH)` or `1` | HIGH | Close to 5 V |
| `digitalWrite(pin, 128)` | HIGH | Close to 5 V |
| `digitalWrite(pin, 255)` | HIGH | Close to 5 V |
What Counts as "Close To" 5 V
Real output voltages are not exact, and the exact figures matter once you start measuring. A pin's voltage depends on the current it supplies, because the switch inside the chip has a small resistance.
The ATmega328P datasheet guarantees at least 4.2 V on a HIGH pin supplying 20 mA, and at most 0.9 V on a LOW pin sinking 20 mA, with a 5 V supply. With almost no load, the pin sits close to the rails. So the pin voltage sags a little as the load draws more current. That sag is a side effect of the transistor's resistance. It is not a way to choose a voltage, because you cannot control it, and it changes with load and temperature.
The chip also has limits you should respect:
- The datasheet's absolute maximum for one pin is 40 mA. Treat 20 mA as the practical ceiling for continuous use.
- The whole chip has a total current limit as well, so many pins at 20 mA each can exceed it.
An LED with a suitable resistor draws roughly 5 to 15 mA, which is well inside these limits.
On the input side, a pin does not read a continuous value either. It compares the incoming voltage with thresholds and returns HIGH or LOW. This is a separate matter from output, but it shows the same design: digital pins deal in two levels.
Ways to Get a Steady In-Between Voltage
If you need a real, steady 2.5 V, a digital pin is the wrong tool by itself. These are the usual options.
| Approach | How it works | Cost and drawbacks |
|---|---|---|
| Digital-to-analog converter (DAC) | A circuit that produces a voltage proportional to a number | Not built into the Uno. Available on some boards (for example the Arduino Due) or as an external chip such as the MCP4725 |
| Resistor divider | Two resistors split a fixed voltage | The result is fixed, not adjustable from code, and sags under load |
| PWM plus a low-pass filter | Fast switching averaged by a resistor and capacitor | Needs extra parts, has some ripple, and responds slowly |
| PWM driving a load that averages it | Fast switching feeds a load that responds slowly, such as an LED seen by the eye | No extra parts, but the pin voltage itself is never steady |
The last row is the approach this tutorial uses. Notice what it gives up. The pin still outputs only 0 V or 5 V at any instant. What changes is how long it spends at each level. The load, here an LED watched by a human eye, responds to the average. So the pin never outputs a steady in-between voltage. The effect you need arrives by a different route.
Many Arduino tutorials call this "analog output", and the function is named analogWrite(). On an Uno, that name is misleading. The function does not drive a DAC. It starts the PWM hardware. You will meet it in a later chapter, and the name will stop being confusing once you know what is behind it.
A Preview: Fast Switching You Can Build Yourself
You can test the idea with nothing but digitalWrite() and delayMicroseconds(). This sketch turns a pin on and off 500 times per second, and changes the fraction of each cycle that the pin spends HIGH. It uses pin 8, which has no PWM hardware, to make the point that the technique does not depend on special pins.
const uint8_t LED_PIN = 8;
const unsigned long PERIOD_US = 2000; // 2 ms per cycle = 500 Hz
const uint8_t DUTY_PERCENT[] = {10, 50, 90};
const uint8_t LEVEL_COUNT = sizeof(DUTY_PERCENT) / sizeof(DUTY_PERCENT[0]);
const uint16_t PERIODS_PER_LEVEL = 1000; // about 2 seconds per level
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
for (uint8_t level = 0; level < LEVEL_COUNT; level++) {
unsigned long onTime = PERIOD_US * DUTY_PERCENT[level] / 100;
unsigned long offTime = PERIOD_US - onTime;
for (uint16_t p = 0; p < PERIODS_PER_LEVEL; p++) {
digitalWrite(LED_PIN, HIGH);
delayMicroseconds(onTime);
digitalWrite(LED_PIN, LOW);
delayMicroseconds(offTime);
}
}
}Connect an LED and a resistor from pin 8 to ground (the next chapters cover the circuit in detail). The LED looks dim for about two seconds, medium for two seconds, then bright, then repeats. At no point is the pin at 1 V or 3 V. It is always at 0 V or 5 V, and your eye averages it.
This software approach has costs that explain why microcontrollers include PWM hardware:
- The processor spends all its time inside the loop, so nothing else can run.
- Each
digitalWrite()call takes a few microseconds, so the real period is a little longer than 2000 µs. The error is small here but grows when periods are short. - Any interrupt, such as the one that keeps
millis()running, delays an edge slightly and can cause visible jitter.
Common Errors
- Expecting
digitalWrite()to scale with its value. Any nonzero value gives HIGH. UseanalogWrite()on a PWM-capable pin when you need brightness levels. - Measuring a PWM pin with a basic multimeter and trusting the reading. Many meters average the signal, which gives a value that looks like a steady in-between voltage. An oscilloscope shows the actual 0 V and 5 V pulses. Both readings are correct, but they measure different things.
- Assuming the pin holds 5.00 V under load. The voltage drops as current rises. Check the datasheet figures if your circuit is sensitive.
- Drawing too much current from a pin. Driving a motor or a relay directly from a pin can damage the chip. Use a transistor or driver for loads above the pin's rating.
- Leaving a pin as an input and calling
digitalWrite()on it. On AVR boards, writing to a pin set asINPUTtoggles its internal pull-up resistor instead of driving the pin. The LED stays dim or off, which looks like a partial voltage but is a different mechanism.
Key Points
- A digital output pin connects to either the supply or ground. It has no third state that produces a chosen voltage.
digitalWrite()treats every nonzero value as HIGH.- HIGH and LOW voltages vary with load, but you cannot use that variation as a control.
- A steady in-between voltage needs a DAC, a divider, or filtered PWM.
- Switching fast between the two levels lets a slow load, such as an LED viewed by the eye, respond to the average. This is the idea behind PWM.
Exercise
You write digitalWrite(8, 100); and wire an LED to pin 8. A friend expects the LED to be about 40% as bright as with digitalWrite(8, 255);. What actually happens, and what does the software PWM sketch from this chapter do differently to dim the LED?
Show answer
The LED is exactly as bright as with 255, because digitalWrite() treats any nonzero value as HIGH and the pin goes to about 5 V. The software PWM sketch never holds the pin at an intermediate voltage either. It switches the pin between 0 V and 5 V 500 times per second and changes the fraction of each 2 ms cycle spent HIGH. The LED, watched by the eye, responds to the average, so a 10% duty looks dim and a 90% duty looks bright.
Exercise
In the software PWM sketch, onTime is declared unsigned long. What goes wrong on an Uno if you declare it int instead, with PERIOD_US at 2000 and a duty of 90?
Show answer
On the Uno an int is 16 bits, with a maximum of 32767. The product 2000 * 90 is 180000, which overflows, so onTime comes out as a wrong value, and the duty cycle no longer matches the percentage. Using unsigned long (32 bits) holds the product without overflow.