You'll have to excuse my stupidity as I am just a hobbyist with minimal experience and I tried my best to search for an answer on here but it seems nobody is having a similar issue as I. I am building a logging device using the low-side function of the INA260. The purpose is to log parasitic draw on car batteries over long periods of time (days to weeks) on an SD Card and have the capability to real-time monitor using an LCD display. For the most part, it seems to be working pretty well, but I am running into an issue where I am getting random characters in both the log and when monitoring it via the serial monitor. I poster my code below, some screenshots of the issue, and would appreciate anybody's input or assistance. I am not an engineer and this is most likely a simple fix, so I apologize in advance.
FYI- For the screenshots below I was not connected to bus voltage when taking amperage samples. Could that have something to do with it?
Code: Select all
Code: Select all
#include <hd44780.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_INA260.h>
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
File myFile;
Adafruit_INA260 ina260 = Adafruit_INA260();
void setup()
{
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1,0);
Serial.begin(115200);
// Wait until serial port is opened
while (!Serial) { delay(10); }
Serial.println("Adafruit INA260 Test");
if (!ina260.begin()) {
Serial.println("Couldn't find INA260 chip");
while (1);
}
Serial.println("Found INA260 chip"); // Confirmation INA260 commmunicates
while (!SD.begin(10)) {
Serial.println("STARTUP SD Card Initialization FAILED!");
}
Serial.println("STARTUP initialization SUCCESS.");
File logFile = SD.open("LOG.csv", FILE_WRITE);
if (logFile) {
logFile.println(", ,");
String header="Time(ms),Current(ma),Voltage(mv)"; // List the Header
logFile.println(header);
logFile.close();
Serial.println(header);
}
else {
Serial.println("Couldn't read");
}
}
void loop()
{
long timestamp = millis(); //Time Stamp Callout
// SD CARD FUNCTION
while (!SD.begin(10)) {
Serial.println("SD CARD NO LONGER READABLE!");
// Send it to serial
}
myFile = SD.open("AmpLog.txt", FILE_WRITE);
String dataString = String(timestamp) + "," + String(ina260.readCurrent()) + "," + String(ina260.readBusVoltage());
File logFile = SD.open("LOG.csv", FILE_WRITE);
if (logFile) {
logFile.println(dataString);
logFile.close();
Serial.println(dataString);
}
else {
Serial.println("Couldn't open log file");
}
timestamp++;
// LCD OPERTATION Turn on the blacklight and print a message.
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("(mv): ");
lcd.print((String)ina260.readBusVoltage());
lcd.setCursor(0, 1);
lcd.print("(ma): ");
lcd.print((String)ina260.readCurrent());
lcd.setCursor(0, 1);
delay(1000);
}