Foundations · inputs-and-outputs
Read a sensor, drive an output
Question: How does an Arduino turn a changing voltage into a number?
Understand the idea
A digital input answers a threshold question: is the voltage low or high? An analog input uses an analog-to-digital converter (ADC) to represent a voltage as a number. The count depends on the board's ADC resolution and reference voltage, so it is not automatically a reading in volts.
A potentiometer wired as a divider gives a useful first experiment: its middle pin moves between ground and the reference rail. Keep every signal within the board's allowed input range; 5 V-tolerant and 3.3 V-only boards are not interchangeable.
PWM output is also easy to misunderstand. `analogWrite` commonly changes the fraction of time a digital pin is high; it does not create a smooth analog voltage on most Arduino boards. For a motor, use PWM through a suitable driver.
Worked example
On a 10-bit ADC with a 5 V reference, a reading near 512 represents about 512/1023 × 5 V ≈ 2.50 V. Check the specific board: ADC resolution, reference, and PWM-capable pins differ.
Try it at the bench
- Wire a potentiometer's outer legs to the board's appropriate supply and ground, and its wiper to an analog input.
- Print the raw ADC count while turning the knob slowly. Note the low and high counts.
- Map the measured range to a safe LED brightness using a PWM-capable pin and a series resistor.
Check your understanding
Does PWM value 128 necessarily mean the pin is outputting a steady half-voltage?
Show the answer
No. It usually means a pulse train with roughly half its period high; the load or an added filter determines the averaged effect.