Problem with multiple ADCs

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
randomhuman
Posts: 19
Joined: Mon Jun 09, 2014 1:54 pm

Problem with multiple ADCs

Post by randomhuman » Mon Jun 16, 2014 10:12 am

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?

randomhuman
Posts: 19
Joined: Mon Jun 09, 2014 1:54 pm

Re: Problem with multiple ADCs

Post by randomhuman » Mon Jun 16, 2014 1:30 pm

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)


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

Re: Problem with multiple ADCs

Post by dhylands » Mon Jun 16, 2014 4:45 pm

I found the problem, and created a pull request to fix the problem:
https://github.com/micropython/micropython/pull/698

randomhuman
Posts: 19
Joined: Mon Jun 09, 2014 1:54 pm

Re: Problem with multiple ADCs

Post by randomhuman » Mon Jun 16, 2014 5:20 pm

Cool, thanks. That would have been a bit beyond me.

Post Reply