ESP32 ADC error

Trying to read readings from the ADC of the esp32c3 mini-1 on the serial monitor but the serial monitor shows the adc error which is this
ADC2 is no longer supported please use ADC1

#include <driver/adc.h>
#include <esp_adc_cal.h>

#define PV_SENSOR ADC2_CHANNEL_0 // GPIO 5
#define B_SENSOR ADC1_CHANNEL_3  // GPIO 3
#define I_SENSOR ADC1_CHANNEL_4  // GPIO 4

float adc_voltage = 0.0;
float in_voltage = 0.0;

float adc_voltage1 = 0.0;
float in_voltage1 = 0.0;

float R1 = 30000.0;
float R2 = 7500.0;

float R3 = 30000.0;
float R4 = 7500.0;

float ref_voltage = 3.3;

int adc_value = 0;
int adc_value1 = 0;

int sensitivity = 66;
int adcValue = 0;
int offsetVoltage = 2500;
double adcVoltage = 0;
double currentValue = 0;

void setup()
{
  Serial.begin(9600);

  // Configure ADC channels
  adc1_config_width(ADC_WIDTH_BIT_12);
  adc1_config_channel_atten(B_SENSOR, ADC_ATTEN_DB_11);
  adc1_config_channel_atten(I_SENSOR, ADC_ATTEN_DB_11);
  adc2_config_channel_atten(PV_SENSOR, ADC_ATTEN_DB_11);
}

void loop()
{
  PV_voltage();
  P_voltage();
  I_sensor();
  delay(1000);
}

void PV_voltage()
{
  int raw;
  if (adc2_get_raw(PV_SENSOR, ADC_WIDTH_BIT_12, &raw) == ESP_OK)
  {
    adc_value = raw;
    adc_voltage = (adc_value * ref_voltage) / 4096.0;
    in_voltage = adc_voltage * (R1 + R2) / R2;

    Serial.print("PV_Voltage = ");
    Serial.println(in_voltage, 2);
  }
  else
  {
    Serial.println("Error reading PV sensor");
  }
}

void P_voltage()
{
  adc_value1 = adc1_get_raw(B_SENSOR);
  adc_voltage1 = (adc_value1 * ref_voltage) / 4096.0;
  in_voltage1 = adc_voltage1 * (R3 + R4) / R4;

  Serial.print("B_Voltage = ");
  Serial.println(in_voltage1, 2);
}

void I_sensor()
{
  adcValue = adc1_get_raw(I_SENSOR);
  adcVoltage = (adcValue / 4096.0) * ref_voltage * 1000; // Convert to mV
  currentValue = ((adcVoltage - offsetVoltage) / sensitivity);

  Serial.print("\t Current = ");
  Serial.println(currentValue, 3);
}

Ok, so change the reference to adc2 to adc1 like it says.

If you use wifi on esp32, for analog use adc1.

Instead of this, on an ESP32-C3, use ADC1 which is assigned to pins GPIO0 to GPIO4

See here: Analog to Digital Converter (ADC) - ESP32-C3 - — ESP-IDF Programming Guide v4.4 documentation and look at the limitations.

Can't changed the pins on the PCB right now

There is a error in the IC and Espessif decided not to fix it. They expect you to read the data sheets and the eratta. Although I have to admit they did not make it easy to find the information.

1 Like

A scalpel and some thin insulated wire is all you need to fix the PCB.

1 Like