How can print maximum and minimum current reading from INA 219 using Arduino Uno

Hi ,

In my project I want to know the maximum and minimum current drawn by a gadget. I am using INA219 and Arduino Uno. Please support me with valuable feedback.

@knshanil, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

  1. Keep two variables, one for the minimum and one for the maximum. Set maximum to 0 and minimum to e.g. 9999.
  2. Read the INA219 (no idea how, no experience with it).
  3. Compare the reading with minimum, update minimum if reading is lower.
  4. Compare the reading with maximum, update maximum if reading is higher.
  5. Back to (2).

Thanks for the feedback. I am very new to Arduino , just now learning the steps.
The project was to make a tester to measure the current drawing by a gadget ( hall effect sensor). I want to display both min and max current during its operation.

Determine a period over which the maximum and minimum will be observed. E.g. 10 seconds.
When starting measurements, let the Arduino note the time (e.g. store output of millis() in an unsigned long variable).
Then perform measurements in a loop. How many measurements per second you can do (sensibly) depends on the measurement resolution you require, but mostly on the time it takes to query the sensor over I2C and get its readings. Let's say you can achieve something like a 500 samples per second (assuming a ca. 2ms I2C 'blackout' time).
For each measurement, compare the value with the minimum value you store in a variable. If the measured value is lower than the one in your variable, replace the value in the variable with it.
Do the same for the maximum.
At the end of the measurement cycle (or perhaps even intermittently during it) output the maximum and minimum values (which you get from their respective variables) in your preferred way, e.g. over Serial.

In pseudo-code, it would look something like this (note: this won't compile as it's not proper code, but a shorthand description; e.g. I left out all the code to initiate the Serial connection and to start Wire etc.):

int max_reading, min_reading;
unsigned long start_time;
bool values_printed = false;
#define RUN_TIME 10000 //Total sample loop time in milliseconds
INA219 sensor;

void setup ()
{
  sensor.init();
  min_reading = 65535;  //This high initial value ensures the value will be replaced more or less immediately with a lower one
  max_reading = 0; //Similar to above, but the other way around
  Serial.print(F("Starting measurements...please hold"));
  start_time = millis();
}

void loop ()
{
  if (millis() - start_time > RUN_TIME)  //Are we already done sampling? Then don't read any more values from the sensor
  {
    if (!values_printed)  //If we haven't printed the outcome to the Serial port, we should do so...
    {
      Serial.print(F("Minimum: "));
      Serial.print(min_reading);
      Serial.print(F(" maximum: "));
      Serial.println(max_reading);
      values_printed = true;  //...but only print the values once
    }
    else return; //This takes us back to the start of loop() - if we get here, we'll keep looping forever without doing anything. Reset the Arduino to perform the process again. You could also write "while(1);" here to the same effect. Or replace this with code where you read a button that will re-start the measurement process.
  }
  //If we make it this far, we're actually in the measurement loop and we should take samples
  int value = sensor.take_reading();
  if (value < min_reading) min_reading = value;  //Is the most recent measurement lower than the minimum value we've stored? Then we should keep it!
  if (value > max_reading) max_reading = value; //Same as above for max_value, but the other way around
  //From here the Arduino will automatically return to the start of loop() and redo the measurement until we reach the 10-second limit we defined all the way at the top.
}

Like I said, the above won't work the way I wrote it, but you can quite easily flesh it out into working code. You have to include the Wire library and initialize it, initialize the Serial connection and do the appropriate work to set up the sensor and take measurements from it. I didn't check the library you use for its proper function names; they're almost certainly different from what I wrote above.

So the example only intends to show a quite minimal/basic way of doing what you asked. There's many ways in which you can make it more fancy/functional, but this would already do something.

1 Like

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