0
0
Embedded Cprogramming~5 mins

ADC resolution and reference voltage in Embedded C

Choose your learning style9 modes available
Introduction

ADC resolution and reference voltage help convert real-world signals into numbers a microcontroller can understand.

Measuring temperature with a sensor that outputs voltage.
Reading battery level to check how much charge is left.
Detecting light intensity using a photoresistor.
Measuring sound levels with a microphone.
Reading joystick position for a game controller.
Syntax
Embedded C
digital_value = (analog_voltage / reference_voltage) * ((1 << resolution) - 1);

resolution is the number of bits the ADC uses (e.g., 10 bits means values from 0 to 1023).

reference_voltage is the maximum voltage the ADC can measure (e.g., 5V or 3.3V).

Examples
This example calculates the digital value for 2.5V input with 10-bit ADC and 5V reference.
Embedded C
resolution = 10;
reference_voltage = 5.0;
analog_voltage = 2.5;
digital_value = (analog_voltage / reference_voltage) * ((1 << resolution) - 1);
Here, a 12-bit ADC with 3.3V reference converts 1.65V input.
Embedded C
resolution = 12;
reference_voltage = 3.3;
analog_voltage = 1.65;
digital_value = (analog_voltage / reference_voltage) * ((1 << resolution) - 1);
Sample Program

This program calculates the digital output of a 10-bit ADC with 5V reference for an input voltage of 2.5V.

Embedded C
#include <stdio.h>

int main() {
    int resolution = 10; // 10-bit ADC
    float reference_voltage = 5.0; // 5V reference
    float analog_voltage = 2.5; // example input voltage

    int max_digital = (1 << resolution) - 1; // 1023 for 10-bit
    int digital_value = (int)((analog_voltage / reference_voltage) * max_digital);

    printf("Analog voltage: %.2f V\n", analog_voltage);
    printf("Digital value (ADC output): %d\n", digital_value);

    return 0;
}
OutputSuccess
Important Notes

Higher resolution means more precise measurements but may take longer to convert.

Reference voltage sets the maximum voltage the ADC can read; using a stable reference improves accuracy.

Always scale your input voltage to be within 0 and reference voltage to avoid incorrect readings.

Summary

ADC resolution defines how many steps the analog input is divided into.

Reference voltage is the maximum voltage the ADC can measure.

Digital value = (input voltage / reference voltage) x (2^resolution - 1).