Forum >DFR1075 - Firebeetle 2 - ESP32-c6 analog read

DFR1075 - Firebeetle 2 - ESP32-c6 analog read

userHead iisfaq 2025-03-02 07:45:04 497 Views2 Replies

I have a simple app that performs an Analog read from A1 (ADC1_2) and A2 (ADC1_3)

 

I have a LDR connected to 10K resister and I am reading this value.

 

The ADC 1 channel 2 gives me a light value of 192 and a dark value of 1456.


The ADC 1 channel 3 gives me a light value of 176 and a dark value of 336

 

Why is this? 

 

I have tried disabling pull up and pull down on the channels. Made no difference.

 

The pins appear to be quite generic and not really connected to anything else.

 

I am wondering why the difference in values. And is there any way to make them the same value?

 

I am using ESP-IDF because arduino IDE seems to have issues with serial ports on these devices now. Also I could not upload the code as a file as it says 10MB.

/*

 * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD

 *

 * SPDX-License-Identifier: Apache-2.0

 */

 #include <stdio.h>

 #include <stdlib.h>

 #include <string.h>

 #include "freertos/FreeRTOS.h"

 #include "freertos/task.h"

 #include "soc/soc_caps.h"

 #include "esp_log.h"

 #include "esp_adc/adc_oneshot.h"

 #include "esp_adc/adc_cali.h"

 #include "esp_adc/adc_cali_scheme.h"

 #include "driver/gpio.h"


 

 const static char *TAG = "EXAMPLE";

 

 /*---------------------------------------------------------------

         ADC General Macros

 ---------------------------------------------------------------*/

 //ADC1 Channels

 #define EXAMPLE_ADC1_CHAN0          ADC_CHANNEL_2

 #define EXAMPLE_ADC1_CHAN1          ADC_CHANNEL_3

 

 // GPIO pins corresponding to ADC channels

 #define GPIO_ADC1_CHAN0             2  // GPIO 2 for ADC Channel 2

 #define GPIO_ADC1_CHAN1             3  // GPIO 3 for ADC Channel 3

 

 #define EXAMPLE_ADC_ATTEN           ADC_ATTEN_DB_12

 

 static int adc_raw[2];  // Store one reading per channel

 static bool do_calibration = true;

 static adc_cali_handle_t adc1_cali_handle = NULL;

 

 static bool init_adc_calibration(void) {

     esp_err_t ret = ESP_FAIL;

     bool calibration_enabled = false;

 

 #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED

     if (!calibration_enabled) {

         ESP_LOGI(TAG, "Calibration scheme: Curve Fitting");

         adc_cali_curve_fitting_config_t cali_config = {

             .unit_id = ADC_UNIT_1,

             .atten = EXAMPLE_ADC_ATTEN,

             .bitwidth = ADC_BITWIDTH_12,

         };

         ret = adc_cali_create_scheme_curve_fitting(&cali_config, &adc1_cali_handle);

         if (ret == ESP_OK) {

             calibration_enabled = true;

         }

     }

 #endif

 

 #if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED

     if (!calibration_enabled) {

         ESP_LOGI(TAG, "Calibration scheme: Line Fitting");

         adc_cali_line_fitting_config_t cali_config = {

             .unit_id = ADC_UNIT_1,

             .atten = EXAMPLE_ADC_ATTEN,

             .bitwidth = ADC_BITWIDTH_12,

         };

         ret = adc_cali_create_scheme_line_fitting(&cali_config, &adc1_cali_handle);

         if (ret == ESP_OK) {

             calibration_enabled = true;

         }

     }

 #endif

 

     return calibration_enabled;

 }

 

 static void deinit_adc_calibration(void) {

     if (adc1_cali_handle) {

 #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED

         adc_cali_delete_scheme_curve_fitting(adc1_cali_handle);

 #elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED

         adc_cali_delete_scheme_line_fitting(adc1_cali_handle);

 #endif

     }

 }

 

 void app_main(void)

 {

     // Disable pull-up/pull-down for ADC pins using direct register access

     // This is a simpler approach than using gpio_config

     gpio_pullup_dis(GPIO_ADC1_CHAN0);    // Disable pull-up on GPIO 2

     gpio_pulldown_dis(GPIO_ADC1_CHAN0);  // Disable pull-down on GPIO 2

     

     gpio_pullup_dis(GPIO_ADC1_CHAN1);    // Disable pull-up on GPIO 3

     gpio_pulldown_dis(GPIO_ADC1_CHAN1);  // Disable pull-down on GPIO 3

     

     ESP_LOGI(TAG, "GPIO pins %d and %d configured with no pull-up/down resistors",

              GPIO_ADC1_CHAN0, GPIO_ADC1_CHAN1);

     

     //-------------ADC Calibration Init---------------//

     do_calibration = init_adc_calibration();

     if (!do_calibration) {

         ESP_LOGE(TAG, "Calibration failed to initialize. Using raw ADC readings.");

     }

 

     //-------------ADC1 Init---------------//

     adc_oneshot_unit_handle_t adc1_handle;

     adc_oneshot_unit_init_cfg_t init_config1 = {

         .unit_id = ADC_UNIT_1,

     };

     ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));

 

     //-------------ADC1 Config---------------//

     adc_oneshot_chan_cfg_t config = {

         .atten = EXAMPLE_ADC_ATTEN,

         .bitwidth = ADC_BITWIDTH_DEFAULT,

     };

     ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, EXAMPLE_ADC1_CHAN0, &config));

     ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, EXAMPLE_ADC1_CHAN1, &config));

 

     // Print confirmation of setup

     ESP_LOGI(TAG, "ADC configured with attenuation: %d", EXAMPLE_ADC_ATTEN);

 

     // Main loop for ADC readings

     while (1) {

         // Read from ADC Channel 2 (GPIO 2)

         ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, EXAMPLE_ADC1_CHAN0, &adc_raw[0]));

         ESP_LOGI(TAG, "ADC%d Channel[%d] (GPIO %d) Raw Data: %d",

                  ADC_UNIT_1 + 1, EXAMPLE_ADC1_CHAN0, GPIO_ADC1_CHAN0, adc_raw[0]);

         

         // Apply calibration if available

         if (do_calibration) {

             int voltage0 = 0;

             ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[0], &voltage0));

             ESP_LOGI(TAG, "ADC%d Channel[%d] (GPIO %d) Calibrated Data: %d mV",

                      ADC_UNIT_1 + 1, EXAMPLE_ADC1_CHAN0, GPIO_ADC1_CHAN0, voltage0);

         }

         vTaskDelay(pdMS_TO_TICKS(250));

 

         // Read from ADC Channel 3 (GPIO 3)

         ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, EXAMPLE_ADC1_CHAN1, &adc_raw[1]));

         ESP_LOGI(TAG, "ADC%d Channel[%d] (GPIO %d) Raw Data: %d",

                  ADC_UNIT_1 + 1, EXAMPLE_ADC1_CHAN1, GPIO_ADC1_CHAN1, adc_raw[1]);

         

         // Apply calibration if available

         if (do_calibration) {

             int voltage1 = 0;

             ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[1], &voltage1));

             ESP_LOGI(TAG, "ADC%d Channel[%d] (GPIO %d) Calibrated Data: %d mV",

                      ADC_UNIT_1 + 1, EXAMPLE_ADC1_CHAN1, GPIO_ADC1_CHAN1, voltage1);

         }

         vTaskDelay(pdMS_TO_TICKS(1000));

     }

 

     // Note: This teardown code will never be reached due to the infinite loop

     ESP_ERROR_CHECK(adc_oneshot_del_unit(adc1_handle));

     if (do_calibration) {

         deinit_adc_calibration();

     }

 }

 

2025-03-04 13:27:38

I have a simple app that performs an Analog read from A1 (ADC1_2) and A2 (ADC1_3)

 

I have a LDR connected to 10K resister and I am reading this value.

 

The ADC 1 channel 2 gives me a light value of 192 and a dark value of 1456.


The ADC 1 channel 3 gives me a light value of 176 and a dark value of 336

 

Why is this? 

userHeadPic SDdgf456.Sg567
iisfaq wrote:

Are you asking me why I am doing this?

 

The ESP32-C6 has multiple Analog to Digital pins.

 

If I connect my LDR circuit with a 10K LDR and a 10K resister, I expect each pin to return the same values +/- a small variation.

 

This is not the case - if I block most of the light from getting to the LDR the value I read is

 

Using Pin 2 - 2200

Using Pin 3 - 705

Using Pin 4 - 2750

Using Pin 5 - 1040

 

Why are they not all the same value?   Why is there such a variation?

 

 

 

 

2025-03-06 12:11:01
1 Replies