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.
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
}
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.
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
}
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