Hello guys, I am new here !!!
Im trying to build me a DIY Powermeter, with the INA219, a 9.96" Oled display and a SD card reader (8gb Fat32 micro SD-card). I use the Arduino NANO.
I have the problem, that I can not write the informations (Time, current and voltage) on the sd card. sometimes it works but mostly it doesn´t. extra information: on the SD card should be 3 txt files (TIME.txt, VOLT.txt, CUR.txt), in every file should be written the appropriate value.
I dont know why, i know that there are allready a few of this projects on google but i can´t fix my problem till now.
I have also already checked if my SD-card-reader is working and my is SD-card is compatible, it seems like it is working fine. I checkt it with the Arduino examplecode "Cardinfo".
I hope some of you can help me.
Im sorry if something like this was already postet here but im new on reddit and can´t find something.
Thank you guys
//Powermeter by keppioo5
//===================================Libaries====================================
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include "SdFat.h"
#define OLED_RESET 4
//===========================Deklaration & Initialisation========================
Adafruit_SSD1306 display(OLED_RESET);
Adafruit_INA219 ina219;
SdFat SD;
unsigned long previousMillis = 0;
unsigned long interval = 100;
const int chipSelect = 10;
float current_mA = 0;
float voltage = 0;
float energy = 0;
File TimeFile;
File VoltFile;
File CurFile;
//==============================================================================
void setup() {
SD.begin(chipSelect); //SD begin
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Display begin
ina219.begin(); // Current-sensor "INA219" begin
}
//==============================================================================
void loop() {
unsigned long currentMillis = millis(); //ms counter
if (currentMillis - previousMillis >= interval)
/*waits 100ms, that means that every 100ms the values will be refreshed on the
display and the "new" values of time, current and voltage will be written on the sd card.*/
{
previousMillis = currentMillis;
ina219values(); //get Values
TimeFile = SD.open("TIME.txt", FILE_WRITE); //open SD file: TIME.txt
if (TimeFile) {
TimeFile.println(currentMillis); //writes the time in ms into the file
TimeFile.close(); // close file
}
VoltFile = SD.open("VOLT.txt", FILE_WRITE); //open SD file: VOLT.txt
if (VoltFile) {
VoltFile.println(voltage); //writes the voltage value into the file
VoltFile.close(); //close file
}
CurFile = SD.open("CUR.txt", FILE_WRITE); //open SD file: CUR.txt
if (CurFile) {
CurFile.println(current_mA); //writes the current value into the file
CurFile.close(); //close file
}
displaydata(); // opens the desplaydata methode
}
}
//==============================================================================
void displaydata() {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println(voltage);
display.setCursor(35, 0);
display.println("V");
display.setCursor(50, 0);
display.println(current_mA);
display.setCursor(95, 0);
display.println("mA");
display.setCursor(0, 10);
display.println(voltage * current_mA);
display.setCursor(65, 10);
display.println("mW");
display.setCursor(0, 20);
display.println(energy);
display.setCursor(65, 20);
display.println("mWh");
display.display();
}
//===============================================================================
void ina219values() { // Current-sensor methode
current_mA = ina219.getCurrent_mA(); //Current measurement
voltage = (current_mA/1000)* 6.1; // calculate the voltage with current * //resistor, here the total resistance is 6.1ohm
energy = energy + (voltage)* current_mA /3600; // calculating the energy
}