UncleRus / esp-idf-lib

Component library for ESP32-xx and ESP8266
https://esp-idf-lib.readthedocs.io/en/latest/
1.33k stars 414 forks source link

INA219 - Wrong current reading #622

Open r3v1 opened 3 months ago

r3v1 commented 3 months ago

The issue

I'm running the example provided by de library with an ESP32 and INA219 sensor, configured for 400mA, 16V and 40mV gain as:

  ESP_ERROR_CHECK(ina219_configure(&dev, INA219_BUS_RANGE_16V, INA219_GAIN_1,
                                   INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS));

That is the same configuration I've been using the Adafruit INA219 library in Arduino. However, the esp-idf-lib library reports wrong measures on this simple circuit (also simulated in Falstad):

image

The multimeter shows almost the same value around 36mA, but the library says about 76mA.

I think the calibration factor is somehow, in the Arduino library says 0x2000 and here, as is computed dinamically, says 0x1000. I tried to hardcode this value with no positive results. Shunt resistor is set to 100miliohms.

Which SDK are you using?

esp-idf

Which version of SDK are you using?

master

Which build target have you used?

Component causing the issue

ina219

Anything in the logs that might be useful for us?

I (102) main_task: Started on CPU0
I (102) main_task: Calling app_main()
I (102) INA219_example: Initializing INA219
D (102) i2cdev: Reconfiguring I2C driver on port 0
D (102) i2cdev: I2C driver successfully reconfigured on port 0
D (112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
D (112) ina219: Initialize, config: 0x03ff
I (112) INA219_example: Configuring INA219
D (112) ina219: Config: 0x019f
D (112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
I (112) INA219_example: Calibrating INA219
D (112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
D (112) ina219: Calibration: 0.1000 Ohm, 0x1000
D (112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
I (102) main_task: Returned from app_main()
I (112) INA219_example: Starting the loop
D (112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
112,92.00
D (1112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
1112,-100.00
D (2112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
2112,76.00
D (3112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
3112,76.00
D (4112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
4112,76.00
D (5112) i2cdev: Timeout: ticks = 0 (0 usec) on port 0
5112,76.00

Additional information or context

No response

Confirmation