An analog input measures a voltage. PWM produces a timed sequence of on/off pulses. They can participate in the same project, but they are different electrical signals. Here, a knob controls the brightness of a small status indicator while Serial Monitor shows the input and output numbers.
Connections
| Connection | Destination or purpose |
|---|---|
| Potentiometer outer terminals | 5 V and GND. |
| Potentiometer center wiper | A0. |
| D9 | Through 1 kΩ to the LED anode. |
| LED cathode | GND. |
These values are for an Uno R3. Its default ADC returns 0–1023 for the range from ground to its reference voltage, normally the board’s supply. The supply is not a precision voltage reference. Never feed an input outside the limits of the actual board.
Complete example
Download the complete Arduino sketch
const byte knobPin = A0;
const byte ledPin = 9;
unsigned long reportedAt = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
const int reading = analogRead(knobPin);
const int duty = reading / 4;
analogWrite(ledPin, duty);
const unsigned long now = millis();
if (now - reportedAt >= 200) {
reportedAt = now;
Serial.print("ADC="); Serial.print(reading);
Serial.print(" PWM="); Serial.println(duty);
}
}
Turn the knob and watch the LED and 9600-baud serial output. Dividing the 10-bit ADC reading by four maps it into the 0–255 range used by analogWrite() here. The perceived brightness will not look perfectly linear because your eyes respond nonlinearly to light.
What the output really does
On this board, analogWrite() on D9 switches between approximately 0 V and 5 V. The duty cycle is the fraction of each period spent high. It does not produce a steady 2.5 V output at half scale. Uno R3 PWM pins are D3, D5, D6, D9, D10, and D11; other board families have different APIs, timers, or real DAC outputs.
A resistor-capacitor low-pass filter can smooth PWM for a lightly loaded signal input. Its cutoff is fc = 1 / (2πRC); this is the filter cutoff, not the PWM carrier frequency. For example, 10 kΩ and 1 µF give about 16 Hz. Lower cutoff reduces ripple but slows response. A load changes the result, so this is not a general-purpose regulated supply.
Measuring pulse timing
pulseIn(pin, HIGH, timeoutUs) measures one high pulse and returns an unsigned long. Zero means it timed out. A high-pulse width alone does not establish duty cycle; you also need the period. The function blocks while waiting, so choose a finite timeout. For continuous capture, use timer hardware or a carefully designed interrupt routine rather than attaching two handlers to the same interrupt.
Troubleshooting and next steps
- Only on or off: verify the LED is on PWM-capable D9.
- Reading jumps: check the wiper connection and common ground; an unconnected analog input floats.
- Knob reversed: swap the two outside potentiometer connections with power disconnected.
- Output changes after adding a library: some libraries claim timers and affect PWM pins. Check their timer documentation.
Try two analog axes, or use a transistor driver when the eventual output needs more current. Consult the Arduino PWM reference for board-specific behavior.