trying to read an ADC GPIO

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

trying to read an ADC GPIO

Postby mzimmers » Thu Jun 21, 2018 7:56 pm

...the docs make it seem fairly straightforward, so I must be doing it wrong. BATTERY_EN_NBR refers to an output pin that tells the batter controller to output its voltage level (to pin GPIO35). I assert the enable pin, do the ADC stuff, and clear the enable pin (to prevent further battery drawdown).

Code: Select all

#define BATTERY_EN_NBR (GPIO_NUM_32)
int voltage;
...
ESP_ERROR_CHECK(gpio_set_level(BATTERY_EN_NBR, 1));
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_10));
ESP_ERROR_CHECK(adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11));
voltage = adc1_get_raw(ADC1_CHANNEL_7);
ESP_LOGI(TAG, "battery voltage is %x.", voltage);
ESP_ERROR_CHECK(gpio_set_level(BATTERY_EN_NBR, 0));
I run this in a loop, and get pure 0s in my voltage variable. Where might I be botching this?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: trying to read an ADC GPIO

Postby kolban » Thu Jun 21, 2018 10:52 pm

I'd suggest wiring GPIO 35 directly to 3.3V and see if the output is other than 0. That will eliminate half the logic as a problem.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: trying to read an ADC GPIO

Postby WiFive » Thu Jun 21, 2018 11:15 pm

Did you configure gpio32 as a gpio (normally a 32k crystal pin) and enable its output driver?

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: trying to read an ADC GPIO

Postby mzimmers » Thu Jun 21, 2018 11:37 pm

No, I didn't...oops. So, disable pullup/pushdown/interrupts, set as output and call gpio_config()?

I'm not sure what it means to enable the output driver.

Thanks...

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: trying to read an ADC GPIO

Postby WiFive » Fri Jun 22, 2018 3:09 am

Configuring it as an output is the same

mikemoy
Posts: 626
Joined: Fri Jan 12, 2018 9:10 pm

Re: trying to read an ADC GPIO

Postby mikemoy » Fri Jun 22, 2018 4:19 am

There is a post here by rudi ;-) that talks about it:
viewtopic.php?f=12&t=1408&p=6568&hilit=gpio32#p6568

Specifically here in that post:
you need first to clear the base bits if you want to use the GPIO32 and GPIO33 as the general GPIO

Code: Select all

REG_CLR_BIT(RTC_IO_XTAL_32K_PAD_REG, RTC_IO_X32P_MUX_SEL); /* gpio32 route to digital io_mux */
REG_CLR_BIT(RTC_IO_XTAL_32K_PAD_REG, RTC_IO_X32N_MUX_SEL); /* gpio33 route to digital io_mux */

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: trying to read an ADC GPIO

Postby mzimmers » Fri Jun 22, 2018 3:20 pm

Thanks for the help...the suggestions seem to have gotten it working.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: trying to read an ADC GPIO

Postby mzimmers » Fri Jun 22, 2018 8:31 pm

Except...

...it turns out that I need to keep GPIO 32 as is...changing its configuration causes problem with the clock.

So, before I go picking another GPIO at random, is there a master list somewhere that explains which GPIOs are pre-allocated, and which are free? I couldn't find this information in the page on GPIOs.

Thanks...

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: trying to read an ADC GPIO

Postby kolban » Fri Jun 22, 2018 11:32 pm

This is the page I normally go to:

http://esp-idf.readthedocs.io/en/latest ... /gpio.html

If it needs updating with new findings, post about it.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: trying to read an ADC GPIO

Postby mzimmers » Sun Jun 24, 2018 3:48 pm

Hi, Neil - I don't think that page necessarily needs updating. I was just hoping that there would be a location where all the pertinent information for GPIOs (including any use by the ESP32 module itself, for example GPIO32 which evidently is the input to the RTC). We chose GPIO32 for our application use because our HW guy didn't know it was pre-allocated. So, the question is, where do we go to see if another pin we choose is free or allocated?

Who is online

Users browsing this forum: Google [Bot], username and 97 guests