Page 1 of 1

Problem with multiple ADCs

Posted: Mon Jun 16, 2014 10:12 am
by randomhuman
I'm having some problems with reading from multiple ADC pins. I have two potentiometers hooked up to X7 and X8, but one of the pots controls both readings, while the other one has no effect. Here's the code that I'm using for testing:

Code: Select all

from pyb import ADC
import pyb

if __name__ == "__main__":
    switch = pyb.Switch()
    pyb.delay(5000)
    if switch():
        pot_1 = ADC(pyb.Pin.board.X7)
        pot_2 = ADC(pyb.Pin.board.X8)

        while True:
            print("pot_1:", pot_1.read(), "pot_2", pot_2.read())
            pyb.delay(1000)

The values printed are not exactly the same (but close) and increase and decrease when the pot on X8 is turned.

If the order of the declarations is changed then it is the pot on X7 that has the effect (i.e. the last ADC pin configured is effective). I tried it on X11 and X12 as well, with the same result.

Could this be a bug in the ADC class or am I not using it correctly?

Re: Problem with multiple ADCs

Posted: Mon Jun 16, 2014 1:30 pm
by randomhuman
The ADCAll class seems to work as expected:

Code: Select all

from pyb import ADCAll
import pyb

if __name__ == "__main__":
    switch = pyb.Switch()
    pyb.delay(5000)
    if switch():
        adc = ADCAll(12)
        pot_1 = 6 # pyb.Pin.board.X7
        pot_2 = 7 # pyb.Pin.board.X8
        while True:
            print("pot_1:", adc.read_channel(pot_1), "pot_2", adc.read_channel(pot_2))
            pyb.delay(1000)


Re: Problem with multiple ADCs

Posted: Mon Jun 16, 2014 4:45 pm
by dhylands
I found the problem, and created a pull request to fix the problem:
https://github.com/micropython/micropython/pull/698

Re: Problem with multiple ADCs

Posted: Mon Jun 16, 2014 5:20 pm
by randomhuman
Cool, thanks. That would have been a bit beyond me.