Skip to content

Instantly share code, notes, and snippets.

@Craigson
Created May 11, 2015 17:14
Show Gist options
  • Select an option

  • Save Craigson/99e2cc80e9bbe76a7a1f to your computer and use it in GitHub Desktop.

Select an option

Save Craigson/99e2cc80e9bbe76a7a1f to your computer and use it in GitHub Desktop.
Arduino Test Code for INA3221 on SunAirPlus board (in CSV format)
//
// SDL_Arduino_INA3221 Library Test Code
// SDL_Arduino_INA3221.cpp Arduino code - runs in continuous mode
// Version 1.1
// SwitchDoc Labs January 31, 2015
//
//
// This was designed for SunAirPlus - Solar Power Controller - www.switchdoc.com
//
#include <Wire.h>
#include <SDL_Arduino_INA3221.h>
SDL_Arduino_INA3221 ina3221;
// the three channels of the INA3221 named for SunAirPlus Solar Power Controller channels (www.switchdoc.com)
#define LIPO_BATTERY_CHANNEL 1
#define SOLAR_CELL_CHANNEL 2
#define OUTPUT_CHANNEL 3
void setup(void)
{
Serial.begin(9600);
Serial.println("SDA_Arduino_INA3221_Test");
Serial.println("Measuring voltage and current with ina3221 ...");
ina3221.begin();
}
void loop(void)
{
float shuntvoltage1 = 0;
float busvoltage1 = 0;
float current_mA1 = 0;
float loadvoltage1 = 0;
busvoltage1 = ina3221.getBusVoltage_V(LIPO_BATTERY_CHANNEL);
shuntvoltage1 = ina3221.getShuntVoltage_mV(LIPO_BATTERY_CHANNEL);
current_mA1 = -ina3221.getCurrent_mA(LIPO_BATTERY_CHANNEL); // minus is to get the "sense" right. - means the battery is charging, + that it is discharging
loadvoltage1 = busvoltage1 + (shuntvoltage1 / 1000);
float shuntvoltage2 = 0;
float busvoltage2 = 0;
float current_mA2 = 0;
float loadvoltage2 = 0;
busvoltage2 = ina3221.getBusVoltage_V(SOLAR_CELL_CHANNEL);
shuntvoltage2 = ina3221.getShuntVoltage_mV(SOLAR_CELL_CHANNEL);
current_mA2 = -ina3221.getCurrent_mA(SOLAR_CELL_CHANNEL);
loadvoltage2 = busvoltage2 + (shuntvoltage2 / 1000);
float shuntvoltage3 = 0;
float busvoltage3 = 0;
float current_mA3 = 0;
float loadvoltage3 = 0;
busvoltage3 = ina3221.getBusVoltage_V(OUTPUT_CHANNEL);
shuntvoltage3 = ina3221.getShuntVoltage_mV(OUTPUT_CHANNEL);
current_mA3 = ina3221.getCurrent_mA(OUTPUT_CHANNEL);
loadvoltage3 = busvoltage3 + (shuntvoltage3 / 1000);
Serial.print(busvoltage1); Serial.print(",");
Serial.print(shuntvoltage1); Serial.print(",");
Serial.print(loadvoltage1); Serial.print(",");
Serial.print(current_mA1); Serial.print(",");
Serial.print(busvoltage2); Serial.print(",");
Serial.print(shuntvoltage2); Serial.print(",");
Serial.print(loadvoltage2); Serial.print(",");
Serial.print(current_mA2); Serial.print(",");
Serial.print(busvoltage3); Serial.print(",");
Serial.print(shuntvoltage3); Serial.print(",");
Serial.print(loadvoltage3); Serial.print(",");
Serial.print(current_mA3); Serial.print(",");
Serial.println("");
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment