Skip to main content
FEich.1
Associate II
August 22, 2020
Question

Unable to get a valid ADC-value

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

This topic has been closed for replies.

11 replies

Mc3
Associate II
August 22, 2020

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
Super User
August 22, 2020

Can you share the generated code too?

FEich.1
FEich.1Author
Associate II
August 22, 2020

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

TDK
August 22, 2020

> 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""."
FEich.1
FEich.1Author
Associate II
August 22, 2020

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

TDK
August 22, 2020

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
FEich.1Author
Associate II
August 22, 2020

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

FEich.1
FEich.1Author
Associate II
August 22, 2020

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

PC4 ->with AD12_IN13

TDK
August 22, 2020

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""."
FEich.1
FEich.1Author
Associate II
August 22, 2020

So here is the hal_msp

FEich.1
FEich.1Author
Associate II
August 24, 2020

Today I tried several things:

1.Changed the ADC2 regular Channel to 13 and got the same result.

sConfig.Channel = ADC_CHANNEL_13;

2. Connected PC4 to the potentiometer ->4095

3. Connected PC4 to ground ->4095

4. Connected PC4 to 3.3V ->4095

5. Connecteed PC4 to ground with an resistor in between -> showing 0 and 4095 in irregular intervals

6. Generate the code with an older MX-Version 5.6.1 ->4095

7.Tried the code snippet I posted in the first post on an other discoveryboard (stm32f4) with the generated code from the CubeMX->Everything is working as it should