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 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.

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.

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.

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.

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

analogRead() works in three steps:
- A switch connects the chosen pin, A0 to A5, to the converter. There is only one converter, so the pins share it.
- 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.
- 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:
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 anint. With more readings it wouldn’t, so the sum is alongfrom the start (see Variables and data types).sum / 16.0: dividing by16.0rather than16gives 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:


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?
2. analogRead() returned 300. What is the voltage on the pin?
3. A pin of an Arduino Uno gets 2 V. Will digitalRead() read HIGH or LOW?
4. Why does the average of 16 measurements jump less than a single measurement?
5. A multimeter on pin D9 with analogWrite(9, 64) shows about 1.25 V. Is that the real voltage on the pin?