Digital and analogue

What a digital and an analogue signal are, how the micro:bit and Arduino measure a voltage (0 to 1023), when a pin is HIGH, and why PWM isn't a real voltage.

A light switch, a volume knob, a potentiometer, a micro:bit, a strip of paper with a smooth and a stepped line, and an Arduino

A switch and a knob

The light switch in your room has only two positions: the light is either on or off. There is nothing in between. A signal like that is called digital.

You can turn the volume knob on a speaker a little, halfway or all the way, and the volume follows every move. A signal like that, which can have any value, is called analogue.

Two graphs of a voltage over 10 seconds. On the left a digital signal jumps only between 0 V and 5 V. On the right an analogue signal changes slowly and can be any voltage between 1 V and 4.3 V

On the left graph the digital signal is either 0 V or 5 V. On the right one the analogue signal goes up and down and passes through every value in between.

The world is analogue, a computer counts

Light, temperature, sound and speed change gradually, like the knob: the world around us is analogue. A computer, a micro:bit and an Arduino work with numbers. So a sensor turns what it measures into a voltage, and the microcontroller turns that voltage into a number.

Digital and analogue on the micro:bit

The micro:bit has blocks for both kinds of signal:

  • button A is pressed is digital: the button is pressed or it isn’t, true or false.
  • digital read pin P0 is digital too: it gives 0 or 1.
  • light level is analogue: it gives a number from 0 (dark) to 255 (very bright). The micro:bit measures light with its LEDs.
  • analog read pin P0 is analogue: it gives a number from 0 to 1023, depending on the voltage on pin P0.

Try this program: the block plot bar graph of light level up to 255 inside a forever block shows the light level as bars on the display. Cover the micro:bit with your hand, then turn it towards the window, and watch the bars rise and fall.

MakeCode blocks: inside a forever block, the block plot bar graph of light level up to 255

Stairs instead of a ramp

A microcontroller doesn’t turn a voltage into just any number, but into one of a set of whole numbers, step by step. A smooth ramp becomes a staircase.

Two graphs. On the left a voltage rises steadily from 0 to 5 V, and a three-bit converter turns it into eight steps, the numbers 0 to 7, each 0.625 V high. On the right the first seven steps of the Arduino's converter, each 4.9 mV high

On the left graph the converter has only eight steps (the numbers 0 to 7), so they are big and easy to see. The Arduino’s converter has 1024 steps, and each is only about 5 thousandths of a volt high. The right graph shows its first seven steps, greatly magnified. The more steps there are, the smaller they are and the more exact the measurement is.

Digital and analogue at the Arduino workshop

When is a pin HIGH, and when LOW?

A digital input measures a voltage too, only it doesn’t turn it into a number: it compares it with two limits.

Two voltage columns. An Arduino Uno on 5 V: LOW up to 1.5 V, an uncertain zone from 1.5 to 3 V, HIGH from 3 V to 5 V. An arrow shows that 3.3 V falls in the HIGH zone. A micro:bit on 3.3 V: LOW up to 0.99 V, an uncertain zone up to 2.31 V, HIGH from 2.31 V to 3.3 V

The chip on an Arduino Uno reliably reads a voltage up to 0.3 · 5 V = 1.5 V as LOW, and one from 0.6 · 5 V = 3 V upwards as HIGH. In between is an uncertain zone: there the pin may read anything, and the value can jump about. That is why an input that is connected to nothing, not even to a resistor to 5V or GND, reads random values (see Buttons and switches).

Microcontrollers on 3.3 V have lower limits. An Uno still reads a 3.3 V output as HIGH, because it is above 3 V, but with little to spare. The other way round is not allowed: 5 V on a pin of a micro:bit or an ESP32 can damage it. There is more about this in misconception 9.

How an Arduino measures a voltage

Diagram of the Arduino's converter: pins A0 to A5 go to one switch, which is now on A2. After it is a capacitor C to GND, then a 10-bit converter, the ADC, which gives a number from 0 to 1023

analogRead() works in three steps:

  1. A switch connects the chosen pin, A0 to A5, to the converter. There is only one converter, so the pins share it.
  2. A small capacitor charges to the pin’s voltage and holds it while the converter measures, so that the voltage doesn’t change in the meantime.
  3. The converter (ADC, analogue-to-digital converter) compares the voltage with 5 V and gives a number from 0 to 1023. That is 10 bits (see Binary, octal and hexadecimal numbers).

One step is 5 V / 1024 ≈ 4.9 mV, and you work out the voltage from the number n:

\[ U = n \cdot \frac{5\ \text{V}}{1024} \]
A voltage of 2.5 V gives the number 512, and 3.3 V the number 675. One measurement takes about 0.1 ms, so an Arduino can measure about 9000 times a second.

What spoils a measurement

  • The supply is the ruler. The converter compares the voltage with the 5 V supply. USB gives 4.75 to 5.25 V, so every measurement can be 5 % off. When you need more accuracy, analogReference(INTERNAL) uses an internal 1.1 V, but then you can measure only up to 1.1 V.
  • A large source resistance. The capacitor from step 2 needs to charge. If the voltage comes through a resistance larger than about 10 kΩ, and the program measures several pins one after another, the first measurement after changing pins may show part of the previous pin’s voltage. Then measure twice and take the second measurement.
  • Noise. The number jumps by a step or two even when the voltage doesn’t change. The average of several measurements calms it down.
  • Interference from the supply. A motor or a buck converter on the same supply moves the measurement by a dozen steps.

Program: measuring a voltage

Connect a potentiometer as in the lesson Arduino - step by step: its ends to 5V and GND, and its wiper to A0. Every 100 ms the program prints one measurement, the average of 16 measurements and the voltage in millivolts. Open the Serial Plotter in the Arduino IDE and you will see that the average stays still, while a single measurement jumps a little.

// Measuring the voltage on A0: the raw value, the average of 16 readings and the voltage.
// A potentiometer wired as in lesson 2.0: its ends on 5V and GND, its wiper on A0.
// Open the Serial Plotter (115200 baud) and turn the potentiometer.

const int POT = A0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  int raw = analogRead(POT);               // one reading, 0 to 1023

  long sum = 0;                            // 16 readings added up don't fit in an int
  for (int i = 0; i < 16; i++) {
    sum += analogRead(POT);
  }
  float average = sum / 16.0;

  float voltage = average * 5.0 / 1024.0;  // one step is 5 V / 1024, about 4.9 mV

  Serial.print("raw:");
  Serial.print(raw);
  Serial.print(" average:");
  Serial.print(average);
  Serial.print(" mV:");
  Serial.println(voltage * 1000.0, 0);     // the voltage in millivolts, so it fits on the same graph

  delay(100);
}
  • long sum: 16 readings of 1023 make 16,368, which would still fit in an int. With more readings it wouldn’t, so the sum is a long from the start (see Variables and data types).
  • sum / 16.0: dividing by 16.0 rather than 16 gives a decimal number.

PWM isn’t an analogue voltage

analogWrite() doesn’t give a voltage between 0 and 5 V. It switches the pin on and off very fast (PWM, as for the brightness of an LED). An LED or a motor then behaves as if it got a lower voltage, but the pin is always at either 0 V or 5 V. An oscilloscope shows this, and misconception 11 explains it.

When you really need a steady voltage, a resistor and a capacitor smooth the PWM:

Schematic: pin D9 through a 10 kΩ resistor R1 to a 10 µF capacitor C1 to GND. The voltage U1 is on pin D9, and U2 across the capacitor
Graphs of the voltages with analogWrite(9, 128). On the left, over 600 ms, the capacitor's voltage U2 rises from 0 and approaches 2.5 V, while the PWM on the pin is a purple area from 0 to 5 V. Top right, three periods: the pin's voltage U1 jumps between 0 and 5 V, and U2 is an almost flat line at 2.5 V. Bottom right, U2 enlarged: it varies between 2.48 and 2.51 V

With analogWrite(9, 128) the pin is on half the time, 490 times a second. Top right is the real signal on the pin, U1: square pulses between 0 and 5 V. The capacitor charges while the pin is on and discharges while it is off, so its voltage, U2, settles at about 2.5 V and varies only a little, as in the enlarged graph bottom right. The graph is calculated: it reaches 95 % of that voltage after about 0.3 s, and then varies by only about 25 mV. That voltage can’t power anything that draws current, because the current has to pass through 10 kΩ. For that you need an amplifier or a real digital-to-analogue converter (DAC). The Arduino Uno doesn’t have one. The original ESP32 does, and the lab power supply uses an MCP4725 module.

How often to measure

The temperature in a room changes slowly, so one measurement a second is enough. A button or the ball in a pinball machine needs a measurement every few milliseconds. Sound changes thousands of times a second, and to record it you have to measure at least twice as often as the highest frequency you want to hear. An Arduino Uno, with about 9000 measurements a second, can record sound up to about 4500 Hz, which is like a phone call, not like music.

Note

The diagrams, graphs and values on this page are calculated from simplified models or are typical examples. They are here to explain, and may differ from the actual specifications of the parts. Before you build anything, always check the datasheets of the parts you use.

What did we learn?

  • A digital signal has only two values, on and off. An analogue signal can have any value.
  • A microcontroller turns an analogue voltage into a number, in steps. An Arduino Uno gives a number from 0 to 1023, and one step is about 4.9 mV.
  • A digital input on an Arduino Uno reads LOW up to 1.5 V and HIGH from 3 V. In between is an uncertain zone.
  • An inaccurate supply, noise and a large source resistance spoil a measurement. The average of several measurements helps.
  • analogWrite() gives PWM, not a real analogue voltage. A resistor and a capacitor can smooth it.

Quick quiz

1. Which of these micro:bit blocks are analogue: button A is pressed, light level, analog read pin P0?
light level (0 to 255) and analog read pin P0 (0 to 1023). button A is pressed is digital, because a button can only be pressed or not pressed.
2. analogRead() returned 300. What is the voltage on the pin?
U = 300 · 5 V / 1024 ≈ 1.46 V.
3. A pin of an Arduino Uno gets 2 V. Will digitalRead() read HIGH or LOW?
You can’t tell: 2 V is between 1.5 V and 3 V, in the uncertain zone. The pin may read anything. That is why a digital input should get a voltage clearly below 1.5 V or above 3 V.
4. Why does the average of 16 measurements jump less than a single measurement?
Noise moves each measurement a little up or down, differently each time. In the average those moves mostly cancel out, and the real voltage remains.
5. A multimeter on pin D9 with analogWrite(9, 64) shows about 1.25 V. Is that the real voltage on the pin?
No. The pin is at 5 V a quarter of the time and at 0 V the rest. The multimeter shows the average, 5 V · 64 / 255 ≈ 1.25 V. An oscilloscope would show a square wave.