To convert an analog input of 2.50V to a digital code using a 16-bit ADC with a 5.00V reference, the exact output code is 32768 (assuming unipolar, straight binary encoding). The governing formula is Code = (V_in / V_ref) × (2^n - 1). Substituting our exact values: Code = (2.50 / 5.00) × (65535) = 32767.5, which rounds to 32768. Whether you are pairing an I2C ADS1115 with an MCP4725, or using a true mixed-signal IC like the Analog Devices AD74413R (roughly $18 on Mouser), this math is the bedrock of sensor translation in embedded systems.

Core Conversion Formula & Neighboring Values

The assumption that fixes the answer above is a purely DC, unipolar signal with a perfectly stable Vref. If you are measuring AC waveforms where power factor (pf) and phase angle come into play, the instantaneous ADC code only gives you a snapshot; calculating true RMS requires sampling over a full cycle and applying a root-mean-square algorithm. Furthermore, if your reference voltage sags under load, your conversion ratio shifts instantly.

Below is a quick-reference table showing the ±20% neighboring values around our 2.50V target. This is highly useful when setting up window comparators or threshold interrupts on microcontrollers like the ESP32 or STM32.

Table 1: Voltage-to-Code Conversion (±20% Range of 2.50V)
Input Voltage (V) 12-Bit Code (3.3V Ref) 16-Bit Code (5.0V Ref) Delta from Target
2.00V248126214-20.0%
2.10V260527525-16.0%
2.30V285330146-8.0%
2.50V310032768Target
2.70V334835389+8.0%
2.90V359638011+16.0%
3.00V372039321+20.0%

Resolution Shifts: 3.3V Logic vs. Mains Scaling (120V/230V)

A common point of confusion for DIYers moving from bench sensors to home energy monitors is how the ADC conversion shifts when measuring 120V vs 230V vs 3-phase mains. The ADC silicon itself does not care about 120V or 230V. It only sees 0V to 3.3V (or 5V). The shift happens entirely in the Analog Front End (AFE) scaling factors.

When measuring mains, you must step down the voltage and isolate it using an amplifier like the TI AMC1301. Here is how the physical-to-code mapping shifts based on the grid topology:

Table 2: AFE Scaling Factors for Mains ADC Measurement
Grid Topology Nominal RMS Peak Voltage Required AFE Divider Ratio ADC Code for 1 Nominal RMS
US Split-Phase (L-N)120V169.7V1:52 (to yield ~3.2V peak)22938 (16-bit @ 5V)
EU/UK Single Phase230V325.3V1:100 (to yield ~3.25V peak)22938 (16-bit @ 5V)
3-Phase (L-N)230V325.3V1:10022938 (16-bit @ 5V)
3-Phase (L-L)400V565.7V1:175 (to yield ~3.23V peak)22938 (16-bit @ 5V)

Notice that the ADC Code for 1 Nominal RMS remains constant in the table above because the AFE divider is specifically chosen to map the peak voltage of that specific grid to the ADC's maximum input range. If you take a US-calibrated 120V sensor (1:52 divider) and plug it into a 230V EU outlet without changing the resistor network, the ADC will saturate at code 65535 during the peaks, rendering your RMS calculations completely meaningless due to clipping.

When Voltage-to-Code Conversions Become Meaningless

Theoretical math assumes perfect hardware. In practice, several embedded realities will break your conversion formula:

  • Vref Noise Floor: If your 5.0V reference has 15mV of switching noise from a nearby buck converter, your 16-bit ADC (where 1 LSB = 76µV) is effectively blinded. The noise spans roughly 197 LSBs, reducing your 16-bit converter to an effective 9-bit resolution. Always use an LDO or LC filter for Vref, as detailed in Analog Devices' DC accuracy guides.
  • Internal ESP32 Non-Linearity: The internal 12-bit ADC and 8-bit DAC on the ESP32-WROOM-32 are notoriously non-linear near the GND and VDD rails. If you try to convert 0.15V using the standard formula, your physical reading may be off by 10% to 15%. For precision DC work, bypass the internal silicon and use an external I2C ADC DAC combo like the ADS1115 ($2) and MCP4725 ($1.50). See the official Espressif ADC documentation for the required software calibration eFuse lookup tables if you must use the internal ADC.
  • Bipolar Encoding Mismatches: If you are measuring current via a shunt resistor, the voltage swings positive and negative. A unipolar ADC will clip the negative half-cycle. You must either bias the signal to Vref/2 (shifting your zero-code to 32768 on a 16-bit system) or use a true bipolar ADC configured for two's complement output.

FAQ: ADC/DAC Math Edge Cases

Q: Do true "combo" ICs calculate this internally?
A: No. Even advanced software-configurable I/O chips like the Maxim MAX1407 or Analog Devices AD74413R output raw register values over SPI. The microcontroller's firmware must still apply the Vref and bit-depth math. The advantage of combo ICs is that they share a single, highly stable internal bandgap reference for both the ADC and DAC paths, eliminating the channel-to-channel drift you get when pairing two separate $2 breakout boards.

Q: How do I convert a DAC code back to voltage for a closed-loop PID?
A: Invert the formula: V_out = (Code / (2^n - 1)) × V_ref. However, remember that DACs suffer from settling time. If you write code 32768 to an MCP4725, the output will not hit exactly 2.500V instantaneously; it will slew at roughly 0.5V/µs. Your ADC must wait for the DAC's settling time (typically 5µs to 10µs) plus the ADC's acquisition time before sampling the feedback loop, or your PID derivative term will amplify the slew noise.

Q: What happens if my input voltage exceeds Vref?
A: The ADC will saturate at the maximum code (e.g., 4095 for 12-bit, 65535 for 16-bit). More dangerously, if the voltage exceeds the absolute maximum rating (usually Vref + 0.3V or VDD + 0.3V), the internal ESD protection diodes will forward-bias, injecting current into the microcontroller's power rail and potentially bricking the IC. Always use a clamping diode or a simple resistor-Zener network on the ADC input pin.