Power monitor and serial plotter

I am using INA226 as a power monitor: INA226 Current and Power Sensor • Wolles Elektronikkiste

Now I want to use Serial Plotter to show Shunt Voltage [mV] -value.

Code can be found below. Could someone help me, please? I have tried but Plotter does not say a thing.

> /***************************************************************************
> * Example sketch for the INA226_WE library
> *
> * This sketch shows how to use the INA226 module in continuous mode. 
> *  
> * Further information can be found on:
> * https://wolles-elektronikkiste.de/ina226 (German)
> * https://wolles-elektronikkiste.de/en/ina226-current-and-power-sensor (English)
> * 
> ***************************************************************************/
> #include <Wire.h>
> #include <INA226_WE.h>
> #define I2C_ADDRESS 0x40
> 
> /* There are several ways to create your INA226 object:
>  * INA226_WE ina226 = INA226_WE(); -> uses I2C Address = 0x40 / Wire
>  * INA226_WE ina226 = INA226_WE(I2C_ADDRESS);   
>  * INA226_WE ina226 = INA226_WE(&Wire); -> uses I2C_ADDRESS = 0x40, pass any Wire Object
>  * INA226_WE ina226 = INA226_WE(&Wire, I2C_ADDRESS); 
>  */
> INA226_WE ina226 = INA226_WE(I2C_ADDRESS);
> 
> void setup() {
>   Serial.begin(9600);
>   Wire.begin();
>   ina226.init();
> 
>   /* Set Number of measurements for shunt and bus voltage which shall be averaged
>   * Mode *     * Number of samples *
>   AVERAGE_1            1 (default)
>   AVERAGE_4            4
>   AVERAGE_16          16
>   AVERAGE_64          64
>   AVERAGE_128        128
>   AVERAGE_256        256
>   AVERAGE_512        512
>   AVERAGE_1024      1024
>   */
>   //ina226.setAverage(AVERAGE_16); // choose mode and uncomment for change of default
> 
>   /* Set conversion time in microseconds
>      One set of shunt and bus voltage conversion will take: 
>      number of samples to be averaged x conversion time x 2
>      
>      * Mode *         * conversion time *
>      CONV_TIME_140          140 µs
>      CONV_TIME_204          204 µs
>      CONV_TIME_332          332 µs
>      CONV_TIME_588          588 µs
>      CONV_TIME_1100         1.1 ms (default)
>      CONV_TIME_2116       2.116 ms
>      CONV_TIME_4156       4.156 ms
>      CONV_TIME_8244       8.244 ms  
>   */
>   //ina226.setConversionTime(CONV_TIME_1100); //choose conversion time and uncomment for change of default
>   
>   /* Set measure mode
>   POWER_DOWN - INA226 switched off
>   TRIGGERED  - measurement on demand
>   CONTINUOUS  - continuous measurements (default)
>   */
>   //ina226.setMeasureMode(CONTINUOUS); // choose mode and uncomment for change of default
>   
>   /* Set Current Range
>     * Mode *   * Max Current *
>      MA_400          400 mA
>      MA_800          800 mA (default)
>   */
>   //ina226.setCurrentRange(MA_800); // choose gain and uncomment for change of default
>   
>   /* If the current values delivered by the INA226 differ by a constant factor
>      from values obtained with calibrated equipment you can define a correction factor.
>      Correction factor = current delivered from calibrated equipment / current delivered by INA226
>   */
>   // ina226.setCorrectionFactor(0.95);
>   
>   Serial.println("INA226 Current Sensor Example Sketch - Continuous");
>   
>   ina226.waitUntilConversionCompleted(); //if you comment this line the first data might be zero
> }
> 
> void loop() {
>   float shuntVoltage_mV = 0.0;
>   float loadVoltage_V = 0.0;
>   float busVoltage_V = 0.0;
>   float current_mA = 0.0;
>   float power_mW = 0.0; 
> 
>   ina226.readAndClearFlags();
>   shuntVoltage_mV = ina226.getShuntVoltage_mV();
>   busVoltage_V = ina226.getBusVoltage_V();
>   current_mA = ina226.getCurrent_mA();
>   power_mW = ina226.getBusPower();
>   loadVoltage_V  = busVoltage_V + (shuntVoltage_mV/1000);
>   
>   Serial.print("Shunt Voltage [mV]: "); Serial.println(shuntVoltage_mV);
>   Serial.print("Bus Voltage [V]: "); Serial.println(busVoltage_V);
>   Serial.print("Load Voltage [V]: "); Serial.println(loadVoltage_V);
>   Serial.print("Current[mA]: "); Serial.println(current_mA);
>   Serial.print("Bus Power [mW]: "); Serial.println(power_mW);
>   if(!ina226.overflow){
>     Serial.println("Values OK - no overflow");
>   }
>   else{
>     Serial.println("Overflow! Choose higher current range");
>   }
>   Serial.println();
>   
>   delay(3000);
> }
  • Use the Serial.print(F()) macro to save memory
  • Plot on one line and add delimiters after each value:
Serial.print(F("Shunt [mV]: "));     Serial.print(shuntVoltage_mV); Serial.print(F(", "));
Serial.print(F("Bus [V]: "));        Serial.print(busVoltage_V);    Serial.print(F(", "));
Serial.print(F("Load [V]: "));       Serial.print(loadVoltage_V);   Serial.print(F(", "));
Serial.print(F("Current[mA]: ");     Serial.print(current_mA);      Serial.print(F(", "));
Serial.print(F("Bus Power [mW]: ")); Serial.print(power_mW);        Serial.println();

Comment this out as it may interfere with your plot:

  if(!ina226.overflow){
     //Serial.println("Values OK - no overflow");
   }
   else{
     //Serial.println("Overflow! Choose higher current range");
   }
   //Serial.println();
 }

Thanks! I made the changes but plotter remains the same - no values? Serial monitor works but plotter not.

Just a temporary wokwi example.

  • Click image to view serial monitor.
void setup() {
  Serial.begin(9600);
  Serial.println("Plotter Test");
}

void loop() {
  float shuntVoltage_mV = 1.1;
  float loadVoltage_V = 2.2;
  float busVoltage_V = 3.3;
  float current_mA = 4.4;
  float power_mW = 5.5;

  Serial.print(F("Shunt [mV]: "));     Serial.print(shuntVoltage_mV); Serial.print(F(", "));
  Serial.print(F("Bus [V]: "));        Serial.print(busVoltage_V);    Serial.print(F(", "));
  Serial.print(F("Load [V]: "));       Serial.print(loadVoltage_V);   Serial.print(F(", "));
  Serial.print(F("Current[mA]: "));    Serial.print(current_mA);      Serial.print(F(", "));
  Serial.print(F("Bus Power [mW]: ")); Serial.print(power_mW);        Serial.println();
  delay(3000);
}

I tried that also, did not work?

But finally its working. I edited code like this and plotter seems to work now. I had to remove empty spaces. Weird?

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