Page 1 of 2

ADC Conversion

Posted: Sun Nov 15, 2015 2:04 am
by Oli_
Hi,

While playing with ADC to read the Lipo voltage from my expansion board, I experience strange behavior with the modulus operator on ADC readout.

Try the following :
>>> from machine import ADC
>>> adc = ADC()
>>> lipo = adc.channel(pin='GP3')
>>> lipo()
3594
>>> lipo()
3603
>>> lipo()
3601
>>> lipo(),lipo()%4095
(3600, 3603)
>>> lipo(),lipo()%4095
(3601, 3603) <----
>>> lipo(),lipo()%4095
(3601, 3607) <----
>>> lipo(),lipo()%4095
(3602, 3602)
>>> lipo(),lipo()%4095
(3601, 3601) <----
>>> lipo(),lipo()%4095
(3602, 3602)
>>> lipo(),lipo()%4095
(3601, 3602) <----
>>> lipo(),lipo()%4095
(3602, 3600)
>>>
If in the REPL I type 3601%4095 several times, no surprise I always get 3601.
If I readout ADC value, the modulus to 4095 of same readout does not return the same value. See example here above.

Any thought ?

Olivier

Re: ADC Conversion

Posted: Sun Nov 15, 2015 4:26 pm
by dhylands
The ADC line will have fluctuations as the MCU consumes varying amounts of current as it runs. This is normal.

Re: ADC Conversion

Posted: Sun Nov 15, 2015 5:19 pm
by scudderfish
As Dave said (but in other words) uour two calls to lipo() will be returning different values. Call it once and assign it to a variable.
a=lipo()
a,a%4095

Regards,
David

Re: ADC Conversion

Posted: Sun Nov 15, 2015 7:05 pm
by dhylands
And also, Not quite sure why you're using modulo 4095, which will give values between 0 and 4094. I would have expected you to use modulo 4096 which will give values between 0 and 4095 (unrelated to the issue you posted about, just an observation).

Re: ADC Conversion

Posted: Mon Nov 16, 2015 10:46 am
by pythoncoder
Well, indeed. But given the essentially similar results without the modulo operator I can't see it serves any purpose here.

Re: ADC Conversion

Posted: Tue Nov 17, 2015 12:14 am
by Oli_
scudderfish wrote:As Dave said (but in other words) uour two calls to lipo() will be returning different values. Call it once and assign it to a variable.
a=lipo()
a,a%4095

Regards,
David
Yes ! Of course ... this was a dumb question ... I forgot that I made two calls ...

Thanks David and Dave

Re: ADC Conversion

Posted: Tue Nov 17, 2015 12:17 am
by Oli_
dhylands wrote:And also, Not quite sure why you're using modulo 4095, which will give values between 0 and 4094. I would have expected you to use modulo 4096 which will give values between 0 and 4095 (unrelated to the issue you posted about, just an observation).
Yes, its a typo ;)

Re: ADC Conversion

Posted: Tue Nov 17, 2015 12:26 am
by Oli_
pythoncoder wrote:Well, indeed. But given the essentially similar results without the modulo operator I can't see it serves any purpose here.
Indeed, but I was just trying to understand why I got different values.
Before doing this test I was writing a method to convert the ADC to a decimal formated value and used modulus to compute it.

Re: ADC Conversion

Posted: Fri Nov 20, 2015 6:16 am
by Oli_
I continue on the same topic for an other issue.
It's written in the documentation of the Wipy that the maximum voltage at the input of the ADC is 1.4V.
This mean that I should have an ADC reading of 4095 for 1.4V, right ?
Practically, if I do a rules of 3 with my actual readings, the full scale voltage is 1.53V.
Do you experience the same thing ?

Olivier

Re: ADC Conversion

Posted: Fri Nov 27, 2015 6:17 pm
by Oli_
Hi All,

Nobody did the test ?