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
- Voltage sensors are connected to IO5 & IO3
- Current sensor (ACS712) is connected to IO4
#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);
}