Sub topic 1
Analog and digital signals
Before you wire a potentiometer, you need to know what kind of signal it produces and how an Arduino sees it. This chapter covers the difference between analog and digital signals, how a digital input pin treats a voltage, and why an analog input needs extra hardware. The next chapter covers that hardware, the analog-to-digital converter, in detail.
Prerequisites
This chapter assumes you can already do the following:
- Write the basic structure of an Arduino sketch with
setup()andloop(). - Upload a sketch and open the Serial Monitor.
- Wire a simple breadboard circuit and explain voltage and ground.
- Use
pinMode()and drive a digital output withdigitalWrite().
The examples target an Arduino Uno, which runs at 5 V. Other boards differ, and the last chapter of this tutorial covers those differences.
What a signal is
A signal is a quantity that carries information. In electronics it is usually a voltage on a wire, measured against ground. The sensor or control changes the voltage, and the microcontroller measures it. The question is how many different voltages carry meaning.
- A digital signal carries meaning in only two states, usually called HIGH and LOW. The exact voltage inside each state does not matter, as long as it falls in the right range.
- An analog signal can take any value within a continuous range. A voltage of 1.2 V, 1.21 V and 1.213 V all mean different things.
Many physical quantities are naturally analog: light level, temperature, the angle of a knob, the position of a slider. A button is naturally digital, because it is either pressed or not.
| Property | Digital signal | Analog signal |
|---|---|---|
| Possible values | Two states (HIGH or LOW) | Any value in a continuous range |
| Typical sources | Buttons, switches, logic chips | Potentiometers, light sensors, temperature sensors |
| Arduino input function | `digitalRead()` | `analogRead()` |
| Result in your sketch | `HIGH` or `LOW` (1 or 0) | An integer, 0 to 1023 on an Uno |
| Sensitivity to noise | Low, because small errors stay inside the same state | Higher, because every small error changes the value |
How a digital input decides HIGH or LOW
A digital input pin does not measure voltage. It compares the voltage against two thresholds and reports one of two answers. On the ATmega328P chip used in the Uno, running at 5 V, the datasheet specifies these limits:
- Any voltage below about 0.3 × Vcc, which is 1.5 V, is guaranteed to read LOW.
- Any voltage above about 0.6 × Vcc, which is 3.0 V, is guaranteed to read HIGH.
- Voltages between 1.5 V and 3.0 V are in an undefined zone. The pin may read either value, and it may change from one read to the next.
This is why digitalRead() is reliable for buttons. A pressed button gives about 0 V or 5 V, both far from the undefined zone. A small amount of electrical noise does not change the result.
The same behavior is a problem for a potentiometer. Suppose the knob sits halfway and puts 2.5 V on the pin. A digital read gives an unpredictable answer, and even a predictable one would discard almost all the information. Turning the knob from 0 V to 1.4 V gives LOW every time. You cannot tell the difference between those positions.
Seeing the difference on a real pin
The pins A0 to A5 on an Uno can work as digital pins as well as analog inputs. Inside the sketch, A0 is also available as digital pin 14. That lets you run both kinds of read on the same wire and compare the results.
Connect the middle terminal of a potentiometer to A0, one outer terminal to 5V, and the other outer terminal to GND. The next chapters explain this wiring in detail. For now it only supplies a voltage that you can vary by hand.
const int SIGNAL_PIN = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int digitalValue = digitalRead(SIGNAL_PIN);
int analogValue = analogRead(SIGNAL_PIN);
Serial.print("digital=");
Serial.print(digitalValue);
Serial.print(" analog=");
Serial.println(analogValue);
delay(200);
}Open the Serial Monitor at 9600 baud and turn the knob slowly from one end to the other. You should see output that follows this pattern:
- With the knob near GND, the digital value is 0 and the analog value is near 0.
- With the knob near 5V, the digital value is 1 and the analog value is near 1023.
- In the middle, the digital value flips from 0 to 1 somewhere in the undefined zone. The analog value keeps rising smoothly through the whole turn, and it is the only one of the two that tells you where the knob is.
The exact analog value where the digital value flips can differ between boards and between runs, because it falls in the undefined zone. Do not rely on it.
Why an analog input needs an ADC
A microcontroller is a digital machine. Its memory and arithmetic work on integers, not on voltages. To use an analog voltage in a sketch, the chip must convert it into a number. The part that does this is the analog-to-digital converter, or ADC.
An ADC measures the voltage on an input pin at one instant and returns an integer proportional to it. The Uno's ADC returns a value from 0 to 1023, so a reading is always one of 1024 possible numbers. The conversion has two consequences that matter for the rest of this tutorial.
- The result is quantized. A continuous voltage becomes a number from a fixed list. Two voltages that are very close together can produce the same number.
- The result is a snapshot. The ADC samples the voltage at a moment in time. Between samples, your sketch knows nothing about the pin.
The signal path from a knob to a number in your sketch looks like this.
Analog input versus analog output
The names are easy to confuse. Reading and writing are not symmetric on most Arduino boards.
analogRead()uses a real ADC and returns a measured value.analogWrite()does not produce a true analog voltage on an Uno. It produces a fast on-off signal called PWM, with a controllable fraction of time spent on. An LED averages that signal and appears dimmer or brighter.
A later chapter in this tutorial uses analogWrite() to dim an LED from a potentiometer. For now, remember that "analog" in analogWrite() describes the effect, not the signal on the pin.
Common mistakes
These errors come up often when people move from buttons to analog sensors.
- Using
digitalRead()on a sensor that varies continuously. The code compiles and runs, but it throws away the information you wanted. - Expecting exactly 0 or 1023. Real readings rarely reach the extremes, because of wire resistance, supply variation and noise. A pot turned fully to one end often reads a few counts away from the limit.
- Expecting a stable number. Even with the knob untouched, successive readings can differ by one or two counts. A later chapter covers averaging and a deadband for this.
- Leaving an analog pin unconnected. A floating input picks up stray fields from your hand and nearby wires, and the readings wander. Always connect the pin to a defined voltage source.
- Applying more than the supply voltage. On an Uno, an input pin must stay between 0 V and about 5 V. A voltage outside that range can damage the chip.
Choosing the right input type
Use these questions to decide which kind of input a part needs.
- Does the part have only two meaningful states, such as pressed and released? Use a digital input.
- Does the part's output vary smoothly with a physical quantity? Use an analog input.
- Is the part a digital sensor that sends data over a bus such as I2C or SPI? It does not use an analog or digital input in this sense. A library talks to it.
Summary
A digital signal has two states, and a digital input pin turns any voltage into one of them by comparing it with thresholds. An analog signal varies continuously, and the Arduino measures it with an ADC that turns it into an integer. The measurement is quantized, a snapshot in time, and subject to noise. A potentiometer produces a smooth analog voltage, so it needs an analog input, and the rest of this tutorial builds on that.
Exercise
On an Uno running at 5 V, a potentiometer wiper puts about 2.2 V on pin A0. You call digitalRead(A0) in one sketch and analogRead(A0) in another. Which function gives a reliable, repeatable answer for this voltage, and why does the other not?
Show answer
analogRead(A0) is reliable. The ADC converts 2.2 V into an integer proportional to the voltage, so the result is stable apart from a count or two of noise. digitalRead(A0) is not reliable because 2.2 V lies between the LOW threshold (about 1.5 V) and the HIGH threshold (about 3.0 V), which is the undefined zone. The pin may report HIGH or LOW and may change between reads. Even if it were stable, it would return only one bit and discard the position of the knob.
Exercise
A colleague says analogWrite() on an Uno outputs a true analog voltage, so analogRead() and analogWrite() are opposites. What is wrong with this statement?
Show answer
analogRead() measures a real voltage with an ADC and returns an integer from 0 to 1023. analogWrite() on an Uno produces a PWM signal, a fast on-off waveform whose on-time fraction you control. The pin is still only HIGH or LOW at any instant. A load such as an LED averages the waveform, which creates the effect of an intermediate level. The two functions are not symmetric.