Tutorial IX: INA219 Voltage And Current Monitoring Demo

From Waveshare Wiki
Jump to: navigation, search

Modules Usage Tutorial

INA219 Voltage And Current Detection

The General Driver for Robots has an onboard INA219 module, which can detect the power supply voltage and current of the driver board. The monitoring demo of the INA219 is provided below.

Demo

Upload Demo

Download the dependency library INA219_WE.
UGV01 tutorial VII01EN.png
After downloading the zip package, open INA219.ino, connect the multifunctional driver board to the computer with a USB cable (here the Type-C port of the USB of the multifunctional driver board is inserted), click "Tools" → "Ports", and then click the new COM (COM26 in my case) that appears. Click "Tools" → "Ports", and then click the new COM port.
UGV1 doenload03EN.png
In Arduino IDE, click "Tools" → "Development Board" → "ESP32" → "ESP32 Dev Module". Upload the demo after selecting the board and the port. After uploading the demo, open the serial port monitor of Arduino IDE to monitor the power voltage and current. UGV01 tutorial II01.png Tutorial VII03.png

Demo Analysis

// <<<<<<<<<<=========INA219: 0x42===========>>>>>>>>>>
#define S_SDA 32    //Define SDA pin
#define S_SCL 33    //Define SCL pin
#include <INA219_WE.h>    //Import INA219_WE library
#include <Wire.h>    //Import Wire library for I2C communication  
#define INA219_ADDRESS 0x42    //Define INA219 module address
INA219_WE ina219 = INA219_WE(INA219_ADDRESS);

float shuntVoltage_mV = 0.0;
float loadVoltage_V = 0.0;    //Define the load voltage 
float busVoltage_V = 0.0;    //Define the serial bus voltage 
float current_mA = 0.0;    //Define current
float power_mW = 0.0;      //Define power 
bool ina219_overflow = false;

void InitINA219(){
  if(!ina219.init()){
    //Initialize INA219 module
    Serial.println("INA219 not connected!");
  }
  ina219.setADCMode(BIT_MODE_9);
  ina219.setPGain(PG_320);
  ina219.setBusRange(BRNG_16);
  ina219.setShuntSizeInOhms(0.01); // used in INA219.
}

void InaDataUpdate(){
  shuntVoltage_mV = ina219.getShuntVoltage_mV();
  busVoltage_V = ina219.getBusVoltage_V();    //Get serial bus voltage  
  current_mA = ina219.getCurrent_mA();    //Get current
  power_mW = ina219.getBusPower();    //Get power
  loadVoltage_V  = busVoltage_V + (shuntVoltage_mV/1000);
  ina219_overflow = ina219.getOverflow();
}

void allDataUpdate(){
    Serial.print("battery:");
    Serial.println(loadVoltage_V);
    Serial.print("current_mA:");
    Serial.println(current_mA);
}

void setup() {
  Wire.begin(S_SDA,S_SCL);
  Serial.begin(115200);    //Initial serial communication
  while(!Serial){}    //Wait for serial port connection
  InitINA219();    //Initialize the INA219 module
}

void loop() {
  InaDataUpdate();    //Update INA219 data
  allDataUpdate();    //Output data
  delay(1000);
}

Resource