cancel
Showing results for 
Search instead for 
Did you mean: 

Failing to read useful data from ADC of STM32f411

Qwyntex
Associate III

Hello,

upfront, it might be an incredibly dumb problem on my behalf as I am fairly new to this.

For a couple days now I have been trying to read out the voltage through the onboard ADC configured for 12 bits but all I am getting back are values in a similar range (mostly somewhere in between 1500 and 1800 but that changes with the amount of delay that I introduce, does that help?). The value itself does change, floating within that range.
I have tested for faulty connections and switched the board but the issue persists.

It is a very basic setup, just a potentiometer connected to the A0, and it works if I use an Arduino with its framework to try and read the value (the potentiometer is not broken). But also supplying A0 with ground or 3.3v directly doesn't change the read value. After reading the value I write it to serial, which is also working.

My guess is that something with my clock configuration is wrong and the ADC has not enough time to run a meaningful measurement.

I hope that any of you can help, thank in advance!

 

the main loop (is there any better way to format code blocks?):

while (1)
{
  HAL_ADC_Start(&hadc1);
  HAL_Delay(500);
  HAL_ADC_PollForConversion(&hadc1, 100);
  value_adc = HAL_ADC_GetValue(&hadc1);
  HAL_ADC_Stop(&hadc1);

  // value_adc, message and len are defined outside of the main function
  sprintf(message, "value: %hu\r", value_adc);
  while(CDC_Transmit_FS(message, len) != USBD_OK);
  memset(message, '\0', len);


  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
}

I used CubeMx to generate the pretty much all other code, besides a tiny bit in the CDC_Receive_FS function for communication. Here is my ADC configuration:

config.png

PCLK2 is configured for 14Mhz

1 ACCEPTED SOLUTION

Accepted Solutions
Qwyntex
Associate III

I was testing the other boards and while for none of them the PA0 worked, neither did PB15. I was suspicious because I could swear when I had tested that one on that specific board it had worked.

To make it short, the connection on the boards site was faulty and, regrettably the only one I hadn't thought about testing. Never going to use solderless connections ever again (that's probably not true, they are convenient).

A bit unlucky that that specific pin and all other pins that I tested from port 'A' didn't connect properly.

Anyways, thank you for your help, without it I would probably not have reconnected the boards. Everything works now!

View solution in original post

7 REPLIES 7
TDK
Guru

It sounds like the pin you're monitoring isn't changing in value. What board is this and how is that pin hooked up? Look at the schematic to verify connectivity between pin and header. Set PA0 as an output, toggle and monitor to establish that you're monitoring what you think you are monitoring.

Unlikely to be any issues in the ADC setup.

> is there any better way to format code blocks

Click the "..." in the editor and then click "</>" to insert a code sample.

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

Is the ADC enabled ?

Once it is working, increase the sampling time to >100 cycles for better accuracy

Qwyntex
Associate III

You were correct and apparently I cannot use port 'A'? I've tested different ones and all of them do not respond. Just to make sure that the board isn't broken I tested a pin on port 'B' (specifically PB15 if that matters) and it works as expected. The onboard LED works as well. I connected an LED to the pin and set it to GPIO_Output.

I am using a blackpill with this pinout.Is there maybe something special about the 'A' ports? Do I have to enable them somehow?

> Is there maybe something special about the 'A' ports? Do I have to enable them somehow?

Not really. You have to enable the clock like any other port, but GPIOA clock is enabled by default after reset and the CubeMX code should enable it anyway.

If you set PA0 (and PA1, etc..) as output and it's not able to be toggled, the hardware is bad. Most likely due to an overvoltage event.

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

But I had tried three different boards (including different chips and different vendors) before coming here, all the same. I will test them again, but I doubt I will find anything conclusive.

You don't think that maybe I have a broken usb port or STLink (I am not using both at the same time) that supplies too much power practically immediately damaging the boards?

Unlikely three boards are busted, or a problem with the ST-Link. Can you get an example ADC project to run?

Can't seem to find a working schematic for that board. Some sources suggest PA0 is tied to the button.

Nucleo boards are cheap, and work. Might consider getting one of those instead.

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

I was testing the other boards and while for none of them the PA0 worked, neither did PB15. I was suspicious because I could swear when I had tested that one on that specific board it had worked.

To make it short, the connection on the boards site was faulty and, regrettably the only one I hadn't thought about testing. Never going to use solderless connections ever again (that's probably not true, they are convenient).

A bit unlucky that that specific pin and all other pins that I tested from port 'A' didn't connect properly.

Anyways, thank you for your help, without it I would probably not have reconnected the boards. Everything works now!