0
0
Power-electronicsHow-ToBeginner · 2 min read

Embedded C: Convert ADC Value to Voltage Easily

To convert an ADC value to voltage in Embedded C, use the formula voltage = (adc_value * reference_voltage) / max_adc_value, where reference_voltage is the ADC reference voltage and max_adc_value is the ADC resolution maximum (e.g., 1023 for 10-bit ADC).
📋

Examples

Inputadc_value = 0, reference_voltage = 5.0, max_adc_value = 1023
Outputvoltage = 0.0 V
Inputadc_value = 512, reference_voltage = 5.0, max_adc_value = 1023
Outputvoltage ≈ 2.502 V
Inputadc_value = 1023, reference_voltage = 3.3, max_adc_value = 1023
Outputvoltage = 3.3 V
🧠

How to Think About It

To convert the ADC reading to voltage, think of the ADC value as a fraction of the maximum ADC count. Multiply this fraction by the known reference voltage to get the actual voltage. This works because the ADC maps voltages linearly from 0 to the reference voltage across its range.
📐

Algorithm

1
Get the ADC reading value.
2
Know the ADC reference voltage and maximum ADC value (resolution).
3
Calculate voltage by multiplying ADC value by reference voltage, then divide by maximum ADC value.
4
Return or use the calculated voltage.
💻

Code

embedded_c
#include <stdio.h>

int main() {
    int adc_value = 512; // example ADC reading
    float reference_voltage = 5.0; // ADC reference voltage in volts
    int max_adc_value = 1023; // for 10-bit ADC

    float voltage = (adc_value * reference_voltage) / (float)max_adc_value;

    printf("ADC Value: %d\n", adc_value);
    printf("Voltage: %.3f V\n", voltage);

    return 0;
}
Output
ADC Value: 512 Voltage: 2.502 V
🔍

Dry Run

Let's trace adc_value = 512, reference_voltage = 5.0, max_adc_value = 1023 through the code

1

Read ADC value

adc_value = 512

2

Calculate voltage

voltage = (512 * 5.0) / 1023 = 2560 / 1023 ≈ 2.502 V

3

Print voltage

Output: Voltage: 2.502 V

adc_valuereference_voltagemax_adc_valuevoltage
5125.010232.502
💡

Why This Works

Step 1: ADC value as fraction

The ADC value represents how much of the reference voltage is measured, so adc_value / max_adc_value gives the fraction of the full scale.

Step 2: Multiply by reference voltage

Multiplying the fraction by reference_voltage converts the fraction back to an actual voltage value.

Step 3: Use float for precision

Using floating-point arithmetic ensures the voltage calculation is accurate and includes decimals.

🔄

Alternative Approaches

Using integer math with scaling
embedded_c
#include <stdio.h>

int main() {
    int adc_value = 512;
    int reference_voltage_mv = 5000; // in millivolts
    int max_adc_value = 1023;

    int voltage_mv = (adc_value * reference_voltage_mv) / max_adc_value;

    printf("Voltage: %d mV\n", voltage_mv);
    return 0;
}
This method avoids floating-point math by using millivolts, which is useful on microcontrollers without FPU but less precise for fractional volts.
Using lookup table
embedded_c
#include <stdio.h>

float voltage_table[1024];

void init_voltage_table(float ref_voltage) {
    for (int i = 0; i <= 1023; i++) {
        voltage_table[i] = (i * ref_voltage) / 1023;
    }
}

int main() {
    int adc_value = 512;
    init_voltage_table(5.0);
    printf("Voltage: %.3f V\n", voltage_table[adc_value]);
    return 0;
}
Precomputing voltages in a table speeds up conversion but uses more memory.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation is a simple arithmetic operation done once per ADC reading, so it runs in constant time O(1).

Space Complexity

Only a few variables are used, so space complexity is O(1), constant memory.

Which Approach is Fastest?

Direct calculation is fastest and simplest; lookup tables trade memory for speed; integer math avoids floating-point but may lose precision.

ApproachTimeSpaceBest For
Direct calculationO(1)O(1)Simple and precise conversions
Integer math scalingO(1)O(1)MCUs without floating-point support
Lookup tableO(1)O(n)Speed-critical applications with enough memory
💡
Always use the correct ADC resolution and reference voltage for accurate voltage conversion.
⚠️
Forgetting to use floating-point division causes incorrect voltage calculation due to integer division truncation.