Cueent sensor sct 013 does not giving correct current value with esp32

i am using current sensor sct 013 50A/1V with the esp32 wroom devkit module but i am not getting any correct value of current. i am using the code mentioned below. please help me if anyone have any idea

#include <Arduino.h>

const int sensorPin = 34; // Analog input pin for the SCT-013-030 sensor
const float burdenResistor = 32.0; // Burden resistor value in ohms
const float calibrationFactor = 50.0; // Calibration factor for converting voltage to current
const float vRef = 3.315; // Reference voltage of ESP32
const int numSamples = 2000; // Number of samples for averaging

void setup() {
Serial.begin(115200); // Initialize serial communication
pinMode(sensorPin, INPUT); // Set the sensor pin as input
}

void loop() {
long sumOfSquares = 0;
for (int i = 0; i < numSamples; i++) {
int sensorValue = analogRead(sensorPin); // Read the analog value from the sensor
float sensorVoltage = (sensorValue / 4095.0) * vRef; // Convert the analog value to voltage
float centeredVoltage = sensorVoltage - (vRef / 2); // Center the signal around zero
float current = (centeredVoltage / burdenResistor) * calibrationFactor; // Calculate the current
sumOfSquares += (current * current); // Sum of squares for RMS calculation

delayMicroseconds(10);  // Small delay for sampling rate control

}

float meanSquare = sumOfSquares / numSamples;
float rmsCurrent = sqrt(meanSquare); // Calculate RMS current

Serial.print("RMS Current: ");
Serial.print(rmsCurrent);
Serial.println(" A");

delay(1000); // Delay between readings
}

what circuit are you using to connect the SCT-013 to the ESP32 ADC?
the SCT-013 output is a AC voltage which you need to offset to input to the ADC then sample over several cycles to get the RMS current
have a look at measure-ac-current-using-arduino-and-sct-013

using the SCT-013 clamps with the ESP32 I use a ESP32-4-Channel-Mains-Current-Sensor

improve your code presentation by using code tags, e.g. click < CODE > and paste you code where it says 'type or paste code here

It is always better to use a digital sensor rather than an analog sensor. More accuracy, stability, and reliability. It requires more programming - but less hassle and headache, and surely the library functions are available.

Checkout the current (and voltage) sensors based on the INA226 and the INA236 chips.

Sorry, I can’t follow your code or the missing annotated schematic. Please post a complete schematic showing all connections and hardware components. Additionally, when sharing code, use code tags (e.g., <code> ... </code> or triple backticks `````) to ensure proper formatting for readability.