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 S_SCL 33
#include <INA219_WE.h>
#include <Wire.h>
#define INA219_ADDRESS 0x42
INA219_WE ina219 = INA219_WE(INA219_ADDRESS);

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; 
bool ina219_overflow = false;

void InitINA219(){
  if(!ina219.init()){
    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();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getBusPower();
  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);
  while(!Serial){}
  InitINA219();
}

void loop() {
  InaDataUpdate();
  allDataUpdate();
  delay(1000);
}

Resource