INA260 Random Characters in SD Card Log

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
aber8552
 
Posts: 1
Joined: Wed Dec 01, 2021 5:15 pm

INA260 Random Characters in SD Card Log

Post by aber8552 »

Hello All,

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? Thank you so much for the help!!

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);
}
Attachments
INA260 log issue 4.PNG
INA260 log issue 4.PNG (22.14 KiB) Viewed 138 times
INA260 log issue.PNG
INA260 log issue.PNG (17.22 KiB) Viewed 138 times
INA260 log issue 2.PNG
INA260 log issue 2.PNG (15.63 KiB) Viewed 138 times
Last edited by dastels on Wed Dec 01, 2021 6:46 pm, edited 2 times in total.
Reason: Add code block and format code for readability

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: INA260 Random Characters in SD Card Log

Post by dastels »

Have you tried constructing the datastring differently? I.e. not using String conversion and concatenation. Maybe use sprintf?

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”