Esp32 wrong voltage mesurement

I am using esp 32 for voltage and current measurement using acs712 and voltage divider method i used R1 = 1 megohm
R2=22k.But its showing wrong voltage and current measurement.Can someone help me to figure it out.

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <DHT.h>


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const int ACS712_PIN = 35;
const int VOLTAGE_PIN = 34;

char auth[] = "*****";
char ssid[] = "****";
char pass[] = "****";

void setup() {
  Serial.begin(9600);
  dht.begin();
  display.begin(0x3C);
  display.clearDisplay();
  display.display();
  
  Blynk.begin(auth, ssid, pass);
}

void loop() {
  float voltage = (analogRead(VOLTAGE_PIN) * 0.00488) * 5.0;
  float current = (analogRead(ACS712_PIN) - 2500) / 66;
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0,0);
  display.print("Voltage: ");
  display.print(voltage);
  display.println(" V");
  display.print("Current: ");
  display.print(current);
  display.println(" A");
  display.print("Humidity: ");
  display.print(humidity);
  display.println(" %");
  display.print("Temperature: ");
  display.print(temperature);
  display.println(" C");
  display.display();
  
  Blynk.virtualWrite(V0, voltage);
  Blynk.virtualWrite(V1, current);
  Blynk.virtualWrite(V2, humidity);
  Blynk.virtualWrite(V3, temperature);

  Blynk.run();

  delay(1000);
}


Perhaps one or more of your magical constants is wrong.

Perhaps the '5.0' should be '3.3'?

I think the ESP32 might default to 12-bit ADC values (0-4095) instead of the Arduino UNO 10-bit values (0-1023).

so can you please rearrange the code?can you please mention why acs712 5a is showing wrong amps

 float voltage = (analogRead(VOLTAGE_PIN) * 0.00488) * 3.3;

its still showing as 2.61v ,i have connected 12.30 as input

You haven't taken the potential divider into account.

( that magic number 0.00488 looks like 5.0/1024.0 to me )

can you share the code for measuring voltage in serial monitor for esp32

const int analogInPin = 34; // Analog input pin that the voltage sensor is attached to
const float R1 = 1000000.0; // Value of R1 in ohms
const float R2 = 22000.0; // Value of R2 in ohms

void setup() {
  Serial.begin(9600); // initialize serial communication at 9600 bits per second
}

void loop() {
  // read the input on the analog pin:
  int sensorValue = analogRead(analogInPin);
  // Convert the analog reading (which goes from 0 - 4095) to a voltage (0 - 3.3V):
  float voltage = sensorValue * (3.3 / 4095.0);
  // calculate the actual voltage based on the voltage divider circuit:
  float actualVoltage = voltage * (R1 + R2) / R2;
  // print out the voltage value
  Serial.print("Voltage: ");
  Serial.print(actualVoltage);
  Serial.println(" V");
  delay(1000); // delay for 1 second
}

i tried this code but still shows wrong voltage

Is it so hard to link to the data sheet?

how are these connected? Hint ..

Can you tell us what the "wrong" values are?

image

R1-1MEGAOGM
R2-22K
VOUT-D34
When i connect 12.3 as input i got voltage output as 6.18

image

Impossible
20230427_033133

so what i have to do to rectify the problem

can you please share the code for esp32 to monitor voltage i think something is wrong in my code

image
OK so lets be clear, ignore the ACS712, you are measuring a voltage Vout with an ESP32 as shown above.
You apply 12.3V as Vin. So Vout = 12.3 *22k/1022k = 0.264V
in your program you calculate V - but you have not set the attenuation nor the resolution for the ADC.
So the default will be 12 bits and attenuation of 11dB which gives a useful range of
ADC_ATTEN_DB_11 150 mV ~ 2450 mV

So basically you are getting the wrong results because you are not converting the raw data in the right way.

Also the ADC on the ESP32 is a piece of ....

const int analogInPin = 34; // Analog input pin that the voltage sensor is attached to
const float R1 = 1000000.0; // Value of R1 in ohms
const float R2 = 22000.0; // Value of R2 in ohms

void setup() {
  Serial.begin(9600); // initialize serial communication at 9600 bits per second
}

void loop() {
  // read the input on the analog pin:
  int sensorValue = analogRead(analogInPin);
  // Convert the analog reading (which goes from 0 - 4095) to a voltage (0 - 3.3V):
  float voltage = sensorValue * (3.3 / 4095.0);
  // calculate the actual voltage based on the voltage divider circuit:
  float actualVoltage = voltage * (R1 + R2) / R2;
  // print out the voltage value
  Serial.print("Voltage: ");
  Serial.print(actualVoltage);
  Serial.println(" V");
  delay(1000); // delay for 1 second
}

so can u help me to make the changes in this code

can you help to edit the code

The ESP32 uses it's own internal reference of 1.1 volts (nominal) as the ADC reference voltage.

See https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32/api-reference/peripherals/adc.html for details.

Replace the 3.3 in you sketch with 1.1

This should give you sensible readings for Vin in the range 150mV to 2450mV

const int analogInPin = 34; // Analog input pin that the voltage sensor is attached to
const float R1 = 1000000.0; // Value of R1 in ohms
const float R2 = 22000.0; // Value of R2 in ohms

void setup() {
  Serial.begin(115200); // initialize serial communication USE A SENSIBLE RATE!
}

void loop() {
  // read the input on the analog pin:
  int sensorValue = analogRead(analogInPin);
  // Convert the analog reading (which goes from 0 - 4095, ie 4096 steps) to a voltage 
sensorValue += 120; //adjust for zero offset
  float voltage = sensorValue * (1.1 / 4096.0);  //1.1 is the "average" reference voltage
  voltage = voltage * 3.55;  // the ADC has a default gain of -11dB to allow a safe input voltage range of 3.3V
  // calculate the actual voltage based on the voltage divider circuit:
  float actualVoltage = voltage * (R1 + R2) / R2;
  // print out the voltage value
  Serial.print("Voltage: ");
  Serial.print(actualVoltage);
  Serial.println(" V");
  delay(1000); // delay for 1 second
}
The ESP32 ADC has a serious zero offset error.  To get useful results from a low input voltage you need to add ABOUT 120 to your reading, as shown above.

Realistically if you want to measure voltage with an ESP32 the best way is to use an external ADC