ESP32 - ADC - Invalid Pin

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
fractal-synapse
Posts: 3
Joined: Thu Sep 07, 2017 9:11 pm

ESP32 - ADC - Invalid Pin

Post by fractal-synapse » Fri Sep 08, 2017 7:00 pm

Hi Guys this might be an easy one....

I am trying to test an analog read input and failing hard on something so easy.

>>> p = Pin(0)
>>> adc = ADC(p)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid Pin for ADC

What is the proper way to get this? Any call to the ADC(var) in other attempts like ADC(0) states it expects a Pin object.

There also don't seem to be and well named examples for the analog pins in the repo - something I could contribute once I solve this.

Let me know !

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

Re: ESP32 - ADC - Invalid Pin

Post by dhylands » Fri Sep 08, 2017 11:55 pm

Here's an example:

Code: Select all

import pyb

pot = pyb.ADC('X1')
while True:
    x = pot.read()
    print(x)
    pyb.delay(1000)
On the pyboard, pin X1 maps to CPU pin A0 which maps to adc channel 0 (ADC123_IN0 in the datasheet). So all of the following should be equivalent:

Code: Select all

pot = ADC(0)
pot = ADC('X1')
pot = ADC('A0')
pot = ADC(pyb.Pin('A0'))
pot = ADC(pyb.Pin('X1'))
pot = ADC(pyb.Pin.cpu.A0)
pot = ADC(pyb.Pin.board.X1)

fractal-synapse
Posts: 3
Joined: Thu Sep 07, 2017 9:11 pm

Re: ESP32 - ADC - Invalid Pin

Post by fractal-synapse » Sat Sep 09, 2017 12:58 am

Thx Dave!

I am however on the Adafruit ESP32 Huzzah. I managed to figure it out after I posted lol. Seems there is a delay in post authorization. I do that a fair bit (answering my own questions hehe)

import machine, neopixel
from time import sleep

np = neopixel.NeoPixel(machine.Pin(4), 120,bpp=3,timing=1)
adc = machine.ADC(machine.Pin(36))


The Above worked for me ... I was trying to control some neopixels with a potentiometer which is now working.

I figured it out by looking at the github port...

https://github.com/micropython/micropyt ... hine_adc.c

STATIC const madc_obj_t madc_obj[] = {
{{&machine_adc_type}, GPIO_NUM_36, ADC1_CHANNEL_0},
{{&machine_adc_type}, GPIO_NUM_37, ADC1_CHANNEL_1},
{{&machine_adc_type}, GPIO_NUM_38, ADC1_CHANNEL_2},
{{&machine_adc_type}, GPIO_NUM_39, ADC1_CHANNEL_3},
{{&machine_adc_type}, GPIO_NUM_32, ADC1_CHANNEL_4},
{{&machine_adc_type}, GPIO_NUM_33, ADC1_CHANNEL_5},
{{&machine_adc_type}, GPIO_NUM_34, ADC1_CHANNEL_6},
{{&machine_adc_type}, GPIO_NUM_35, ADC1_CHANNEL_7},
};

I restricted to one of those Pins Channel 0 / 36 and all cruised through.

Thanks for the assistance!

Hailing from Edmonton, AB!

JDRBoston
Posts: 21
Joined: Mon Feb 19, 2018 9:43 pm

Re: ESP32 - ADC - Invalid Pin

Post by JDRBoston » Tue Apr 30, 2019 10:44 pm

Is there a way to create A0 = GPIO26 (ADC2_CHO) as an analog pin for the ESP32 using the machine module? I am trying to use this pin for analog input on the ESP32 Huzzah because the physical location matches the ADC pin of the ESP8266 Huzzah and I’ve already wired up a LDR on a featherwing proto board that uses that pin. In inspecting machine_adc.c, I see that only the ADC1 channels 0 – 7 (GPIO pins 32 – 39) are defined.

Thank you for your advice!

JumpZero
Posts: 54
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

Re: ESP32 - ADC - Invalid Pin

Post by JumpZero » Wed May 01, 2019 7:39 am

Hi!
MicroPython for ESP32 only allows ADC1 as mentioned here: On the ESP32 ADC functionality is available on Pins 32-39
Adafruit ESP32 Huzzah exposes ADC2 but also pins 32 & 33 for ADC1https://learn.adafruit.com/adafruit-huz ... er/pinouts It should be possible to use one of these.

JDRBoston
Posts: 21
Joined: Mon Feb 19, 2018 9:43 pm

Re: ESP32 - ADC - Invalid Pin

Post by JDRBoston » Wed May 01, 2019 4:20 pm

Thank you, JumpZero. I was hoping for a software solution so that I could swap the LDR-soldered feather proto board between the Huzzah ESP8266 and ESP32. Rather than rewiring the proto board so that the ESP32 can access the LDR voltages via ADC1, I might make one that has a VEML7700 lux sensor. That way, I can then use i2c pins that are compatible with the ESP8266 and ESP32 to get the light levels with either ESP. I see that paulouf34 has very kindly recently shared a library for that light sensor (link to description below).

viewtopic.php?t=5777

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: ESP32 - ADC - Invalid Pin

Post by OutoftheBOTS_ » Wed May 01, 2019 9:17 pm

Lobo port also has access to the ADC2 see https://github.com/loboris/MicroPython_ ... o/wiki/adc

You will need to read the docs to see the restriction on ADC2

Post Reply