cancel
Showing results for 
Search instead for 
Did you mean: 

ADC read from multiple pins? STM32F411

MLemo.1
Associate III

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?

7 REPLIES 7
TDK
Guru

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
Associate III

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?

There are 3 channels and the data are stored in a aADCxConvertedData array. You can do the same with HAL also.

P.S. Well HAL doesn't work reliably at all for any peripheral and LL is useless, but that's another story...

S.Ma
Principal

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.

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
Up vote any posts that you find helpful, it shows what's working..

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

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