#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <INA260.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Define the address of the INA260 based on how it is wired.
// In this case both address pins are wired to ground.
static INA260 ina260(INA260::ADDR_GND, INA260::ADDR_GND);
// declare an SSD1306 display object connected to I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
double batvolt = 100;
void setup() {
Serial.begin(9600);
// Call the begin() function to initialize the instance. This will also
// initialize the Wire/I2C library.
ina260.begin();
// initialize OLED display with address 0x3C for 128x32
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(32, 10); // position to display
oled.println("Solar Powered");
oled.setCursor(34, 20);
oled.println("Battery Bank"); // text to display
oled.display();
delay(3000);
oled.clearDisplay(); // show on OLED
}
void loop() {
double power = 0.0;
double voltage = 0.0;
double current = 0.0;
// Read power, voltage, and current measurements from
// the INA260 and print them to the OLED screen
ina260.readPowerRegisterInWatts(power);
ina260.readBusVoltageRegisterInVolts(voltage);
ina260.readCurrentRegisterInAmps(current);
oled.setCursor(0, 0);
oled.print("Load W: ");
oled.print(power);
oled.setCursor(0, 10);
oled.print("Load V: ");
oled.print(voltage);
oled.setCursor(0, 20);
oled.print("Load I: ");
oled.print(current);
oled.display();
delay(1000);
oled.clearDisplay();
}