STM32 ADC | PART 9 Oversampling Technique

Boost STM32 ADC resolution with hardware oversampling. This is a Step-by-step guide for STM32, including CubeMX config, VM shifts, sampling, and HAL code demo. The project is available to download at the end of this post.

STM32 ADC Oversampling Technique

Recommended Resources:

This is the 8th tutorial in the STM32 ADC series. In the previous tutorials we covered how to configure the ADC in STM32 F1, F4 and F7 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 with DMA.

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

Introducing STM32 ADC Peripheral

The Analog-to-Digital Converter (ADC) in STM32 microcontrollers is a powerful peripheral that allows the conversion of analog input signals into digital values. This capability enables STM32 devices to interface seamlessly with real-world sensors and analog inputs, such as temperature sensors, light sensors, potentiometers, and more. The ADC module is highly configurable and supports a range of resolutions and modes, making it suitable for both basic and advanced embedded applications.

Whether you’re developing a battery-powered IoT device or a high-speed data acquisition system, the STM32 ADC provides flexible configuration options, precision control, and efficient performance.

Here are some important features of STM32 ADC:

  • Multi-Channel Support – Capable of reading from multiple analog inputs using internal channel scanning, reducing the need for external hardware.
  • High Resolution Options – Offers selectable resolutions (6-, 8-, 10-, or 12-bit), allowing a balance between accuracy and conversion speed.
  • Multiple Conversion Modes – Supports single, continuous, scan, and discontinuous modes to suit different application needs.
  • Flexible Triggering – Conversion can be started via software or automatically triggered by hardware events like timers or external interrupts.
  • Low Power Consumption – Designed with power-saving features, ideal for energy-efficient and battery-operated systems.

WHAT IS OVERSAMPLING?

All STMicroelectronics microcontrollers embed an ADC (analog-to-digital converter) with a given resolution (number of bits) and sampling rate. For most applications, this resolution is sufficient, but in some cases where a higher accuracy is required, oversampling, and decimating the input signal can be implemented to avoid the use of an external ADC solution and the associated increase in application power consumption.

The hardware oversampling engine accumulates the results of ADC conversions. The accumulated output data can be right-shifted (and rounded) to provide selected bit-depth in relation to OSR. The output value is not updated every sampling period, but once N samples are accumulated, therefore, the output data rate is decimated by a factor of OSR.

In some of the STM32 MCUs (For eg- STM32L496), the oversampling engine keeps the 16 least significant bits after the shift, and rounds the result to the nearest value according to the bits removed by the shifting. The final result is saved in the ADC_DR data register and because of the 16-bit truncation, it cannot be represented on more than 16 bits. While in other STM32 MCUs (For eg- STM32H750), the bit shifting and truncation is optional. These MCUs has 32-bit wide ADC_DR register, therefore storing higher resolution data in the data Register is possible.

Below is the formula to calculate the bit depth after oversampling.

ADC Oversampling Formula

Here X represents the number of bits on which the samples are accumulated. OSR is the Oversampling ratio and M represents the number of bits to be shifted.

Note:- The output value is not updated every sampling period, but once N samples are accumulated, therefore, the output data rate is decimated by a factor of OSR. Therefore our formula to calculate the sampling frequency / conversion time changes compared to what we discussed in the previous tutorial.

CUBEMX CONFIGURATION

Clock Configuration

Below is the image showing the clock configuration for STM32L496ZG.

STM32 ADC Clock Configuration

I have configured the system clock to run at maximum 80MHz. The ADC is also configured to run at 64MHz.

ADC Configuration

Below is the image showing the ADC configuration.

STM32 ADC Configuration
  • I have configured the ADC1 CH8, which is connected to pin PA3.
  • The clock prescaler is set to 1, so the ADC clock is still at 64MHz (64MHz/PSC).
  • The Resolution is set to 12-bit. The extra samples we take, will be accumulated on 12-bit.
  • The continuous conversion mode is enabled, so the next conversion will automatically start once the previous conversion is over.
  • The DMA continuous request is enabled, so the DMA will fetch the data from the channel continuously.

Oversampling configuration

Below is the image showing the oversampling and channel configuration of the ADC.

STM32 ADC Oversampling Configuration

Here we will first enable the oversampling for the Regular channels. As I mentioned, below is the formula to calculate the oversampling bit width.

Oversampling formula

Here I am choosing the OSR as 64. Therefore 64 samples will be collected for each trigger (software start). The Right shift (M) is set to 2. This will result in the bit width of 16 bits.

Oversampling calculation

As I already mentioned, the STM32L496 has 16-bit ADC Data register, therefore we can not have the bit width higher than this value. Therefore the bit shift (M) and OSR should be selected in a way that the bit width is not higher than 16 bits.

The sampling time for the Channel 8 is set to 47.5 Cycles. With the ADC clock of 64 megahertz, the total time to convert the channel should be as shown below.

ADC Sampling Time calculation
  • Here 12.5 is the ADC clock for 12-bit Resolution.
  • 47.5 is the sampling time we selected.
  • 64MHz is the ADC clock.
  • 64 is the OSR we selected.

The total conversion time for channel 8 should be 60uS. We will measure it in the analyzer.

DMA Configuration

Below is the image showing the ADC DMA Configuration.

STM32 DMA Configuration

The DMA is configured in the Circular mode, so that it can work continuously. The data width is set to 16-bit.

WIRING DIAGRAM

Below is the image showing the connection used in this project.

oversampling pin connection

The potentiometer is connected to 3.3V with the MCU. The output pin of the potentiometer is connected to the pin PA3 (ADC1 CH8). I am going to use the pin PD7 toggle to measure the ADC conversion time, therefore it is connected to the Logic Analyzer.

THE CODE

There is nothing special in the programming section to make the oversampling work. We will simply start the ADC in the DMA mode.

uint16_t ADC_VAL = 0;

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
    HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_7);
}

int main(void)
{
  ....
  ....
  
  HAL_ADC_Start_DMA(&hadc1, &ADC_VAL, 1);
  while (1)
  {
  }
}

Here inside the main function, we will start the ADC in the DMA mode. The converted data will be stored in the ADC_VAL variable.

Once the conversion is finished, an interrupt will trigger and the conversion complete callback is called. Inside the callback we will toggle the pin PD7. This is to measure the conversion time of the ADC.

RESULT

Below is the gif showing the ADC_VAL in the live expression.

Oversampling Working

You can see in the gif above, as the potentiometer is rotated, the ADC_VAL is increasing. The interesting thing is that the maximum value of this variable is around 65000.

The STM32L496 supports a maximum of 12-bit ADC resolution. With 12-bit resolution, the maximum value of the variable should have been 4095. But because of oversampling, we are able to increase the resolution to 16-bit and that is why the value is reaching 65000.

Below is the image showing the sampling time as measured by the pin PD7.

Oversampling Timing

As you can see above, the pin toggles every 60us. This is exactly the same time as we calculated using the formula.

The point is, Oversampling increases the number of samples taken per trigger by the factor OSR but it also reduces the overall sampling frequency by same factor. Hence we need to take in consideration the OSR factor while calculating the sampling or conversion frequency.

VIDEO TUTORIAL

You can check the video to see the complete explanation and working of this project.

Check out the Video Below

PROJECT DOWNLOAD

Info

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

You May Also Like..

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

🛈 Advertising Disclosure

This website relies on advertisements as its main source of revenue to support the creation of free, high-quality content.
If you enjoy our work and would like to help us continue, please consider disabling your ad blocker while browsing here.

Your support truly makes a difference — thank you!

Share this Article

Recent Posts

Join Our Community

Weekly Newsletter

Subscribe to our newsletter to get our news