STM32 ADC Multiple Channels
Today in this tutorial, we will see how to read multiple channels in ADC in STM32.
For this demonstration, I am using STM32F103C8 controller and True-Studio IDE.
For the ADC purpose, I am using 3 channels as mentioned below:-
- CHANNEL 0 –> IR sensor
- CHANNEL 1 –> Potentiometer
- CHANNEL 16 –> Internal Temp sensor
UPDATE
If the ADC Frequency is very high, the DMA Interrupt will execute at very high rate, and this will render the while loop Useless.
There is a different way to handle multiple channels without the DMA and you can check it at https://controllerstech.com/stm32-adc-multi-channel-without-dma/
HOW TO
In order to read the multiple channels, we need to use DMA. The benefit of it is the conversion will take place in the background and we can perform some other operation with the controller and when we need the values, we can just read them easily.
Before proceeding further, Let’s see How we have to get the value from the internal temperature sensor of the STM32. According to the reference manual (Pg- 236), The temp is given as follows
V25, Avg_Slope are given in the datasheet (Pg-79)
Also note that the ADC sampling time, while reading the temperature, needs to be maximum 17.1 us.
CubeMx Setup
- Above is the clock section from the CubeMx. Note that I have selected the ADC clock as 14 MHz.
- The reason behind this is that the Temp sensor sampling time needs to be 17.1 us.
- While in the ADC setting, we have maximum sampling time as 239.5 cycles. So (239.5/14) gives us 17.1 us sampling time.
Note that 3 channels are selected and the Continuous conversion mode is enabled. Also in the new CubeMx, you don’t need to worry about scan conversion mode. It will enable and disable by it’s own, based on if you are using multi channels or only one channel.
Above is the DMA setting for the ADC. Make sure that the DMA is circular and data width is selected as WORD or HALF WORD . This is because the CubeMx uses ADC in 12 bits resolution by default and in order to store 12 bits we need the variable of the same size.
Some Insight into the Code
uint32_t value[3];
HAL_ADC_Start_DMA(&hadc1, value, 3); // start adc in DMA mode
- Starts the ADC in DMA mode and the converted data is stored in ‘value‘ buffer.
- ‘3’ is the length of data to be transferred from ADC peripheral to memory.
float temp;
#define V25 1.43 // from datasheet
#define VSENSE 3.3/4096 // VSENSE value
#define Avg_Slope .0043 // 4.3mV from datasheet
float get_temp (uint32_t variable)
{
return (((V25 - (variable*VSENSE)) / Avg_Slope) + 25); // formula from datasheet
}
The above code gets the temperature of the sensor in °C. You can change the values of V25 and Avg_Slope according to your controller’s datasheet.
9 Comments. Leave new
While(1) loop does not execute in this mode
Reduce the ADC clock. Also increase the sampling time..
Hi, I would like to know how you can send multiple ADC in single buffer and transmit over UART.
yes you can. But depending on the ADC resolution, you have to manipulate data.
For example, if the resolution is 12 bit, you can send 2 ADC values (totally 24 bits) in 3 bytes of the UART.
Of course the data must be converted to ascii format (characters) first
It works well for my project,
Thank you very much
Using ADC with DMA
1) I Connected 2 ADC channels.
2) And Created a timer function for 1 minute
3) Whenever the timer call back executes, I have to start my ADC & read the ADC value for all the mentioned channels & print in the serial port (Should not use delay function)
4) After execution, it should not measure the ADC, until it reaches the 1-minute interval
In this, I have to start my ADC with DMA in Timer Callback, I have to print ADC values in serial port.
I configure the ADC, DMA and Timer in proper way.
but In this i got the output as ADC channel 0 value as 210, and channel 1 as 0.
I don’t know where did my mistake.
Can u please help me to solve this issue?
HI!
Very fine example.
What I do for changing channel is to reinitiate the DAC, for the next measurement, for instance:
Startup:
/* USER CODE BEGIN SysInit */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE END SysInit */
…….
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_RTC_Init();
……
/* USER CODE BEGIN 2 */
HAL_ADCEx_Calibration_Start(&hadc1);
/* USER CODE END 2 */
sConfig.Channel = ADC_CHANNEL_VREFINT;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
// HAL_ADCEx_Calibration_Start(&hadc1); // advisable to do a self calibrate at startup, you get 1.5 digits better performance
HAL_ADC_Start(&hadc1);
// WAIT FOR EOC
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
// GET VALUE
raw = HAL_ADC_GetValue(&hadc1);
Hi, thank you very much from your site
I want to do sampling at 10 kHz ADC and DMA
Maybe guide me ?
sampling freq = ADCCLOCK/Cycles. U know the freq, chose the other 2 according to that