Sub topic 1
Analog and Digital Signals
Prerequisites
This chapter assumes that you know the basic Arduino sketch structure, including setup() and loop(). You should also know how to upload a sketch, open the Serial Monitor, and connect power, ground, and components on a breadboard. Familiarity with pinMode() and digital outputs is helpful.
What Makes a Signal Analog or Digital?
A signal is a changing electrical quantity that carries information. In an Arduino circuit, the signal is usually a voltage measured relative to ground.
An analog signal can have any value within a continuous range. For example, a sensor might produce 1.21 V, 1.22 V, or 1.23 V. A potentiometer produces this kind of signal as you turn its knob. Its output voltage changes gradually rather than jumping between fixed states.
A digital signal uses defined states. In a simple Arduino circuit, these states are usually LOW and HIGH. A digital input interprets a voltage as one state or the other. It does not normally report the exact voltage that it sees.
| Signal type | Possible values | Typical Arduino operation |
|---|---|---|
| Analog | Any voltage in a range | `analogRead()` |
| Digital | LOW or HIGH | `digitalRead()` |
The distinction describes how the value is represented and interpreted. An analog voltage can still be read by digital electronics, but the electronics must first convert it into a number.
What an ADC Does
An analog-to-digital converter (ADC) is a circuit that measures an analog voltage and represents the measurement as a digital number. Arduino boards use an ADC for their analog input pins.
The ADC performs two important operations:
- It samples the voltage at a particular moment.
- It rounds that voltage to one of a fixed number of numeric steps.
This rounding is called quantization. Because the ADC has a finite number of steps, two slightly different voltages can produce the same reading. The reading is therefore an estimate of the input voltage, not an infinitely precise measurement.
On a typical Arduino Uno, the ADC has 10-bit resolution. Ten bits provide 2¹⁰, or 1,024, possible codes. The codes are numbered from 0 through 1,023. The lowest code represents a voltage near the selected reference level's ground, and the highest code represents a voltage near the reference voltage.
Why This Matters for a Potentiometer
A potentiometer is useful because it lets you create a controlled analog voltage. Turning its shaft changes the voltage sent to an analog input. The Arduino then uses its ADC to turn that changing voltage into a number.
This number can represent a position, a setting, or a control level. For example, a program could use a low number for a slow motor speed and a high number for a fast speed.
The ADC does not automatically know what the number means. The program must interpret the number. It can convert the reading to volts, compare it with limits, or map it to another range such as LED brightness.
Common Errors
A digital input is not a substitute for an analog input when you need a changing measurement. A digital input might tell you only that the potentiometer voltage is above or below a threshold.
An analog reading also depends on the board's ADC resolution and voltage reference. The same physical voltage can produce different raw numbers on different Arduino-compatible boards. Always check the board documentation before treating a raw reading as a specific voltage.
A floating input is another common problem. An input is floating when it is not connected to a defined voltage. It can then pick up electrical noise and produce unpredictable values. A potentiometer wired between power and ground provides a defined voltage at its wiper, avoiding this problem.
Exercise
Why does a potentiometer connected to a digital input fail to provide a useful position value?
Show answer
A digital input normally reduces the voltage to LOW or HIGH, so it does not report the potentiometer's full range of changing voltage. An analog input uses an ADC to produce a numeric measurement.