ESP32 current monitor DC with ACS758 or ACS712

I'm trying to do a project to make an amperage monitor, the problem is that I can't find anything that works, the codes that use libraries don't work either, I have the ESP32 WROOM-32 from aliexpress, and the 30A ACS712 sensors and the 50A ACS758, someone who has done this project before can guide me... I don't know if it is a problem with the ESP32 that I am using... any recommendation is welcome...

Start by posting a schematic of your project and a photograph of how you have everything connected.
What kind of currents do you expect to measure and what are you using for testing your setup?
Please also share the code of your best/closest/most recent attempt and indicate what specifically does not work as intended about it.

This should get us going; please note that the list above is not "either/or", but kindly provide all of it.

Okay, I have connected the ESP32 in my first test to the ACS758, using this code:

const int CURRENT_SENSE_PIN = 34; 
const float VOLTAGE_REFERENCE = 3.3; 
const float SENSITIVITY = 0.04; 
const float OFFSET_VOLT = VOLTAGE_REFERENCE / 2; 
 
void setup() {
  Serial.begin(115200); 
  pinMode(CURRENT_SENSE_PIN, INPUT); 
}
 
void loop() {
  int raw_adc = analogRead(CURRENT_SENSE_PIN);
   float voltage = raw_adc * (VOLTAGE_REFERENCE / 4095.0);
   float current = (voltage - OFFSET_VOLT)/ SENSITIVITY; 
 
  Serial.print("Current: ");
  Serial.print(current);
  Serial.println(" A");
 
  delay(1000); 
}

The connections of my ESP32 are direct to my ACS758, VCC of the sensor to 3V3 of the ESP32, OUT1 to G34 on the esp32 and the GND to the GND of the esp32, there is one free pin left on the OUT2 sensor.

The result without load on the sensor is:

Current: -3.68 A
Current: -3.52 A
Current: -3.25 A
Current: -3.48 A
Current: -2.61 A
Current: -3.54 A
Current: -3.41 A

When doing small loads practically nothing changes, I know that I should do a larger load, but before carrying out the project I need to check that it is possible to monitor more or less accurately

The current I want to monitor is for a small inverter from a battery, it would be AC that I want to measure

I moved your topic to a more appropriate forum category @pfer_vva.

The Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board.

In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

if you are measuring AC current consider a non-invasive current clamp such as the SCT-013
I used a ESP32-4-Channel-Mains-Current-Sensor and three 100amp clamps to measure 3-phase current to a house
remember you need to take samples over several cycles to calculate RMS current

Those sensors are 5V sensors. I think the ACS773 is a 3.3V sensor. I’ve used these sensors a lot with 328Ps. The minimum supply voltage you are supposed to use is 4.5V. They are supposed to have an under voltage lockout.

Is it?

By design, the ADC reference voltage for ESP32 is 1100 mV. However, the true reference voltage can range from 1000 mV to 1200 mV among different chips.
Analog to Digital Converter (ADC) Calibration Driver - ESP32 - — ESP-IDF Programming Guide v5.2.1 documentation

Now, many/most boards will have some ADC pins broken out via a voltage divider so that the 'reference' does end up being higher - or rather, the input signal is divided before it's fed into the ADC. However, this depends on the board and it's not inherent to the ESP32 WROOM module. What board are you using exactly - and more importantly, have you verified how IO34 is broken out (directly or does it tap into a voltage divider)?

Moreover, you're using this 'offset' as the basis for the zero point setting of your ACS sensor. This will get you into trouble if your 3.3V supply fluctuates - which it will, because there's a microcontroller fed from it (for all we know) and that tends to create some nasty transients a lot of the time. If I were you, I'd make sure to feed the sensor from a stable, separately filtered power supply.

Since the sensor is ratiometric, you'll also have to compensate for any differences in supply voltage vs. what you expect it to be. You're now assuming 3.3V. The readings you present align with a a supply voltage of around 3.0V or thereabouts that's interpreted wrongly. Measure the actual supply voltage on your sensor and take that into account when interpreting your readings. You may want to use a second ADC input to measure this so that you always have a valid pair of supply + output voltage to work with.

What is the actual voltage on the sensor output pin and what is the voltage you're measuring through the ADC? Start there before doing the conversion into current. See also notes about supply voltage above.

In fact, start one step back: verify that the ACS sensor output pin gives the anticipated voltage for the current you're measuring.

Are you using a module or a bare chip? What's the schematic of the implementation of the sensor assembly? Which ACS758 are you using; a bidirectional or a unidirectional one? Your code assumes ACS758LCB-050B - is this correct?

What is the actual current range you will need to measure? Note that you're using (in this case) a sensor with a range of +/-50A and a sensitivity of (I assume, as you do) 40mV/A. If you're going to monitor fairly small currents, you'll have to design something that'll accurately measure fairly small output signals from this sensor.

Also, you want to measure AC - at what frequency? 50/60Hz? What's your sampling strategy for this? Will you attempt to sample the full AC cycle and then average out mathematically? Or will you run the sensor output through a buffer and an R/C filter to smooth it out?

I thought about that, but from the 758 datasheet:
image
Then again, I doubt this sensor is a great choice for measuring relatively small currents.

Yesterday I realized this, if you measure the pinout with 3.3v it doesn't work at all, in my case with 5.3v it returned the pin out 2.8v, I managed to do it with another code, when I check if the measurements are more exact I'll let you know for those who are having trouble getting it to work properly.

UPDATE

#define currentpin 4 // Eliminé el símbolo de igual (=)

void setup() {
  Serial.begin(115200);
  pinMode(currentpin, INPUT);
}

void loop() {
  int adc = analogRead(currentpin);
  float adc_voltage = adc * (5.3 / 4096.0); // Ajuste para 5V
  float current = adc_voltage / 0.066; // Ajuste basado en la sensibilidad del sensor
  Serial.print("Current Value: ");
  Serial.println(current - 61.7);
  delay(100);
}

Ok, you might wonder why -61.7v in the end, this is because I don't have a voltage divider and the sensor is working with 5.3v, this afternoon I will do the test with the resistors to be able to achieve this, in this way I have also I've gotten pretty close to the value, I need to check it with my serial multimeter, I'll do it this afternoon, I've been with this all night and I need to rest :slight_smile:

By the way, I am using a normal and common ESP32 from Aliexpress, just as the sensor is bidirectional and is not bare, it is the sensor with its pcb and its connection terminals, in this last case, the test was with the acs712, which also It was bought on Aliexpress.

The consumption that I want to measure is from a battery, that is, it would be direct current, to answer your questions... the inverter that I want to use as a load is 300w peak and will use a voltage of 16.8v, it is a 4S10P battery that I made laptop and I want to monitor it :slight_smile:

There are dozens of different kinds of boards out there.
Likewise for the sensor. You need to figure out what you have exactly, in particular the signal path.

But you said earlier you want to measure on the AC (output) side?
Btw, I'd measure on the DC/input side; the current will be higher so it'll be easier to get a more precise measurement with the relatively insensitive sensors you're using. 300W at 16.8V is close to 18A; if you use e.g. ACS712ELCTR-30A-T this means you're using ~ 65% of its measurement range, this is pretty nice.

That's not good. It should have been 2.65V at no load. Could be a measurement error. Where did you measure the 5.3V exactly?
Note that things like layout/wiring will have an influence. This is one of the reasons I asked earlier to show a picture of your setup. That was not just some kind of obligatory completeness thing; it actually meant something.

An ACS712/758 is a 5volt-only ratiometric sensor. It is best used with a 5volt logic MCU with ratiometric A/D (Uno, Mega, etc.). The ESP, with it's 3.3volt logic and absolute A/D is the wrong partner for this sensor. You will have two problems to solve. Logic levels and supply voltage variations.

It seems you want to measure high current DC.
An INA219 (12-bit) or INA226 (16-bit) with external shunt could be a far better solution.
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.