ADC1 on Adafruit Feather HUZZAH-ESP32 doesn't work with esp-idf

User avatar
MickPF
Posts: 45
Joined: Tue Apr 03, 2018 8:47 pm

ADC1 on Adafruit Feather HUZZAH-ESP32 doesn't work with esp-idf

Postby MickPF » Thu May 24, 2018 12:43 pm

Hello,

I do not know if this is the right place for my request. Maybe it would be better to place it at Adafruit?

Yesterday I started with the "ADC" part of my DIY project: I use an Adafruit Feather HUZZAH-ESP32 (ESP32-WROOM) with esp-idf and need some ADC channels. I'd prefer to use ADC1 for my goal, because ADC2 is used by WiFi also.
I wrote a simple code for testing the ADC code at all with the esp-idf (with v3.0-rc1 and with v3.0).

Code: Select all

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "driver/gpio.h"
#include "driver/i2c.h"
#include "driver/adc.h"

#include "freertos/FreeRTOS.h"

#include "esp_system.h"

typedef struct
{
    int     channel;
    char    name[8];
} adc_channel;

adc_channel adc1[] =
{
    { ADC1_CHANNEL_0, "A1_0" },     // IO36/A4
    { ADC1_CHANNEL_3, "A1_3" },     // IO39/A3
    { ADC1_CHANNEL_4, "A1_4" },     // IO32/A7
    { ADC1_CHANNEL_5, "A1_5" },     // IO33/A9
    { ADC1_CHANNEL_6, "A1_6" },     // IO34/A2
    { ADC1_CHANNEL_MAX, ""   }
};

adc_channel adc2[] =
{
    { ADC2_CHANNEL_0, "A2_0" },     // IO4/A5
    { ADC2_CHANNEL_3, "A2_3" },     // IO15/A8
    { ADC2_CHANNEL_4, "A2_4" },     // IO13/A12
    { ADC2_CHANNEL_5, "A2_5" },     // IO12/A11
    { ADC2_CHANNEL_6, "A2_6" },     // IO14/A6
    { ADC2_CHANNEL_7, "A2_7" },     // IO27/A10
    { ADC2_CHANNEL_8, "A2_8" },     // A1/DAC1
    { ADC2_CHANNEL_9, "A2_9" },     // A0/DAC2
    { ADC2_CHANNEL_MAX, ""   }
};

void app_main(void)
{
    int             adc_value, ch;
    gpio_num_t      gpio;

    adc_power_on();
    adc_set_data_inv(ADC_UNIT_1, false);
    adc_set_data_width(ADC_UNIT_1, ADC_WIDTH_BIT_12);
    for (ch = 0; adc1[ch].channel != ADC1_CHANNEL_MAX; ch++)
    {
        if (adc1_pad_get_io_num(adc1[ch].channel, &gpio) == ESP_OK)
            gpio_set_direction(gpio, GPIO_MODE_INPUT);
        adc1_config_channel_atten(adc1[ch].channel, ADC_ATTEN_DB_0);
    }
    adc_set_data_inv(ADC_UNIT_2, false);
    for (ch = 0; adc2[ch].channel != ADC2_CHANNEL_MAX; ch++)
    {
        if (adc2_pad_get_io_num(adc2[ch].channel, &gpio) == ESP_OK)
            gpio_set_direction(gpio, GPIO_MODE_INPUT);
        adc2_config_channel_atten(adc2[ch].channel, ADC_ATTEN_DB_0);
    }
    while (true) 
    {
        for (ch = 0; adc1[ch].channel != ADC1_CHANNEL_MAX; ch++)
        {
            adc_value = adc1_get_raw(adc1[ch].channel);
            printf("%s : %4d\n", adc1[ch].name, adc_value);
            vTaskDelay(100 / portTICK_PERIOD_MS);
        }
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        for (ch = 0; adc2[ch].channel != ADC2_CHANNEL_MAX; ch++)
        {
            adc2_get_raw(adc2[ch].channel, ADC_WIDTH_BIT_12, &adc_value);
            printf("%s : %4d\n", adc2[ch].name, adc_value);
            vTaskDelay(100 / portTICK_PERIOD_MS);
        }
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
Because the output displayed different values with open inputs, I connected alternately the input pins with GND.
Running this code displays that the unit ADC2 only works.
Since I have two modules of the feather, I tried the code with both modules: with the same result, the unit ADC1 doesn't work.

Is it a known issue/failure?

Thanks in advance,
Michael
Nobody is perfect!
My name is NOT Nobody...


Nobody lives forever to correct his mistakes someday in the future, if not right now!
Covid-19 is not responsible for any human failures!

User avatar
MickPF
Posts: 45
Joined: Tue Apr 03, 2018 8:47 pm

Re: ADC1 on Adafruit Feather HUZZAH-ESP32 doesn't work with esp-idf

Postby MickPF » Sat Jun 16, 2018 8:20 am

Hello,

Is it possible to get any statement about this problem?
Am I the only one with this problem?
Or is it such an embarrassing topic that nobody addresses it?
Meanwhile, ADC2 does not seem to work after updating ESP-IDF to version ESP-IDF v3.1-dev-1193-g64b56bef also!
Nobody is perfect!
My name is NOT Nobody...


Nobody lives forever to correct his mistakes someday in the future, if not right now!
Covid-19 is not responsible for any human failures!

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ADC1 on Adafruit Feather HUZZAH-ESP32 doesn't work with esp-idf

Postby ESP_Sprite » Sun Jun 17, 2018 4:20 am

Because the output displayed different values with open inputs, I connected alternately the input pins with GND.
Running this code displays that the unit ADC2 only works.
What do you mean by this? Have you tried the obvious as well, that is, feeding a known, perhaps varying voltage into the ADC pin and seeing if this does it?

User avatar
MickPF
Posts: 45
Joined: Tue Apr 03, 2018 8:47 pm

Re: ADC1 on Adafruit Feather HUZZAH-ESP32 doesn't work with esp-idf

Postby MickPF » Sun Jun 17, 2018 3:45 pm

Hello,

maybe my English isn't well, I often use "Google translate".
After trying to measure true voltage of about 1.5V (parallel to a real voltage meter) with every input pin, I connected the inputs to GND, expecting a value close to zero to be read. Instead, completely arbitrary values (even about 4095, hex 0xFFF) are output.
I did not try them all at the same time, but one after each other.
I've got two "used" Feathers and a "virgin" Feather and get same results with these three Feathers. That's why I do not believe that my hardware could be broken.

Is my code (see above) OK?

Thanks in advance,
Michael
Nobody is perfect!
My name is NOT Nobody...


Nobody lives forever to correct his mistakes someday in the future, if not right now!
Covid-19 is not responsible for any human failures!

User avatar
MickPF
Posts: 45
Joined: Tue Apr 03, 2018 8:47 pm

Re: ADC1 on Adafruit Feather HUZZAH-ESP32 doesn't work with esp-idf

Postby MickPF » Mon Jun 18, 2018 2:07 pm

I've modified my test code:

Code: Select all

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "driver/gpio.h"
#include "driver/i2c.h"
#include "driver/adc.h"

#include "freertos/FreeRTOS.h"

#include "esp_system.h"

#define ADC_VALUE_MAX                       4095
#define VOLTAGE_MAX                         3900

typedef struct
{
    gpio_num_t  gpio_pin;
    int         channel;
    char        name[8];
} adc_channel;

adc_channel adc1[] =
{
    { GPIO_NUM_36,  ADC1_CHANNEL_0, "A1_0" },     // IO36/A4
    { GPIO_NUM_39,  ADC1_CHANNEL_3, "A1_3" },     // IO39/A3
    { GPIO_NUM_32,  ADC1_CHANNEL_4, "A1_4" },     // IO32/A7
    { GPIO_NUM_33,  ADC1_CHANNEL_5, "A1_5" },     // IO33/A9
    { GPIO_NUM_34,  ADC1_CHANNEL_6, "A1_6" },     // IO34/A2
    { GPIO_NUM_35,  ADC1_CHANNEL_7, "A1_7" },     // IO35/A13
    { GPIO_NUM_MAX, ADC1_CHANNEL_MAX, ""   }
};

adc_channel adc2[] =
{
    { GPIO_NUM_4,   ADC2_CHANNEL_0, "A2_0" },     // IO4/A5
    { GPIO_NUM_15,  ADC2_CHANNEL_3, "A2_3" },     // IO15/A8
    { GPIO_NUM_13,  ADC2_CHANNEL_4, "A2_4" },     // IO13/A12
    { GPIO_NUM_12,  ADC2_CHANNEL_5, "A2_5" },     // IO12/A11
    { GPIO_NUM_14,  ADC2_CHANNEL_6, "A2_6" },     // IO14/A6
    { GPIO_NUM_27,  ADC2_CHANNEL_7, "A2_7" },     // IO27/A10
    { GPIO_NUM_25,  ADC2_CHANNEL_8, "A2_8" },     // A1/DAC1
    { GPIO_NUM_26,  ADC2_CHANNEL_9, "A2_9" },     // A0/DAC2
    { GPIO_NUM_MAX, ADC2_CHANNEL_MAX, ""   }
};

uint16_t    adc1_value[7], adc2_value[9];

void setup_gpio()
{
    int     ch;
    
    for (ch = 0; adc1[ch].gpio_pin != GPIO_NUM_MAX; ch++)
    {
        gpio_set_direction(adc1[ch].gpio_pin, GPIO_MODE_INPUT);
        gpio_set_pull_mode(adc1[ch].gpio_pin, GPIO_FLOATING);
    }
    for (ch = 0; adc2[ch].gpio_pin != GPIO_NUM_MAX; ch++)
    {
        gpio_set_direction(adc2[ch].gpio_pin, GPIO_MODE_INPUT);
        gpio_set_pull_mode(adc2[ch].gpio_pin, GPIO_FLOATING);
    }
}

void setup_adc()
{
    int         ch;
    gpio_num_t  gpio_pin;
    
    adc_power_on();
    adc_set_data_inv(ADC_UNIT_1, false);
    adc_set_data_width(ADC_UNIT_1, ADC_WIDTH_BIT_12);
    for (ch = 0; adc1[ch].gpio_pin != GPIO_NUM_MAX; ch++)
    {
        if (adc1_pad_get_io_num(adc1[ch].channel, &gpio_pin) == ESP_OK)
            gpio_set_direction(gpio_pin, GPIO_MODE_INPUT);
        if (gpio_pin != adc1[ch].gpio_pin)
            printf("ERROR: Wrong pin assignment for %s\n", adc1[ch].name);
        adc1_config_channel_atten(adc1[ch].channel, ADC_ATTEN_DB_11);
    }
    adc_set_data_inv(ADC_UNIT_2, false);
    for (ch = 0; adc2[ch].gpio_pin != GPIO_NUM_MAX; ch++)
    {
        if (adc2_pad_get_io_num(adc2[ch].channel, &gpio_pin) == ESP_OK)
            gpio_set_direction(gpio_pin, GPIO_MODE_INPUT);
        if (gpio_pin != adc2[ch].gpio_pin)
            printf("ERROR: Wrong pin assignment for %s\n", adc2[ch].name);
        adc2_config_channel_atten(adc2[ch].channel, ADC_ATTEN_DB_11);
    }
}

void read_analog_inputs()
{
    int         ch, val;
    uint32_t    voltage;

    for (ch = 0; adc1[ch].gpio_pin != GPIO_NUM_MAX; ch++)
        printf("  %s  ", adc1[ch].name);
    for (ch = 0; adc2[ch].gpio_pin != GPIO_NUM_MAX; ch++)
        printf("  %s  ", adc2[ch].name);
    printf("\n");
    printf("---------------------------------------------------------------------------------------------------------------------\n");

    for (ch = 0; adc1[ch].gpio_pin != GPIO_NUM_MAX; ch++)
    {
        adc1_value[ch] = (uint16_t) (adc1_get_raw(adc1[ch].channel) & 0xFFFF);
        vTaskDelay(10 / portTICK_PERIOD_MS);
    }
    for (ch = 0; adc2[ch].gpio_pin != GPIO_NUM_MAX; ch++)
    {
        adc2_get_raw(adc2[ch].channel, ADC_WIDTH_BIT_12, &val);
        adc2_value[ch] = (uint16_t) (val & 0xFFFF);
        vTaskDelay(10 / portTICK_PERIOD_MS);
    }
    for (ch = 0; adc1[ch].gpio_pin != GPIO_NUM_MAX; ch++)
        printf("  %4d  ", adc1_value[ch]);
    for (ch = 0; adc2[ch].gpio_pin != GPIO_NUM_MAX; ch++)
        printf("  %4d  ", adc2_value[ch]);
    printf("\n");
    for (ch = 0; adc1[ch].gpio_pin != GPIO_NUM_MAX; ch++)
    {
        voltage = (((uint32_t) adc1_value[ch]) * VOLTAGE_MAX) / ADC_VALUE_MAX;
        printf("  %4d  ", voltage);
    }
    for (ch = 0; adc2[ch].gpio_pin != GPIO_NUM_MAX; ch++)
    {
        voltage = (((uint32_t) adc2_value[ch]) * VOLTAGE_MAX) / ADC_VALUE_MAX;
        printf("  %4d  ", voltage);
    }
    printf(" mV\n");
    printf("=====================================================================================================================\n");
    vTaskDelay(20000 / portTICK_PERIOD_MS);
}

void app_main(void)
{
    
    setup_gpio();
    setup_adc();
    printf("=====================================================================================================================\n");
    for (int i = 0; i < 13; i++)
        read_analog_inputs();
    printf("Gratulations, the test is completed\n");
}
Connection order of ADC channels with GND :
A2_9 A2_8 A1_6 A1_3 A1_0 A2_0 A2_6 A1_4 A2_3 A1_5 A2_7 A2_5 A2_4

A1_7 is permanent internally (on the PCB) wired to the half Vbat (see Feather schamtics) and should display/read a value of about 2070 mV
The real voltage on the pin Vbat is 4130 mV.
Running this modified code I've got the following output

Code: Select all

MONITOR
--- idf_monitor on /dev/ttyUSB0 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5800
load:0x40078000,len:0
ho 12 tail 0 room 4
load:0x40078000,len:15016
entry 0x40078630
I (30) boot: ESP-IDF v3.1-dev-1193-g64b56bef 2nd stage bootloader
I (30) boot: compile time 07:47:37
I (31) boot: Enabling RNG early entropy source...
I (36) boot: SPI Speed      : 40MHz
I (41) boot: SPI Mode       : DIO
I (45) boot: SPI Flash Size : 4MB
I (49) boot: Partition Table:
I (52) boot: ## Label            Usage          Type ST Offset   Length
I (59) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (67) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (74) boot:  2 factory          factory app      00 00 00010000 00100000
I (82) boot: End of partition table
I (86) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x06490 ( 25744) map
I (104) esp_image: segment 1: paddr=0x000164b8 vaddr=0x3ffb0000 size=0x02388 (  9096) load
I (108) esp_image: segment 2: paddr=0x00018848 vaddr=0x40080000 size=0x00400 (  1024) load
0x40080000: _iram_start at /opt/esp32/esp-idf/components/freertos/xtensa_vectors.S:1685

I (113) esp_image: segment 3: paddr=0x00018c50 vaddr=0x40080400 size=0x073c0 ( 29632) load
I (134) esp_image: segment 4: paddr=0x00020018 vaddr=0x400d0018 size=0x137b8 ( 79800) map
0x400d0018: _flash_cache_start at ??:?

I (162) esp_image: segment 5: paddr=0x000337d8 vaddr=0x400877c0 size=0x01328 (  4904) load
0x400877c0: rtc_clk_cal_internal at /opt/esp32/esp-idf/components/soc/esp32/rtc_time.c:127

I (165) esp_image: segment 6: paddr=0x00034b08 vaddr=0x400c0000 size=0x00000 (     0) load
I (174) boot: Loaded app from partition at offset 0x10000
I (175) boot: Disabling RNG early entropy source...
I (181) cpu_start: Pro cpu up.
I (184) cpu_start: Starting app cpu, entry point is 0x40081054
0x40081054: call_start_cpu1 at /opt/esp32/esp-idf/components/esp32/cpu_start.c:225

I (0) cpu_start: App cpu up.
I (195) heap_init: Initializing. RAM available for dynamic allocation:
I (202) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (208) heap_init: At 3FFB33E8 len 0002CC18 (179 KiB): DRAM
I (214) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (220) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (227) heap_init: At 40088AE8 len 00017518 (93 KiB): IRAM
I (233) cpu_start: Pro cpu start user code
I (27) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4095    4095    3614    3788    4095    1791     363     823     567     395     395     348     491       0  
  3900    3900    3441    3607    3900    1705     345     783     540     376     376     331     467       0   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4095    4095    3599    3764    4095    1777     369     541     709     429     347     368       0     432  
  3900    3900    3427    3584    3900    1692     351     515     675     408     330     350       0     411   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4068    4095    3567    3781    4095    1740     386     527     662     395     364     347     496     437  
  3874    3900    3397    3600    3900    1657     367     501     630     376     346     330     472     416   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4089    4095    3533    3807    4095    1807     434     490     749     358     384     314     520     419  
  3894    3900    3364    3625    3900    1720     413     466     713     340     365     299     495     399   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4095    4095    3562    3773    4095    1784     390     510     689     384     362     341     482     448  
  3900    3900    3392    3593    3900    1699     371     485     656     365     344     324     459     426   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4095    4095    3604    3759    4095    1812       0     509     625     397     326     354     469     452  
  3900    3900    3432    3580    3900    1725       0     484     595     378     310     337     446     430   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4079    4095    3562    3802    4095    1801     389     496     688     358       0     319     501     429  
  3884    3900    3392    3620    3900    1715     370     472     655     340       0     303     477     408   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4095    4095    4095    3663    4095    1749     403     473     721     341     399     307     505     429  
  3900    3900    3900    3488    3900    1665     383     450     686     324     380     292     480     408   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4095    4095    3658    3736    4095    1801     371       0     640     402     160     370     477     448  
  3900    3900    3483    3558    3900    1715     353       0     609     382     152     352     454     426   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4086    4095    3471    4095    4095    1786     401     448     734     320     417     285     518     415  
  3891    3900    3305    3900    3900    1700     381     426     699     304     397     271     493     395   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4095    4095    3644    3711    4095    1745     355    2837     592     417     297       0     454     444  
  3900    3900    3470    3534    3900    1661     338    2701     563     397     282       0     432     422   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4094    4095    3450    4095    3949    1772     416     820     784       0     411     240     559     408  
  3899    3900    3285    3900    3760    1687     396     780     746       0     391     228     532     388   mV
=====================================================================================================================
  A1_0    A1_3    A1_4    A1_5    A1_6    A1_7    A2_0    A2_3    A2_4    A2_5    A2_6    A2_7    A2_8    A2_9  
---------------------------------------------------------------------------------------------------------------------
  4095    4095    3562    3788    4095    1762     401     487       0     343     367     324     502     432  
  3900    3900    3392    3607    3900    1678     381     463       0     326     349     308     478     411   mV
=====================================================================================================================
Gratulations, the test is completed
Nobody is perfect!
My name is NOT Nobody...


Nobody lives forever to correct his mistakes someday in the future, if not right now!
Covid-19 is not responsible for any human failures!

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: ADC1 on Adafruit Feather HUZZAH-ESP32 doesn't work with esp-idf

Postby ESP_Dazz » Wed Jun 20, 2018 3:42 am

When I run the code above on the EPS32 Wrover Kit, the results are inverted (where connecting an A1_x channel to GND results in a reading of 4095, and connecting to HIGH results in 0). I think there might be something wrong with your ADC and GPIO configuration code. I've modified the example as follows...

Code: Select all

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "driver/gpio.h"
#include "driver/i2c.h"
#include "driver/adc.h"

#include "freertos/FreeRTOS.h"

#include "esp_system.h"


#define NO_OF_CHANS_ADC1        6
#define NO_OF_CHANS_ADC2        8

#define ADC_VALUE_MAX           4095
#define VOLTAGE_MAX             3900

static adc1_channel_t adc1_channels[NO_OF_CHANS_ADC1] = {ADC1_CHANNEL_0, ADC1_CHANNEL_3, ADC1_CHANNEL_4,
                                                         ADC1_CHANNEL_5, ADC1_CHANNEL_6, ADC1_CHANNEL_7};
static char adc1_channel_names [NO_OF_CHANS_ADC1][8] = {"A1_0", "A1_3", "A1_4", "A1_5", "A1_6", "A1_7"};

adc2_channel_t adc2_channels[NO_OF_CHANS_ADC2] = {ADC2_CHANNEL_0, ADC2_CHANNEL_3, ADC2_CHANNEL_4, ADC2_CHANNEL_5,
                                                  ADC2_CHANNEL_6, ADC2_CHANNEL_7, ADC2_CHANNEL_8, ADC2_CHANNEL_9};
static char adc2_channel_names [NO_OF_CHANS_ADC2][8] = {"A2_0", "A2_3", "A2_4", "A2_5", "A2_6", "A2_7", "A2_8", "A2_9"};

void setup_adc()
{
    adc1_config_width(ADC_WIDTH_BIT_12);
    for (int i = 0; i < NO_OF_CHANS_ADC1; i++) {
        adc1_config_channel_atten(adc1_channels[i], ADC_ATTEN_DB_11);

    }
    for (int i = 0; i < NO_OF_CHANS_ADC2; i++) {
        adc2_config_channel_atten(adc2_channels[i], ADC_ATTEN_DB_11);
    }
}

void read_analog_inputs()
{
    int adc1_readings[NO_OF_CHANS_ADC1];
    int adc2_readings[NO_OF_CHANS_ADC2];

    for (int i = 0; i < NO_OF_CHANS_ADC1; i++) {
        printf("  %s  ", adc1_channel_names[i]);
    }
    for (int i = 0; i < NO_OF_CHANS_ADC2; i++) {
        printf("  %s  ",adc2_channel_names[i]);
    }
    printf("\n");
    printf("---------------------------------------------------------------------------------------------------------------------\n");

    for (int i = 0; i < NO_OF_CHANS_ADC1; i++) {
        adc1_readings[i] = adc1_get_raw(adc1_channels[i]);
        vTaskDelay(pdMS_TO_TICKS(10));
    }
    for (int i = 0; i < NO_OF_CHANS_ADC2; i++) {
        adc2_get_raw(adc2_channels[i], ADC_WIDTH_BIT_12, &adc2_readings[i]);
        vTaskDelay(pdMS_TO_TICKS(10));
    }

    for (int i = 0; i < NO_OF_CHANS_ADC1; i++) {
        printf("  %4d  ", adc1_readings[i]);
    }
    for (int i = 0; i < NO_OF_CHANS_ADC2; i++) {
        printf("  %4d  ", adc2_readings[i]);
    }
    printf("\n");

    for (int i = 0; i < NO_OF_CHANS_ADC1; i++) {
        int voltage = (((uint32_t) adc1_readings[i]) * VOLTAGE_MAX) / ADC_VALUE_MAX;
        printf("  %4d  ", voltage);
    }
    for (int i = 0; i < NO_OF_CHANS_ADC2; i++) {
        int voltage = (((uint32_t) adc2_readings[i]) * VOLTAGE_MAX) / ADC_VALUE_MAX;
        printf("  %4d  ", voltage);
    }
    printf(" mV\n");
    printf("=====================================================================================================================\n");
    vTaskDelay(pdMS_TO_TICKS(2000));
}

void app_main(void)
{
    setup_adc();
    printf("=====================================================================================================================\n");
    while (1) {
        read_analog_inputs();
    }
}
Please see if the following code works for you.

User avatar
MickPF
Posts: 45
Joined: Tue Apr 03, 2018 8:47 pm

Re: ADC1 on Adafruit Feather HUZZAH-ESP32 doesn't work with esp-idf

Postby MickPF » Wed Jun 20, 2018 7:25 am

Hello,

Yes, that's it! :D
As a result to me, the call 'adc_set_data_inv' works incorrectly!
After changing this call in my code, it outputs the expected values.

Many, many Thanks for your help, I was already desperate and wanted to throw the project in the trash.
I apologize if sometimes I seem a little rude or aggressive.

Best Regards,
Michael
Nobody is perfect!
My name is NOT Nobody...


Nobody lives forever to correct his mistakes someday in the future, if not right now!
Covid-19 is not responsible for any human failures!

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: ADC1 on Adafruit Feather HUZZAH-ESP32 doesn't work with esp-idf

Postby ESP_Dazz » Wed Jun 20, 2018 8:08 am

You can probably also remove any GPIO calls in your code, because calls to adc1_config_channel_atten() and adc2_config_channel_atten() will already configure the necessary GPIOs.

Who is online

Users browsing this forum: No registered users and 100 guests