Atlas Robotics
FundamentalsBasic

Digital vs analog: why microcontrollers need an ADC to sense the real world

Atlasbot · 20/8/2026 · 10 min read

Digital vs analog: why microcontrollers need an ADC to sense the real world
Digital signals only have 2 levels, analog continuously changes

Light sensors (LDR), temperature sensors, or microphones all create signals that continuously change over time — called the analog signal. But the processor inside the microcontroller only really understands digital — sequences of 0s and 1s. The bridge between these two worlds — ADC (read analog in) and PWM (simulate analog out) — is required background knowledge before working with any analog sensor.

What is digital?

The digital (digital) signal has only a finite number of discrete levels — the most common are two: HIGH (usually ~3.3V or 5V) and LOW (~0V). The microcontroller distinguishes between these two levels based on a voltage threshold (threshold) — voltage above the threshold reads HIGH (1), below the threshold reads LOW (0), regardless of the exact value in between.

What is analog?

The analog (analog) signal changes continuously within a range of values, without jumping between discrete levels — like a volume knob that rotates smoothly, unlike a button that only has 2 on/off states. Most physical quantities in the real world (light, temperature, pressure, sound) are inherently analog.

In short: Digital = finite discrete level (usually 2 levels: 0/1); Analog = continuously changing, infinite values ​​within a range.

ADC — bridge from Analog to Digital

ADC (Analog-to-Digital Converter) is the part in the microcontroller that measures analog voltage and converts it into a digital number that the program can read. The two most important parameters of ADC:

  • Resolution — number of bits of the result, determines the fineness. 10-bit ADC (common on Arduino Uno) divides the voltage into 2¹⁰ = 1024 levels; 12-bit ADC (STM32, ESP32) divided into 4096 finer levels.
  • Reference voltage — voltage corresponding to the maximum ADC value. On the Arduino Uno, the default is 5V — meaning the ADC value reads 1023 corresponding to exactly 5V applied to the analog pin.
Measured voltage = (ADC value / Maximum ADC value) × Reference voltage

For example: 10-bit ADC, Vref = 5V, read value 512
Voltage = (512 / 1023) × 5V ≈ 2.5V

PWM — simulates Analog output with Digital

In contrast to ADCs, many microcontrollers (especially the regular pins on the Arduino) do not have a real DAC to output an accurate analog voltage — instead use PWM (Pulse Width Modulation): turn the pin on/off at a very fast frequency, varying the ratio of the on time (duty cycle) to create what appears to be a voltage level different average.

// Arduino — 8-bit PWM, values ​​0-255
analogWrite(9, 128);   // duty cycle 50% → average voltage ~2.5V (if the source is 5V)
analogWrite(9, 255);   // duty cycle 100% → almost continuous 5V
analogWrite(9, 0);     // duty cycle 0% → 0V

PWM is fast enough to be "perceived" by the human eye or DC motor as a continuous voltage level (e.g. used to adjust LED brightness or motor speed) — but essentially the signal is still digital on/off continuously, not true analog.

Technical details

Formulas

The voltage measured from the ADC (mentioned above) and the duty cycle of the PWM are two formulas used every day when working with embedded systems:

ADC Voltage = (ADC Value / Maximum ADC Value) × Vref
Duty cycle (%) = (HIGH level time / Cycle) × 100%

VD: PWM 8-bit, analogWrite(9, 64)
Duty cycle = 64 / 255 × 100% ≈ 25%
→ average voltage ≈ 25% × 5V = 1.25V

Parameters

Default ADC resolution and reference voltage on popular boards:

Board/ChipADC resolutionNumber of levelsDefault Vref
Arduino Uno10-bit10245V
STM32 (most lines)12-bit40963.3V
ESP3212-bit40963.3V

Register / Bit / Byte

After the ADC completes a conversion, the result is in a dedicated data register — on the STM32 it is ADCx->DR (Data Register), on the AVR (Arduino Uno) the 10-bit result is divided into 2 8-bit registers ADCH and ADCL (high and low bytes) because the AVR register is only 8-bits wide. Function analogRead() essentially reads and combines these two registers into a 10-bit number — understanding this explains why reading the ADC register directly (instead of calling a library function) is sometimes faster in code that needs high speed.

Practical applications

The line-following robot reads multiple analog infrared sensors through the ADC to distinguish light/dark areas on the line — the higher the ADC resolution, the more intermediate gray levels the robot can distinguish instead of just "line/no line". Similarly, measuring current through an analog current sensor (eg, ACS712) to monitor the AMR robot's battery consumption in real time is also based on this exact ADC principle — the sensor's output voltage is proportional to the current being measured.

Practical example: reading a light sensor via ADC

// Arduino — read photoresistor (LDR) sensor via analog pin
int sensorValue = analogRead(A0);   // returns 0-1023 (10-bit ADC)
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);   // Print out the actual measured voltage, example: 2.45
Sample output:
2.45
2.41
1.83 ← light changes (e.g. covering the sensor), voltage drops

Common mistakes

  • Confusing PWM with true analog — analogWrite() does not produce true analog voltage, just fast on/off digital — if stable true analog voltage (no flickering) is needed, a dedicated RC filter circuit or DAC is needed.
  • Read the ADC value but forget to convert it to voltage — the value analogRead() returns is an integer 0-1023 (or 0-4095 depending on the chip), not a voltage — must be multiplied by Vref/maximum value to get the Volt unit.
  • Reference voltage (Vref) is not as assumed — some boards allow changing Vref (eg: using 3.3V power source instead of default 5V) — forgetting to update the conversion formula when changing Vref leads to incorrect calculation of actual voltage.
  • The ADC resolution is not fine enough for applications requiring high precision — A 10-bit ADC on a 5V measurement range can only distinguish ~4.9mV steps — if you need to measure a very small variable signal, a higher resolution ADC or signal amplification circuit is needed before feeding it to the ADC.

Conclusion

ADC and PWM are two opposing but complementary tools — one reading analog input into digital, the other simulating digital output as analog. Understanding that this is not a "real analog" helps avoid common confusion when debugging sensor circuits or controlling motor speed. The next article in the column is about GPIO — the most basic general-purpose pin, where these digital concepts are directly applied.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

Digital vs analog: why microcontrollers need an ADC to sense the real world — Atlas Robotics