STM32 ADC DMA Example HAL+ Interrupt, Polling (Single Channel)

In this tutorial, we’ll discuss the different possible methods to Read Analog Input With STM32 ADC. You’ll learn how STM32 ADC DMA mode works and how to also use the STM32 ADC Interrupt mode as well as the polling method for the Single-Channel Single-Conversion mode of operation.

The practical example we’ll implement in this tutorial is an STM32 LED dimmer using a potentiometer to an analog input pin and PWM to control the LED brightness. We’ll read the analog input pin (channel) in single-conversion mode using the DMA, Interrupt, and Polling methods. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 ADC DMA, Interrupt, Polling (Single-Channel Read)
  2. STM32 LED Dimmer Example (ADC Read Analog Channel)
  3. STM32 ADC Polling Example (Single-Channel Single-Conv.)
  4. STM32 ADC Interrupt Example HAL (Single-Channel Single-Conv.)
  5. STM32 ADC DMA Example HAL (Single-Channel Single-Conv.)
  6. Wrap Up

STM32 ADC DMA, Interrupt, Polling (Single-Channel Read)

We can read actually configure the STM32 ADC module to sample a single-channel in single-conversion mode using 3 different methods. Depending on the application type and requirements you can choose the best fit for your situation.

1. STM32 ADC Polling

The first of which is the polling method, in this method we’d start an ADC conversion and stop the CPU at this point to wait for the ADC conversion completion. Only after ADC conversion completion, the CPU can resume the main code execution.

2. STM32 ADC Interrupt

The second method is by using interrupts, so we can trigger the ADC in order to start a conversion and the CPU continues executing the main code routine. Upon conversion completion, the ADC fires an interrupt and the CPU is notified so that it can switch the context to the ISR handler and save the ADC conversion results.

Despite being an efficient way, the interrupt method can add so much overhead to the CPU and cause very high CPU loading. Especially when you’re doing so many conversions per second.

3. STM32 ADC DMA

And here comes the third method which is using the DMA unit that can directly transfer the ADC result from the peripheral to the memory in the manner that you want and program it to. All that being done without any CPU intervention and upon DMA transfers completion to maybe a 1kb in length buffer, it can notify the CPU to process the data of whatever.


STM32 LED Dimmer Example (ADC Read Analog Channel)

In this example project, we’ll create an STM32 LED Dimmer using ADC & PWM to read an analog input from a potentiometer to control the brightness of a PWM output going to an LED. This demo will run the STM32 ADC in single-channel single-conversion mode using 3 different ADC reading techniques (DMA, Interrupt, and Polling).

  • Set up a new project as usual with system clock @ 72MHz
  • Set up An Analog Input Pin (Channel 7) In single-Conversion Mode (The Pot. Pin)
  • Set up Timer2 in PWM mode with output on channel 1 (The LED Pin) 

The Application will have 3 versions each does the same thing which is read the ADC result and move it to the timer CCR register which decides the PWM duty cycle percentage on the output LED pin.

Example 1, ADC is used in blocking mode (polling)

Example 2, ADC is used in non-blocking mode (interrupt)

Example 3, ADC is used in non-blocking mode (DMA)


STM32 ADC Polling Example (Single-Channel Single-Conv.)

In this LAB, our goal is to build a system that initializes the ADC with an analog input pin (channel 7). And also configure a timer module to operate in PWM mode with output on channel 1 pin (LED pin). Therefore, we can start an ADC conversion and map the result to the PWM duty cycle, and repeat the whole process over and over again.

And now, let’s build this system step-by-step

Step #1

Open STM32CubeMX, create a new project, and select the target microcontroller.

Step #2

Configure The ADC1 Peripheral, Enable Channel-7 & Set it to be triggered by software. You’ll find that the analog channel has these default configurations which happens to be ok for us in this example project.

STM32 ADC Example HAL Code And CubeMX Configurations

Step #3

Configure Timer2 To Operate In PWM Mode With Output On CH1.

STM32 ADC Read Example HAL Code

Step #4

Go to the RCC clock configuration page and enable the HSE external crystal oscillator input.

STM32 RCC External Clock Selection CubeMX

Step #5

Go to the clock configurations page, and select the HSE as a clock source, PLL output, and type in 72MHz for the desired output system frequency. Hit the “ Enter” key, and let the application solve for the required PLL dividers/multipliers to achieve the desired clock rate.

STM32-Blue-Pill-Proteus-Library-Simulation-Clock-Configuration

Step #6

Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.

STM32 ADC LED Dimmer Example Code (Polling)

Here is The Application Code For This LAB (main.c)

STM32 ADC LED Dimmer Wiring

STM32 ADC DMA Example HAL Code And CubeMX Tutorial

STM32 ADC LED Dimmer Testing Demo

STM32 ADC LED Dimmer Another Testing

I did configure a GPIO pin to be an output pin and programmed it to toggle HIGH at the beginning of ADC conversion and drive the pin LOW after the polling function immediately. This is to approximately measure the conversion time. Which came out around 3.4μs.

STM32 ADC Conversion Time Measurement - Sampling Rate Estimation


STM32 ADC Interrupt Example HAL (Single-Channel Single-Conv.)

The Exact Same Steps As The Previous Example Except For Step 2. The ADC Configuration Will Be As Follows:

All ADC settings will remain the same but we’ll need to enable the interrupt from the NVIC controller tab.

STM32 ADC Example HAL Code And CubeMX Configurations Tutorial

Generate The Project & Open It In The CubeIDE

STM32 ADC LED Dimmer Example Code (Interrupt)

Here Is The Application Code Using The Interrupt Method


STM32 ADC DMA Example HAL (Single-Channel Single-Conv.)

Also The Exact Same Steps As The First Example Except For Step 2. The ADC Configuration Will Be As Follows:

Everything in ADC configurations will be as default in normal mode. However, this time the STM32 ADC interrupts are not activated and the DMA is configured instead and the DMA interrupt is enabled by default in the NVIC controller tab. The STM32 DMA configurations for the ADC will be as shown down below. Just add a DMA channel and that’s all.

STM32 ADC DMA Example HAL Code And CubeMX Configurations Tutorial

Generate The Project & Open It In The CubeIDE

STM32 ADC LED Dimmer Example Code (DMA)

Here Is The Application Code Using The DMA Method


Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.


Wrap Up

In conclusion, we’ve explored the various methods of reading the STM32 ADC single-channel in single-conversion mode using: (DMA, Interrupt, Polling). You can build on top of the examples provided in this tutorial and/or explore the other parts of the STM32 ADC tutorials series for more information about the othe STM32 ADC operating modes and conversion schemes.

This Tutorial is Part of The Following Multi-Part Tutorial Series:

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

4 thoughts on “STM32 ADC DMA Example HAL+ Interrupt, Polling (Single Channel)”

  1. In Polling and Interrupt mode, i am getting the 12-bit output in STM32F429ZI, but in DMA mode i am getting 8-bit output(max value 255 at 3.3V). I checked the ADC DR register in debug mode, the value is 12-bit. I do not understand the reason. need help.

    Reply
    • You need to set in the DMA configurations the word size for each transfer to be 2bytes i think this could be the reason

      Reply

Leave a Comment