Cannot write to SD card when not connected to PC

I’m a bit stumped.

I’ve checked the blink example code works connected to pc or via plug in USB phone charger (which it does) so code can run on the board without PC…

I’ve removed all links to the serial link / print

I’ve had one write where it mis-wrote the file name 3 times by including a box shape instead of the required character - that made me think power supply may be a problem but I tried 3 different plug in USB chargers - none work including a branded 5v 2A supply?

I will try to see how far the code gets by switching a PIN high (I can’t use the built in LED unfortunately as pin 13 is used by the sd card reader.

I’ll see if I can find an external power source, what else should I try/check?

Code is below:

#include “HX711.h”
#include <SPI.h>
#include <SD.h>
/*

*/

float calibration_factor1 = 310; // this calibration factor is adjusted according to my load cell
float calibration_factor2 = 308;
float unit1;
float unit2;
float zero1;
float zero2;
float diff;
const int chipSelect = 4;

void setup() {
/*Serial.begin(9600);
//while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB port only
// }
//Serial.print(“Initializing SD card…”);
*/

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
// Serial.println(“Card failed, or not present”);
// don’t do anything more:
while (1);
}

// Serial.println(“card initialized.”);

// Get a reading to zero the scale
HX711 scale1(3,2);
scale1.set_scale(calibration_factor1); //Adjust to this calibration factor
scale1.get_units(5);
zero1 = scale1.get_units(10);

HX711 scale2(5,6);
scale2.set_scale(calibration_factor2); //Adjust to this calibration factor
scale2.get_units(5);
zero2 = scale2.get_units(10);
}

void loop() {

HX711 scale1(3,2);
scale1.set_scale(calibration_factor1); //Adjust to this calibration factor
unit1 = scale1.get_units() -zero1;

HX711 scale2(5,6);
scale2.set_scale(calibration_factor2); //Adjust to this calibration factor
unit2 = scale2.get_units() -zero2;

diff = unit1 - unit2;

if (unit2 + unit1 > 100) {
/*

Serial.print("Recording Sensor 1: “);
Serial.print(unit1);
Serial.print(” Sensor 2: “);
Serial.print(unit2);
Serial.print(” Diff: ");
Serial.print(diff);
Serial.println();
*/
File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.print(“Sens1 Sens 2 Diff ,”);
dataFile.print(unit1);
dataFile.print(",");
dataFile.print(unit2);
dataFile.print(",");
dataFile.print(diff);
dataFile.println();
dataFile.close();
// print to the serial port too:

}
// if the file isn’t open, pop up an error:
else {
// Serial.println(“error opening datalog.txt”);
}}

else{
/*
Serial.print("Sensor 1: “);
Serial.print(unit1);
Serial.print(” Sensor 2: “);
Serial.print(unit2);
Serial.print(” Diff: ");
Serial.print(diff);
Serial.println();
*/
}
}

OK, just got an external power supply connected and it appears to run nicely with that. Not sure why it didn't run off any of the 3 plug in USB phone charger supplies, maybe they are too noisy or something?

HX711 scale1(3,2);

These should be global scope, not recreated every time around loop.

That's not your current problem, but still something you should fix.