Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days. On MLK Day no orders will be shipped.
0

INA260 Random Characters in SD Card Log
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

INA260 Random Characters in SD Card Log

by aber8552 on Wed Dec 01, 2021 5:28 pm

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?
Code: Select all | TOGGLE FULL SIZE


Thank you so much for the help!!

Code: Select all | TOGGLE FULL SIZE
#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 111 times
INA260 log issue.PNG
INA260 log issue.PNG (17.22 KiB) Viewed 111 times
INA260 log issue 2.PNG
INA260 log issue 2.PNG (15.63 KiB) Viewed 111 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

aber8552
 
Posts: 1
Joined: Wed Dec 01, 2021 5:15 pm

Re: INA260 Random Characters in SD Card Log

by dastels on Wed Dec 01, 2021 6:55 pm

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

Dave

dastels
 
Posts: 8399
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.