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