[Solved] Multiple potentiometers problem (ESP32)

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
macrimarco001
Posts: 7
Joined: Thu Mar 18, 2021 10:22 pm

[Solved] Multiple potentiometers problem (ESP32)

Post by macrimarco001 » Fri Mar 19, 2021 10:52 pm

Hello Guys, this is the second topic i post... i have to say that i think this is a very helpful and productive community since i found someone so kind who helped me unstuck from another problem.
Unfortunately for me i need once again your help.

I tried connecting two potentiometers to the ESP32 board ad wrote a simple code to try to read them and print the values.
The problem is that the output displayed is not what i expect... in fact one of the values is always stuck on the highest value.
I tried swapping the potentiometers and nothing new happens. I also tried to reverse the order of the readings and it works but just for the first reading i do, the other one is stuck as well.

What should i do to solve my problem?
Thanks for your kind attention and sorry for my low english knowledge.

ps. i will attach the code down here.

Code: Select all

from machine import Pin
from machine import ADC



pot1 = ADC(Pin(35))
pot1.atten(ADC.ATTN_11DB)
print(pot1.read())


pot2 = ADC(Pin(32))
pot2.atten(ADC.ATTN_11DB)
print(pot2.read())


"""
OUTPUT:
371 #ok
4095 #not ok

"""

Last edited by macrimarco001 on Sun Mar 21, 2021 8:27 pm, edited 1 time in total.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Multiple potentiometers problem (ESP32)

Post by davef » Fri Mar 19, 2021 11:04 pm

Pin
ADC1_CH6 34 IP, no pull-up
ADC1_CH7 35 IP, no pull-up
ADC1_CH4 32
ADC1_CH5 33

Check to see if there is a channel parameter that you need to call. Looks like pin 32 and 35 are both on ADC1

macrimarco001
Posts: 7
Joined: Thu Mar 18, 2021 10:22 pm

Re: Multiple potentiometers problem (ESP32)

Post by macrimarco001 » Fri Mar 19, 2021 11:17 pm

davef wrote:
Fri Mar 19, 2021 11:04 pm
Pin
ADC1_CH6 34 IP, no pull-up
ADC1_CH7 35 IP, no pull-up
ADC1_CH4 32
ADC1_CH5 33

Check to see if there is a channel parameter that you need to call. Looks like pin 32 and 35 are both on ADC1
I do not understand... what should I check? They are separate pins and channels. Could you explain a little bit better please?

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Multiple potentiometers problem (ESP32)

Post by davef » Sat Mar 20, 2021 2:15 am

Looks like you have followed the example in the docs correctly.
class machine.ADC(id)
Access the ADC associated with a source identified by id. This id may be an integer (usually specifying a channel number), a Pin object, or other value supported by the underlying machine.
I thought you would need to specify the channel, but I now understand that you can call the specific channel using "a Pin object".

pin32 works good for me, I only need one ADC input so can't help further.

Good luck

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Multiple potentiometers problem (ESP32)

Post by davef » Sat Mar 20, 2021 6:16 am

It might help to tell us what hardware you are using. Maybe, pin32 has an external pull-up resistor.

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Multiple potentiometers problem (ESP32)

Post by OlivierLenoir » Sat Mar 20, 2021 7:30 am

If you refer to this document, you can improve reading by adding 0.1µF capacitor and / or sampling the ADC reading.
A code to sample the ADC reading.

Code: Select all

from machine import Pin
from machine import ADC


def read_adc(adc_pin, sample=24):
    cumul = 0
    for _ in range(sample):
        cumul += acd_pin.read()
    return cumul // sample


# default 24 sample
pot1 = ADC(Pin(35))
pot1.atten(ADC.ATTN_11DB)
print(read_adc(pot1))


# 64 sample
pot2 = ADC(Pin(32))
pot2.atten(ADC.ATTN_11DB)
print(read_adc(pot2, 64))

macrimarco001
Posts: 7
Joined: Thu Mar 18, 2021 10:22 pm

Re: Multiple potentiometers problem (ESP32)

Post by macrimarco001 » Sat Mar 20, 2021 11:31 am

OlivierLenoir wrote:
Sat Mar 20, 2021 7:30 am
If you refer to this document, you can improve reading by adding 0.1µF capacitor and / or sampling the ADC reading.
A code to sample the ADC reading.

Code: Select all

from machine import Pin
from machine import ADC


def read_adc(adc_pin, sample=24):
    cumul = 0
    for _ in range(sample):
        cumul += acd_pin.read()
    return cumul // sample


# default 24 sample
pot1 = ADC(Pin(35))
pot1.atten(ADC.ATTN_11DB)
print(read_adc(pot1))


# 64 sample
pot2 = ADC(Pin(32))
pot2.atten(ADC.ATTN_11DB)
print(read_adc(pot2, 64))


thank you all for the replies guys...
my hardware is:

https://www.amazon.it/Hseamall-Potenzio ... ics&sr=1-5

https://www.amazon.it/gp/product/B071P9 ... UTF8&psc=1

I tried Mr Oliver's code and found something more strange:
if I put any code (even mine) in a while cycle, even for just one potentiometer, the output has the first reading correct, but the other ones jump at maximum.
i'll put a screenshot and a photo of my board and connectoins.
Maybe by solving this deep problem we'll be able to solve the other via domino effect, at least i hope so.

ImageImage

Code: Select all

from machine import Pin
from machine import ADC
from time import sleep


def read_adc(pin, sample=24):
    cumul = 0
    for _ in range(sample):
        cumul += ADC(Pin(pin)).read()
    return cumul // sample


while 1:
    print(read_adc(35))
    sleep(1)
    


"""
OUTPUT:
3999 OK
4095 NOT OK
4095 ...
4095 ...
4095 ...
4095 ...
4095 ...
""" 

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Multiple potentiometers problem (ESP32)

Post by OlivierLenoir » Sat Mar 20, 2021 12:34 pm

There is a small mistake on you code. You forget to declare the ADC pin. You should do this one.

Code: Select all

from machine import Pin
from machine import ADC
from time import sleep


def read_adc(adc_pin, sample=24):
    cumul = 0
    for _ in range(sample):
        cumul += acd_pin.read()
    return cumul // sample


# default 24 sample
pot1 = ADC(Pin(35))
pot1.atten(ADC.ATTN_11DB)

while 1:
    print(read_adc(pot1))
    sleep(1)

macrimarco001
Posts: 7
Joined: Thu Mar 18, 2021 10:22 pm

Re: Multiple potentiometers problem (ESP32)

Post by macrimarco001 » Sun Mar 21, 2021 8:26 pm

Hello guys, my father found the solution to the problem... and to tell dthe truth i am a bit embarassed by the mistake i made. it turned out that, since there are two ground pins on the board, i connected my ground to the wrong pin... now both potentiometers work well.

Thank you guys anyway for the help and support :)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: [Solved] Multiple potentiometers problem (ESP32)

Post by pythoncoder » Mon Mar 22, 2021 9:45 am

Ah, I should have spotted that in your photo. That pin has caught me out too. The silkscreen label is actually cmd but it's very easy to misread it as gnd.

See pinout diagram.
Peter Hinch
Index to my micropython libraries.

Post Reply