Skip to main content
MLemo.1
Associate III
Jul 10, 2020 9:55 PM UTC
Question

ADC read from multiple pins? STM32F411

Hello,

I'm using cubeIDE for the whole setup and I'm trying to read a voltage from 2 different pins now when using:

HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
 
ADCPin1Val = HAL_ADC_GetValue(&hadc1);

where hadc1 pointer refers to the first pin I set.

But how do you set the reading channel to be from another pin lets say A2?

This topic has been closed for replies.

7 replies

TDK
Jul 10, 2020 11:00 PM UTC

Here is one example for reading multiple channels with the LL library:

https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Projects/STM32F411RE-Nucleo/Examples_LL/ADC/ADC_MultiChannelSingleConversion/Src/main.c

Similar examples exists in that repository for other boards using the HAL library. You'll need to use DMA.

"If you feel a post has answered your question, please click ""Accept as Solution""."
MLemo.1
MLemo.1Author
Associate III
Jul 11, 2020 9:33 AM UTC

I can't see where it is used to read 2 channels and what is the deal with LL in comparison to HAL why'd you need to use DMA to read more than 1 adc input channel?

Tesla DeLorean
Guru
Jul 11, 2020 3:45 PM UTC

The mechanics on most STM32 is that the ADC scan quickly, more quickly than you want to service.

So you create a scan-list, have an array those go into, use standard DMA to execute that, and have an IRQ for EOC of the list.

You can pace capture with a TIM

If you need multi samples at the same instant, use multiple ADC, in dual or triple mode, which can use the same DMA method into a 2D array

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
Jul 11, 2020 3:31 PM UTC

adc has usually injected and normal channels. simpler way is to run a continuous conversion sequence on injected channels, this way you can directly read in debug mode each channel adc register and no need to setup dma.

MLemo.1
MLemo.1Author
Associate III
Jul 12, 2020 10:19 AM UTC

Well I've basically followed this tutorial on how to get it with continues conversion being off sins using it with DMA and RTOS corrupts the software and it just puts the all the values from each channel in a single DMA buffer so what you read off A1 pin say goes into ADCBuffer[0] and A2 pin reading to ADCBuffer[1] and so on using an ADCRead task in RTOS with:

HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

To refresh the reading.

Look fine and working to me but makes life a little hard for multi sampling and averaging on application that need stable reading.

MechatronikMonkey
Associate II
Jan 31, 2021 12:15 PM UTC

It is easy possible to use polling for more than one ADC Inputs! If you using CubeMX to generate code, you are not able to use all enabled channels because of a bug in CubeMX!!

Read my answer here:

https://community.st.com/s/question/0D50X00009fFZDOSA4/stm32f072-can-adc-conversion-polling-be-done-for-multiple-channel