unstable values when I use analog inputs

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
momidomy
Posts: 4
Joined: Fri Sep 26, 2014 5:12 pm
Location: Italy (Ba)

unstable values when I use analog inputs

Post by momidomy » Sun Nov 09, 2014 11:51 am

Hi,
I use an analog inputs, for example a potentiometer, with ADC() class, but every time I get unstable values.

How can I stabilise these values?

Thanks a lot

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: unstable values when I use analog inputs

Post by dhylands » Mon Nov 10, 2014 3:38 pm

By unstable, how much variation are you seeing?

What value of potentiometer are you using?

And how is it wired up? The ADC inputs are 3.3v.

I moved this to the General topic, since that's where it belongs.

momidomy
Posts: 4
Joined: Fri Sep 26, 2014 5:12 pm
Location: Italy (Ba)

Re: unstable values when I use analog inputs

Post by momidomy » Tue Nov 11, 2014 6:28 pm

Hi,
thanks to your answer,

By unstable, how much variation are you seeing?
For example, at the min value I read 4,0,4,0,23,1,5... and so on, and at the max value: 4081,4093,4075,4095...

What value of potentiometer are you using?
I tried with 10K, 50K, 100K linear potentiometer.

And how is it wired up? The ADC inputs are 3.3v.
I connected the two ends to 3.3v and GND on the pyboard, central to the ADC pin, for example (X1).

I hope which I can solve this issue
Thanks in advance

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: unstable values when I use analog inputs

Post by dhylands » Wed Nov 12, 2014 4:00 pm

And finally, can you post your code which demonstrates the problem (just to eliminate any issues on that front).

It would also be good to be explicit about which pins you're connecting your inputs up to as well.
Are you using the 3.3v from the pyboard? Or from an external source? Or the Analog 3.3v?
Are you using the ground from the pyboard? Or the Analog Ground?

The A3V3 signal corresponds to X23
The AGND signal corresponds to X24

See: http://docs.micropython.org/en/latest/quickref.html

momidomy
Posts: 4
Joined: Fri Sep 26, 2014 5:12 pm
Location: Italy (Ba)

Re: unstable values when I use analog inputs

Post by momidomy » Wed Nov 19, 2014 3:24 pm

Thanks for your patience.

I tried with pin X1, and I used 3.3v from the pyboard and I used GND from the pyboard.

With this simple code (into the REPL prompt):

Code: Select all

pot = pyb.ADC('X1')
while True:
    pot.read()
    pyb.delay(1000)
And then I tried with all possibilities, I wired the pot with A3V3, AGND and X1 (on the centre of the pot) but the unstable values persists.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: unstable values when I use analog inputs

Post by dhylands » Wed Nov 19, 2014 11:32 pm

I tried this program:

Code: Select all

import pyb

pot = pyb.ADC('X1')
while True:
    x = pot.read()
    print(x)
    pyb.delay(1000)
and I see something like this:

Code: Select all

837
834
836
833
836
836
837
837
838
836
837
836
837
837
836
837
Those fluctuations are probably just due to noise on the 3.3v line.

User avatar
bmarkus
Posts: 111
Joined: Tue Oct 21, 2014 5:58 am

Re: unstable values when I use analog inputs

Post by bmarkus » Thu Nov 20, 2014 5:52 am

It sounds a faulty pot.
Tiny Core Linux (piCore) developer
HAM radio call: HA5DI (Béla)

momidomy
Posts: 4
Joined: Fri Sep 26, 2014 5:12 pm
Location: Italy (Ba)

Re: unstable values when I use analog inputs

Post by momidomy » Thu Nov 20, 2014 11:37 am

Hi all,

I don't understand, but this is a common behaviour? because I saw that you have the same issue with the ADC().
Those fluctuations are probably just due to noise on the 3.3v line.
And my final question is: how would you solve this problem?

P.S.: I tried with many potentiometers but I get the same behaviour

Thanks a lot
Domenico

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: unstable values when I use analog inputs

Post by blmorris » Thu Nov 20, 2014 2:35 pm

The range of values shown by @dhylands (~4 out of 4096 possible values) corresponds to a variation of ~0.1%, which really isn't bad for a board that wasn't specifically designed for precision analog data acquisition. That isn't intended as a criticism against the design of the pyboard; the pyboard was designed to pack a lot of general-purpose functionality into a compact and inexpensive package, and in this it succeeded quite well. However, compromises need to be made in any design, and high-precision analog performance really couldn't be within the scope of the pyboard without adding both size and cost to the board.

Your data does show greater variation than Dave's, so it should be possible to improve your precision a bit.
  • Connecting the pot across A3V3 and AGND is a good start; remember to keep your leads short! If they can't be kept short, then twisting the wires together can help to reduce EMF pickup.
  • You could also put an additional capacitor on the pot terminals which are connected to the analog supply; ~10uF should suffice.
  • A small capacitor (~10nF, depends on your pot resistance and desired sampling rate) between the ADC pin and AGND will serve as a low-pass filter, reducing high-frequency noise.
  • Oversampling: take several readings and average them together. If I am doing this at a low level (say on an 8-bit PIC) then I would take 2^N readings, add them together, and bit shift the answer N-bits to the right; although in micropython such an optimization would be kind of pointless.
There are many factors that could be contributing noise to the system. USB power is notoriously noisy (this can vary depending on the computer) and some of this noise can bleed through the LDO voltage regulator to the 3.3V supply. Beyond that, the pyboard doesn't provide a lot of separation between the digital and analog domains (couldn't do this and remain as small and inexpensive as it is.)
Proper shielding and separation in this case could mean a four-layer board, split ground and supply planes with carefully planned connections between digital and analog domains, separate voltage regulation for the digital and analog domains, shield traces on the signal layers, more and larger bypass capacitors, etc. I could go on; I once made a fairly high-precision USB-powered data acquisition instrument, but as I have implied, it didn't look much like a pyboard and would have been a bit more expensive to manufacture.
-Bryan

Post Reply