[SOLVED] SAMD21 ADC 100ms delay after wakeup or you get messy readings - Page 1
Forum section: electronics / general

author: nemail2
author_rank: Regular Contributor
timestamp: June 25, 2022, 01:47:06 pm
content: Hi,I'm building a device with a SAMD21 which is battery powered and sleeps obviously most of the time. It periodically wakes up, makes an ADC measurement with the internal ADC and goes back to sleep again.Now the issue is, that it seems to take 100ms for the ADC to wake up properly, to get reliable measurements. I know that in the datasheet it says that the ADC needs SOME delay, but I wasn't able to find exact figures.I have taken into account the source resistance for the sampling cap to charge up properly and there is even a formula for that in the datasheet.T(sample) >= (R(source) + R(sample)) * C(sample) * (n + 1) * ln(2) where n is the ADC resoultion in bits.R(sample) = 3.5kOhmsC(sample) = 3.5pFADC Clock Frequency = 48MHz / 512 = 93750Hz (512 is the prescaler value)Therefore the ADC clock period is 10.66us (1 / 93750)The default ADC sample time is half the ADC clock period, so normally the sample time is:Default ADC sample time = 10.66us / 2 = 5.33usSo for 1 Meg source resistance I would need to increase sample time by 6 and that would end up in this formula for 8 bit resolution:(6 * 5.33us) / (3.5pF * (8 + 1) * ln(2)) - 3.5kOhms = 1460785 Ohms source resistance which should be plenty.Still, if I do not put in a delay of around 100ms before measuring, the readings are garbage (e.g. measuring 4V instead of 3V).I also tried to increase the number of samples and accumulate and average them within the ADC and again, only if I take that many samples that a time of around 100ms passes, the measurements get useful.So, is the delay the ADC needs really 100ms or am I doing something wrong?Thanks!

author: nemail2
author_rank: Regular Contributor
timestamp: June 25, 2022, 02:38:23 pm
content: Quote from: evb149 on June 25, 2022, 01:58:21 pmindeed.Now, I tried to investigate your suggestions and if I didn't go into depth with some of the topic that's not because of ignorance but of the lack of knowledge, so I apologize upfront.Quote from: evb149 on June 25, 2022, 01:58:21 pmCurrently I'm powering from my lab power supply which is on all the time, only the microcontroller goes to sleep.Quote from: evb149 on June 25, 2022, 01:58:21 pmAlso very good point! I had the ADC tied to the main oscillator which indeed goes to sleep. Instead of trying to find out the values on that, I simply tied the ADC to the internal ultra low power 32kHz crystal which is powered on all the time, even during the deepest of sleep modes. Unfortunately, that didn't resolve the issue. I was really hoping for that.Quote from: evb149 on June 25, 2022, 01:58:21 pmThe ADC is connected to the internal 1V bandgap reference, I wasn't able to find any figures on how long it takes to stabilize after powerup or wakeup...Quote from: evb149 on June 25, 2022, 01:58:21 pmI think I've ruled out any clock issue now with the always-on OSCULP32K and I even discard the first measurement anyway, which is usually enough, if not going to sleep. For wakeup delay after sleep, I wasn't able to find any numbers in the datasheet.Quote from: evb149 on June 25, 2022, 01:58:21 pmThe only thing controlling the speed of the ADC is AFAIK 1) the clock and 2) the sample time, which I have adjusted to be long enough for the 1M source resistance. There is a continuous mode and a single shot mode too, but I'm using single-shot, obviously.I've tried to do mutliple readings and oversampling as well as doing only a single reading but the result remained the same. No matter what I do, if 100ms pass (by sampling or waiting doesn't matter) the readings get useful.

author: nemail2
author_rank: Regular Contributor
timestamp: June 25, 2022, 03:31:27 pm
content: OK, turns out, I am a stoopid idiot.I had set all pins which are inputs to input pullups during sleep, so they don't float and draw unnecessary current.Obviously with the analog pin being high at 3V and having a 100n capacitor as a filter on that pin, it takes some time for that cap to discharge.YIKES.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: June 25, 2022, 05:05:49 pm
content: Long time ago I read a story for a low power application that also had some LED's.When the leds were off, the pins were switched to input to save some uA from the battery.And then the circuits started failing by premature battery drain...It tuns out that some LED's work as photocells and can generate a voltage when enough light shines on them, and this was enough to get the input stage of the uC into it's linear region where it shunts current from the power input to GND...

author: nemail2
author_rank: Regular Contributor
timestamp: June 25, 2022, 05:09:13 pm
content: Quote from: Doctorandus_P on June 25, 2022, 05:05:49 pmNICE! Fortunately I have a buffer between the IO pins and the LEDs and also I'm taking measurements regularly to make sure there is no unexpected power drain.

author: SiliconWizard
author_rank: Super Contributor
timestamp: June 25, 2022, 05:50:40 pm
content: Quote from: Doctorandus_P on June 25, 2022, 05:05:49 pmCan be something to really consider indeed if you drive LEDs from GPIOs directly.All LEDs do this actually. But depending on their spectrum and whether their shell is fully transparent or colored, you'll get various results.