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
}