STM32 huge ADC zero offset/funky values - Page 1
Forum section: electronics / general

author: reyntjensm
author_rank: Regular Contributor
timestamp: March 09, 2020, 11:47:22 pm
content: Hello,I'm working on a project using an stm32F429zi, i'm trying to read the voltage from a voltage divider(10k resistor and LDR) with the ADC.My code is working fine but the values are off, when the input is at 0V the ADC outputs 1023(12bit measurement) whatever i do, i'm 100% sure the pin is at ground.At around 300-400mV the ADC is already at it's max value(4095), this should happen at 3,3V.I've been looking around and found out the VREFINT is the reference for the ADC, so i tried to read this out with the same ADC and the result was 4095 while the VREFINT should be around 1,2V. I'm not sure if i did this measurement correct? My analog ground an supply pins are good connected. So should my chip be broken?I had the same issue with a previous chip but locked my self out by setting all unused pins to analog so i was unable to debug it again....After soldering in a new chip(took some effort for this 144LQFP chip), would i've abused it too much so it's broken?What other things can i troubleshoot?I hope for some feedback and thank you all!

author: ifrenide
author_rank: Regular Contributor
timestamp: March 10, 2020, 12:08:43 am
content: Hello, double check polarity of voltage maybe you are sure about something but it is wrong,It seems that you reversed the polarity and the built in ADC invert results because you just think that it gives you a 1023 at 0v no, for sure it is 5v but you think that is 0v , use your multimeter and check be sure to use COM probe on the ground and Positive probe to the center of the pot .best regards.

author: reyntjensm
author_rank: Regular Contributor
timestamp: March 10, 2020, 12:14:35 am
content: Hello,Thank you for your feedback.I tested this setup first as a prototype with a nucleo and breadboard without any problems.Now i'm using this on my own PCB and things are off.I've measured the ADC input pin with a voltmeter and oscilloscope. Ground is 0V and the ADC result is 1023, i only have one ground for everything in my pcb. Could this be the problem? I'm using pwm all over the place. SPI and I2C are working fine on this chip...

author: ifrenide
author_rank: Regular Contributor
timestamp: March 10, 2020, 12:27:02 am
content: Mistakes are frequently present in PCB prototyping, I'm sure the problem is in "your PCB" because it's exteamly rare that the microcontroller is faulty. so for sure you made a mistake when prototyping the PCB . if you want send the schematic and PCB pictures to verify if there is a bug ,best regards.

author: reyntjensm
author_rank: Regular Contributor
timestamp: March 10, 2020, 12:31:37 am
content: I might send you my PCB tomorrow(i don't have it now) but it's quite large. This is the first analog part i'm using. Since it's only a simple voltage divider i would be surprised if there is a bug in my PCB. Even if i manually short the pin to ground the results are the same.What about the VREFINT measurement? Am i doing this wrong or could my chip be broken?Are there any other external pins i could measure to verify the ADC is working fine?

author: ifrenide
author_rank: Regular Contributor
timestamp: March 10, 2020, 12:55:11 am
content: I think this configuration is simple , there are only few things to check.1-be sure of the voltage polarity.2- be sure that the analog voltage is fed to the ADC pin,and there is no short circuit .3- be sure that you code code is correct,pins cofigured correctely, and fuctions are correct ...there is no way to give you inverted results . be sure that the circuit gives you what you gave to it , it is a slave and you are the master .if you didn't get what you expect , it is because you made a mistake and you just have doubts about the microctroller operating, don't wory your microcontroller is healthy because it give you a digital reading 1023 it is a good indication that it is works well.I know you are tired to check the pcb and the code, but this is how it works , evry project has mistakes and even with professional engineers ,but they just spend more time and efforts to debbug .don't be lazy and verify each point , you will find your mistake.don't forgive up .and I'm here for sure to help you

author: reyntjensm
author_rank: Regular Contributor
timestamp: March 10, 2020, 01:10:41 am
content: Thank you for your motivating feedback, you are totally right.Well i forgot to say one thing, in my PCB design i routed my voltage divider to a micrcontroller pin with no ADC functionality, yes i know it's yet again a very dumb mistake.So since this is my first prototype and there are some pretty expensive components mounted on this PCB, i wired the voltage divider to another microcontroller pin with ADC functionality, but still there is a trace going to an unused pin right now. Could this be the issue? It took me some time to realize this but i guess i have to cut the trace when i'm back at my desk.I'm also very worried about the internal VREFINT reading on the ADC1 channel 17. This should be around 1.2 volt and is now maxed out.I have to say i'm not an expert at PCB design an this is one of my first big projects. I've learned more by making this project than in my 3 year bachelor degree so i'm still motivated to keep working on this project.Great feedback/forum and i will keep you updated if i get around my mess.

author: ifrenide
author_rank: Regular Contributor
timestamp: March 10, 2020, 01:44:02 am
content: you are wellcome .you have to route the analog signal which is the voltage devider output to an ADC pin , if you didn't that , impossible to give you a result, it is a very big mistake.you have to figure out which pin is the input of the adc . and give it the output of the voltage devider and be sure that your code is correct .( but still there is a trace going to an unused pin right now) , so you are not familiar with what you did , you have to know 100%where the attachements between compenents go to.good idea to use the output of the voltage devider and test it withe other devalopment board , but be sure that they share the same ground"ground wire between the two boards.finaly , relax and take your time, step by step you will debbug your project.good nigth.

author: Harjit
author_rank: Regular Contributor
timestamp: March 10, 2020, 01:55:02 am
content: With ADC's, the source resistance is a big deal because of the way the ADC works.The ADC has an internal capacitor that they charge from the analog source. If you source resistance is large and the sampling time small, not enough charge will transfer into the internal capacitor and you'll get a mixture of the previous conversion and this one.Look for source resistance, Rain and Cain in the datasheet.

author: aheid
author_rank: Regular Contributor
timestamp: March 10, 2020, 09:30:50 am
content: Just as a sanity check, have you measured VDDA and AGND pins?

author: mikerj
author_rank: Super Contributor
timestamp: March 10, 2020, 12:18:25 pm
content: What do you have the Vref+ and Vref- pins connected to?

author: aheid
author_rank: Regular Contributor
timestamp: March 10, 2020, 12:51:22 pm
content: Quote from: mikerj on March 10, 2020, 12:18:25 pmThat was what I was thinking of, but forgot I'm used to the smaller footprints which don't have separate ref pins.So yeah, check those.

author: reyntjensm
author_rank: Regular Contributor
timestamp: March 10, 2020, 10:21:39 pm
content: Hello everyone,Thanks for the feedback.So today i've looked a bit further.I only have a Vref+ pin on my chip(STM32F429zit in 144LQFP) and this is 3,3V so this is correct.VDDA and AGND are good.I tried to measure all my unused pin's with all possible ADC's.Every pin resulting in a 4095 ADC measurement. I guess a floating pin should give a result closer to 0?Again my VREFINT was also 4095, so i guess my internal voltage reference is broken? But why are all the other things within my chip working like a charm?I my VREFINT measurement incorrect?

author: thm_w
author_rank: Super Contributor
timestamp: March 10, 2020, 11:31:05 pm
content: There is no way to know what a floating pin would be. Use the internal pull-up or pull-down to set it at some level before measurement. Maybe you can post some of the code.Vrefint should be around 1490 (1.2V).

author: newbrain
author_rank: Super Contributor
timestamp: March 12, 2020, 04:29:54 pm
content: About measuring Vrefint: if you are not using the HAL, you must make sure to:Use ADC1Set the TSVREFE bit in ADC_CCRReset the VBATE bit in ADC_CCRIf you are using the HAL, this is taken care of automatically when setting the channel configuration for channel 17 (still, make sure to use ADC1).

author: reyntjensm
author_rank: Regular Contributor
timestamp: March 14, 2020, 03:13:06 pm
content: i'm using HAL. So if my microcontroller would be ok VREFINT should be around 1490. In my case it's 4095 so for sure the chip is broken?

author: iMo
author_rank: Super Contributor
timestamp: March 14, 2020, 03:25:12 pm
content: Show us you schematics, pcb artwork and the code. Otherwise people here cannot help you much..

author: Doctorandus_P
author_rank: Super Contributor
timestamp: March 20, 2020, 03:45:39 pm
content: Another thing to check is a fault in the hardware.ESD events and such can damage I/O pins in subtle ways such as increased leakage and other parameters worsening, and without complete failure of the pin.I've had ATmega's in which I damaged some of the ADC pins by over voltage, The ADC would not work on those pins anymore, but digital I/O still worked on those pins. So try to verify your problems with a 3nd board.

author: reyntjensm
author_rank: Regular Contributor
timestamp: October 12, 2021, 10:49:47 pm
content: It has been a while since i replied to this topic. I had no time to continue working on this project.I would like to make a V2 PCB now but i'm still not sure about the reason why my ADC values were acting strange.Due to the MCU shortage, i would like to have the option to choose between 2 different MCU types so i'm planning to include 2 different MCU footprints in my PCB design. So one of the reasons why my first version didn't worked was: i had made a connection between the analog input and a MCU pin with no analog input functions... yes this is a stupid mistake. But still with some bodge wires etc. i couldn't get it to work. So i'm wondering if there could be another error in my PCB design. What about GND and AGND? I've been reading a lot of articles talking about a specific ground for the analog part of the circuit, i've also found an equal amount of articles saying it doesn't really matter. I only need 1 analoge reading and it's from a LDR, so it's not a very critical analog signal. Attached you can find some of my PCB design files. I only have 1 ground plane for the whole PCB. The analog signal is taken from a voltage divider made from a LDR and a resistor.I would like to start very soon with the V2 PCB but it would be very sad if i encountered the same issue....

author: Kleinstein
author_rank: Super Contributor
timestamp: October 13, 2021, 08:12:30 am
content: GND and AGND should be connected at 1 point. In an advanced design there may be something like a ferrite beed in between, but in most cases this would be a direct connection relatively close to the µC. With the ground one can make a few mistakes and get a little extra noise, or a slight offset of a few mV if high. So this should not explain a large error unless there is something really wrong, like no link at all.The plan shows relatively few decoupling capacitors. Usually every pair of VCC/GND pins should get at least 1 cap.I don't see the usual SWD debug interface, only SWDIO so far.Unless short on pins I would not share the SWD pins with any other functions.The ADC in many of the STM32 µCs wants to first get a calibration call. This adjusts the offset.Some µC types have a problem when the ADC was not used for a longer time. The first reading after long waiting may be off.If the signal source is relatively high resistance like the LDR one should consider a capacitor (e.g. 10 nF range) in parallel to the signal source. Also the sampling time may have to be set longer.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 13, 2021, 09:58:43 am
content: The input pin shouldn't affect if configured as input, but in any case, you can set any pin as analog (Not Analog for the adc, the other analog that means everything on that pin is turned off) and it'll remain in in high impedance and lowest power state.You should show some of your code and your ADC connections, these schematics are pretty useless, showing nothing...Probably you're skipping ADC calibration, or the sampling time is too high for the input impedance.I've seen the ADC do very weird things when not respecting that.