cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to get a valid ADC-value

FEich.1
Associate II

Hello everyone,

at first the Discoveryboard I'm using is the P-L496G-Cell02 / STM32L496G-DISC.

To test the ADC I tried to recieve a value from a potentiometer.

So I started a new STM32CuvbeIDE project, Selected the board I'm using and changed the Pins (via the CubeMX Interface) to the correct Input. I tried it several times with different pins:

PC4 -> ADC2_IN13

PC1-> ADC1_IN2

PC3-> ADC1_IN4

And i changed the associated channel to Single-ended.

In the main fuction, i declared a variable raw and added the code in the while-loop

int main(void)
{
	uint16_t raw;
	HAL_Init();
	SystemClock_Config();
	MX_ADC2_Init();
	while (1)
	{
    /* USER CODE END WHILE */
	    HAL_ADC_Start(&hadc2);
	    HAL_ADC_PollForConversion(&hadc2, HAL_MAX_DELAY);
	    raw = HAL_ADC_GetValue(&hadc2);
	    HAL_Delay(2);
    /* USER CODE BEGIN 3 */
	}
  /* USER CODE END 3 */
 
}

As a result raw is always 4095 (3.3V) even if i unplug the cable to the potentiometer.

I hope somebody can help me with this problem

11 REPLIES 11
Mc3
Associate II

I had a similar matter with a stm32h743zi , my adc's buffer always returned the same value even if it was not plug. It was solved when I created a project on cubeMX 5.6.1

KnarfB
Principal III

Can you share the generated code too?

TDK
Guru

> MX_ADC1_Init();

> ...

> HAL_ADC_Start(&hadc2);

The first function initializes ADC1, while the second (tries to) start ADC2 but it isn't yet initialized. That's not going to work.

If you feel a post has answered your question, please click "Accept as Solution".

Sry my bad. Copied the wrong line, I initialized ADC1 and also 2 in my code. I will edit it in the first post

I will send the generated code later when i'm back on my PC.

TDK
Guru

Did you measure the voltage on the pin with a multimeter? I don't think the bug is in the code you showed.

Forget the potentiometer and hook it up directory to GND and 3V3 until you get the expected result, then add the potentiometer.

If you feel a post has answered your question, please click "Accept as Solution".
FEich.1
Associate II

I connected it directly to the GND and the same value appeared.

FEich.1
Associate II

This is the main with all the peripherals in their default mode (created by the IDE).

PC4 ->with AD12_IN13

TDK
Guru

In the file you posted, the only ADC converting channel 13 is ADC3.

ADC1 is converting channel 4 and ADC2 is converting channel 9.

The pin initialization is found in stm32l4xx_hal_msp.c.

If you feel a post has answered your question, please click "Accept as Solution".