HomeSTM32 TutorialsSTM32 ADC SeriesSTM32 ADC Part 11 – How to use ADC in Differential Mode

How to use STM32 ADC in Differential Mode

In this tutorial, we’ll explore how to use the ADC differential mode on STM32 microcontrollers to measure the voltage difference between two analog inputs instead of a single-ended signal relative to ground. Differential mode is especially useful for applications requiring noise rejection, precise small-signal measurement, and bidirectional sensing. You’ll learn how to bias the inputs, respect the ADC’s common-mode voltage requirements, configure the differential inputs in STM32CubeMX, and convert the raw ADC result into a meaningful differential voltage reading.

Recommended Resources:

This is the 11th tutorial in the STM32 ADC series. In the previous tutorials we covered how to configure the ADC in STM32 F1, F4 and H7 series and how to use it in the single channel polling, interrupt and DMA modes to read the potentiometer data. We have also covered the Multiple channels in DMA Normal Mode, Circular Mode and Multiple Channels without DMA.

You do not need to go through the previous tutorials in order to understand this one.

How to use STM32 ADC in Differential Mode

What is Differential ADC in STM32?

Differential ADCs are generally used in applications requiring high precision, noise reduction, and increased dynamic range. In this tutorial we will specifically focus on just measuring the voltage difference between the two differential inputs. The ADC data will be based on this differential voltage, which we will later convert into the voltage.

Not all the STM32 devices support differential ADC. I am going to use the STM32H7 based custom development board.

As mentioned in the reference manual both the differential inputs should be biased at Vref/2. The Vref is a custom reference voltage which can be provided externally, but the hardware on the board should be configured accordingly. By default the Vref is connected to the VDDA (& VCC), therefore the Vref is 3.3V.

Biasing means that instead of fluctuating around 0V, the signals should be centred around 1.65V.

Differential ADC explained

We should also take care of the Common mode voltage (CMV). This Common-mode voltage is the average voltage of two input signals in a differential system. This voltage only has a small range in STM32. You can see it in the image from datasheet of the device.

Common mode input voltage

You can see the common mode voltage varies by only 10% from Vref/2 voltage. So if the Vref is at 3.3V, the common mode voltage can vary from around 1.48 V to 1.82V.

CMV Range

When choosing the input voltages on both the differential pairs make sure the average of both voltages lies within this range. It does not mean that it will not work outside this range rather the result outside this range is unpredictable. In certain cases it might work while in other cases it won’t.

The ADC will convert the voltage difference between both the input pins as per the formula shown below.

Converted voltage
  • If the VINN = 0 and VINP = VREF, the ADC value will be equal to the MAXIMUM.
  • Whereas when VINN = VREF and VINP = 0, the ADC value will be equal to the 0.
  • When VINN = VINP, the ADC value will be exactly HALF the MAXIMUM.

Below is the image showing the range of ADC value based on the differential inputs. The VDIFF is the voltage difference between the input signals.

Voltage Range

We will write our code in such a way that the ADC MAXIMUM (65535) indicates a +3.3V while the ADC MINIMUM (0) indicates a -3.3V. Also the ADC MAX/2 (32768) will indicate 0V.

Why Do We Need Differential ADC Mode?

In many real-world applications, measuring a signal with respect to ground is not enough. Signals often ride on top of noise, ground offsets, or common-mode voltages that can significantly affect the accuracy of single-ended ADC measurements. Differential ADC mode solves this problem by measuring the voltage difference between two input pins, effectively rejecting noise that is common to both signals.

One of the biggest advantages of differential mode is noise immunity. Any interference picked up equally on both input lines—such as switching noise, EMI, or ground fluctuations—is largely canceled out during conversion. This makes differential ADC ideal for environments with motors, switching regulators, or long sensor cables where noise is unavoidable.

Differential mode is also extremely useful when working with low-level or bipolar signals. Sensors such as current shunts, strain gauges, pressure sensors, and certain audio or industrial sensors often produce very small voltage differences that can be easily lost in noise when measured single-ended. By directly measuring the difference between the two inputs, the ADC can capture these small changes more accurately.

Another important use case is when the signal is not referenced to ground. In such cases, forcing a single-ended measurement can lead to incorrect readings or even violate ADC input limits. Differential ADC mode allows the signal to float within a valid common-mode range while still providing a correct measurement of the voltage difference.

STM32 CubeMX Configuration

Before moving to the code, let’s quickly look at the STM32CubeMX configuration. In this case, there isn’t much to configure manually, most of the work is simply enabling the ADC and selecting the required channel in differential mode.

ADC Differential Configuration

ADC Channel 3 is configured to operate in differential mode. In this configuration, pin PA6 is selected as the INP (positive) input, while pin PA7 is assigned as the INN (negative) input. The ADC conversion result therefore represents the voltage difference between PA6 and PA7, rather than the voltage of a single pin with respect to ground.

The ADC is set to continuous conversion mode, which allows a new conversion to start automatically as soon as the previous one completes. This ensures a steady stream of differential measurements without requiring software to restart each conversion manually. Additionally, the ADC interrupt is enabled so that the CPU is notified immediately when a conversion is complete, making it easier to process or log the converted data in real time.

Wiring Diagram

The Connection for this project is shown in the image below.

STM32 Connection

I am using Active Pro Debugger to provide variable voltages to the ADC input. The A1 pin is connected to the PA6 (+ve Input) whereas the A0 pin is connected to the PA7 (-ve Input).

STM32 HAL Code for differential ADC

uint16_t ADC_VAL;
int voltage;

The variable ADC_VAL will be used to store the Raw ADC value. This value is generated by the ADC based on the difference in voltage between the inputs. The variable voltage will store the voltage data based on the Raw value.


Inside the main function we will start the ADC in interrupt mode. Once the conversion is finished, an interrupt will trigger and the ConvCpltCallback will be called. Inside this callback we will simply set the variable isDone. The data will be processed in the while loop.

int isDone =0;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
	isDone = 1;
}

Below is the main function.

int main ()
{
  ....
  ....
  HAL_ADC_Start_IT(&hadc1);
  while (1)
  {
    if (isDone)
    {
	  ADC_VAL = HAL_ADC_GetValue(&hadc1);
	  voltage = (3300*2*ADC_VAL/65535)-3300;
	  isDone = 0;
    }
    HAL_Delay(250);
  }
}   

Inside the while loop we will check if the variable isDone is set. If it is, we will read the Raw ADC value.

Then convert this Raw value to the voltage. By default the Vref is connected to VDDA (&VCC), therefore the Vref = 3.3V. Hence the value 3300 is used here. We want to convert the RAW ADC value to the voltage ranging from +Vref to -Vref, hence this formula is used baed on the examples provided by ST.

Result of the STM32 ADC Differential Mode

Below are the images showing the output of the above code. When the +ve Input is at 0V and -ve Input is at 3.3V, the average input voltage is 1.5V, which is within the Common Mode Voltage (CMV ) range. The Raw ADC value is near 0 and the converted voltage (Difference between INP and INN) is -3.3V.

ADC Differential voltage measured

When the +ve Input is at 3.3V and -ve Input is at 0V, the average input voltage is 1.5V, which is within the Common Mode Voltage (CMV ) range. The Raw ADC value is 65535 and the converted voltage (Difference between INP and INN) is 3.3V.

ADC Differential voltage measured

When both the +ve Input and -ve Input are equal at 1.6V, the average input voltage is 1.6V, which is within the Common Mode Voltage (CMV ) range. The Raw ADC value is near Half the MAX and the converted voltage (Difference between INP and INN) is 0V.

ADC Differential voltage measured

When the +ve Input is at 3V and -ve Input is at 1V, the average input voltage is 2.5V, which is far outside the Common Mode Voltage (CMV) range. The ADC behaviour is unpredictable at this point. You can see in the image below the converted voltage is wrong.

ADC Differential voltage measured

Hence while designing the differential ADC, we must take care that the average input voltage of the two inputs must lie within the CMV range.

Video Tutorial

STM32 ADC Differential Mode – Video Tutorial

Along with the written explanation and configuration details, this video provides a complete walkthrough of STM32 ADC differential mode in action. You’ll see how the differential inputs are configured in CubeMX, how the conversions are performed, and how the results change based on the voltage difference between the two input pins. Watching the implementation alongside this guide will help reinforce the concepts and clarify subtle configuration details.

Watch the Video

Conclusion

In this tutorial, we covered how to use ADC differential mode in STM32 microcontrollers to measure the voltage difference between two analog inputs. We looked at why differential mode is needed, how it helps with noise rejection and small-signal measurement, and how to configure the ADC and pins correctly using STM32CubeMX. Practical configuration details and real hardware connections helped demonstrate how differential conversion works in practice.

ADC differential mode is especially useful in applications where accuracy and noise immunity are critical, such as current sensing, sensor interfaces, and industrial measurements. While it requires a bit more care in configuration and signal routing, the improvement in measurement reliability often outweighs the added complexity. Understanding when and how to use differential ADC mode allows you to build more robust and precise embedded systems.

Browse More STM32 ADC Tutorials

STM32 Differential ADC Project Download

Info

You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.

STM32 Differential ADC FAQs

Subscribe
Notify of

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
damar
10 months ago

Hi, can we use DACA

×