Power profiler with INA219 and ESP32-S2 TFT Feather

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
AyubowanPro
 
Posts: 12
Joined: Wed Jul 27, 2022 6:18 am

Power profiler with INA219 and ESP32-S2 TFT Feather

Post by AyubowanPro »

Hello,

I built a power/energy profiler with an INA219 and an ESP32-S2 TFT Feather board. It is working great. I'm using it to measure the energy consumes by an ESP32 remote I built. The thing is, I would like to accurately measure the energy consumes during 1 min, but I'm afraid that the sampling rate is to low to precisely capture all current spikes (due to sending messages through wifi). How can I compensate that ? By using a capacitor on the load side ?

Have a nice day

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by mikeysklar »

You can adjust the ADCResolution for faster sampling. Are you using Arduino or CircuitPython code?

User avatar
AyubowanPro
 
Posts: 12
Joined: Wed Jul 27, 2022 6:18 am

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by AyubowanPro »

Thank you for your reply. I'm using Arduino framework.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by mikeysklar »

The default library for Arduino is configured to take 128 12-bit samples. You can adjust the INA219_CONFIG variables in your code to override with a different number of samples and bit depth.

Code: Select all

#define INA219_CONFIG_BADCRES_MASK (0x0780)
#define INA219_CONFIG_SADCRES_MASK (0x0078) 

Code: Select all

/** values for bus ADC resolution **/
enum {
  INA219_CONFIG_BADCRES_9BIT = (0x0000),  // 9-bit bus res = 0..511
  INA219_CONFIG_BADCRES_10BIT = (0x0080), // 10-bit bus res = 0..1023
  INA219_CONFIG_BADCRES_11BIT = (0x0100), // 11-bit bus res = 0..2047
  INA219_CONFIG_BADCRES_12BIT = (0x0180), // 12-bit bus res = 0..4097
  INA219_CONFIG_BADCRES_12BIT_2S_1060US =
      (0x0480), // 2 x 12-bit bus samples averaged together
  INA219_CONFIG_BADCRES_12BIT_4S_2130US =
      (0x0500), // 4 x 12-bit bus samples averaged together
  INA219_CONFIG_BADCRES_12BIT_8S_4260US =
      (0x0580), // 8 x 12-bit bus samples averaged together
  INA219_CONFIG_BADCRES_12BIT_16S_8510US =
      (0x0600), // 16 x 12-bit bus samples averaged together
  INA219_CONFIG_BADCRES_12BIT_32S_17MS =
      (0x0680), // 32 x 12-bit bus samples averaged together
  INA219_CONFIG_BADCRES_12BIT_64S_34MS =
      (0x0700), // 64 x 12-bit bus samples averaged together
  INA219_CONFIG_BADCRES_12BIT_128S_69MS =
      (0x0780), // 128 x 12-bit bus samples averaged together

};

/** values for shunt ADC resolution **/
enum {
  INA219_CONFIG_SADCRES_9BIT_1S_84US = (0x0000),   // 1 x 9-bit shunt sample
  INA219_CONFIG_SADCRES_10BIT_1S_148US = (0x0008), // 1 x 10-bit shunt sample
  INA219_CONFIG_SADCRES_11BIT_1S_276US = (0x0010), // 1 x 11-bit shunt sample
  INA219_CONFIG_SADCRES_12BIT_1S_532US = (0x0018), // 1 x 12-bit shunt sample
  INA219_CONFIG_SADCRES_12BIT_2S_1060US =
      (0x0048), // 2 x 12-bit shunt samples averaged together
  INA219_CONFIG_SADCRES_12BIT_4S_2130US =
      (0x0050), // 4 x 12-bit shunt samples averaged together
  INA219_CONFIG_SADCRES_12BIT_8S_4260US =
      (0x0058), // 8 x 12-bit shunt samples averaged together
  INA219_CONFIG_SADCRES_12BIT_16S_8510US =
      (0x0060), // 16 x 12-bit shunt samples averaged together
  INA219_CONFIG_SADCRES_12BIT_32S_17MS =
      (0x0068), // 32 x 12-bit shunt samples averaged together
  INA219_CONFIG_SADCRES_12BIT_64S_34MS =
      (0x0070), // 64 x 12-bit shunt samples averaged together
  INA219_CONFIG_SADCRES_12BIT_128S_69MS =
      (0x0078), // 128 x 12-bit shunt samples averaged together
};
https://github.com/adafruit/Adafruit_IN ... t_INA219.h

User avatar
AyubowanPro
 
Posts: 12
Joined: Wed Jul 27, 2022 6:18 am

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by AyubowanPro »

Thank you for your reply. I will try to tweak these parameters.

User avatar
AyubowanPro
 
Posts: 12
Joined: Wed Jul 27, 2022 6:18 am

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by AyubowanPro »

I tried to define at the start of my sketch :
#define INA219_CONFIG_BADCRES_MASK (INA219_CONFIG_BADCRES_12BIT)
#define INA219_CONFIG_SADCRES_MASK (INA219_CONFIG_SADCRES_12BIT_1S_532US)

In order to have just one 12 bit sample taken, but there is still 2ms between each value.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by mikeysklar »

It look like you chose the correct values for a single sample.

Was the 2ms delay what you were seeing initially with the 32 sample default values or is this faster?

Do you need to use 12-bit sampling?

Does hardcoding the values change the behavior?

Code: Select all

#define INA219_CONFIG_BADCRES_MASK (0x0180)
#define INA219_CONFIG_SADCRES_MASK (0x0018)
If the above has no effect does changing the values in the library header cause a change?

User avatar
AyubowanPro
 
Posts: 12
Joined: Wed Jul 27, 2022 6:18 am

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by AyubowanPro »

mikeysklar wrote:Was the 2ms delay what you were seeing initially with the 32 sample default values or is this faster?

Do you need to use 12-bit sampling?

Does hardcoding the values change the behavior?
By using "micros()" before and after using "ina219.getPower_mW()", I measured 2.5ms with 128x12bits and the exact same 2.5ms with 1x12bit.

I don't think I need 12 bits but I need a decent resolution.

I tried to overwrite the two values by using these lines but there is still 2.5ms delay.

Code: Select all

#define INA219_CONFIG_BADCRES_MASK (0x0180)
#define INA219_CONFIG_SADCRES_MASK (0x0018)
I tried also by modifying the header file directly but I got the same results.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by mikeysklar »

It is really odd to me that the overriding values you are using "BADCRES" and "SADCRES" are not having an effect in the code or header.

Just to confirm are you recompiling the code after each modification (not just reuploading the same old HEX file)?

User avatar
AyubowanPro
 
Posts: 12
Joined: Wed Jul 27, 2022 6:18 am

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by AyubowanPro »

Yes, I recompile the code each time

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by mikeysklar »

Here are few more options:

1) Try Setting the i2c bus speed to fast mode plus 1MHz

Code: Select all

Wire.setClock(1000000)
or the max INA219 i2c speed:

Code: Select all

Wire.setClock(2560000)
https://www.arduino.cc/en/Reference/WireSetClock

2) Consider using a INA169 which should be faster.

https://www.adafruit.com/product/1164

3) Post you code so we can make sure there is not serial logging or other logging type activities that would slow things down.

User avatar
AyubowanPro
 
Posts: 12
Joined: Wed Jul 27, 2022 6:18 am

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by AyubowanPro »

Hello,

I tried with Wire.setClock(2560000) but the results are the same.

Here is my test code:

Code: Select all

#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

#define INA219_CONFIG_BADCRES_MASK (0x0180)
#define INA219_CONFIG_SADCRES_MASK (0x0018)

float power_mW = 0;

void setup(void) 
{ 
  Serial.begin(115200);
  Wire.setClock(2560000);

  uint32_t currentFrequency;
  
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  if (! ina219.begin()) {
    Serial.println("Failed to find INA219 chip");
    while (1) { delay(10); }
  }
  // To use a slightly lower 32V, 1A range (higher precision on amps):
  //ina219.setCalibration_32V_1A();
  // Or to use a lower 16V, 400mA range (higher precision on volts and amps):
  ina219.setCalibration_16V_400mA();

  Serial.println("Measuring voltage and current with INA219 ...");
}

void loop(void) 
{
  Serial.println(micros());
  power_mW = ina219.getPower_mW();
  Serial.println(micros());
}

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by mikeysklar »

On the TI forums I see admins suggesting 9-bit mode set for continuous conversion for the fastest response (84us). Checking the Adafruit library the INA219 is by default in shut and bus voltage continuous mode so it would just be a matter of switching to 9-bit sample rate which is all zero'd out for the BADCRES and SADCRES.

Code: Select all

 INA219_CONFIG_BADCRES_9BIT = (0x0000),  // 9-bit bus res = 0..511
INA219_CONFIG_SADCRES_9BIT_1S_84US = (0x0000),   // 1 x 9-bit shunt sample

https://e2e.ti.com/support/amplifiers-g ... ent-sensor
Screen Shot 2022-08-03 at 1.44.27 PM.png
Screen Shot 2022-08-03 at 1.44.27 PM.png (93.61 KiB) Viewed 100 times

Code: Select all

/** mask for operating mode bits **/
#define INA219_CONFIG_MODE_MASK (0x0007) // Operating Mode Mask

/** values for operating mode **/
enum {
  INA219_CONFIG_MODE_POWERDOWN = 0x00,       /**< power down */
  INA219_CONFIG_MODE_SVOLT_TRIGGERED = 0x01, /**< shunt voltage triggered */
  INA219_CONFIG_MODE_BVOLT_TRIGGERED = 0x02, /**< bus voltage triggered */
  INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED =
      0x03,                         /**< shunt and bus voltage triggered */
  INA219_CONFIG_MODE_ADCOFF = 0x04, /**< ADC off */
  INA219_CONFIG_MODE_SVOLT_CONTINUOUS = 0x05, /**< shunt voltage continuous */
  INA219_CONFIG_MODE_BVOLT_CONTINUOUS = 0x06, /**< bus voltage continuous */
  INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS =
      0x07, /**< shunt and bus voltage continuous */
};
https://github.com/adafruit/Adafruit_IN ... 219.h#L129

User avatar
AyubowanPro
 
Posts: 12
Joined: Wed Jul 27, 2022 6:18 am

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by AyubowanPro »

So, you advise me to use #define INA219_CONFIG_MODE_MASK (0x0007) right ? But it is the default value ?

The thing is, whatever I set with "#define INA219_CONFIG_BADCRES_MASK (0x0180)" and "#define INA219_CONFIG_SADCRES_MASK (0x0018)", there is still 2.5ms delay between each measurements.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Power profiler with INA219 and ESP32-S2 TFT Feather

Post by mikeysklar »

Actually I had suggested that both be set to 9-bit.

Code: Select all

INA219_CONFIG_BADCRES_9BIT = (0x0000),  // 9-bit bus res = 0..511
INA219_CONFIG_SADCRES_9BIT_1S_84US = (0x0000),   // 1 x 9-bit shunt sample
In the user code and header since I'm not sure if one is overriding the other.

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”