How to write into a SD card with new line each time using a voltage sensor and a sd card module?

Right now i am able to write into the card. However , when i open the text file, it shows that the information written was written in one long line. I want it to go to the next line after each written data so that i am able to copy and paste into an excel file. Thanks!

#include <SD.h>
#include <SPI.h>

File myFile;
int csPin = 7;

const int voltageinputPIN = A4; //select analog input pin for voltage sensor
const int baudRate = 9600; //sets baud rate in bits per second for serial monitor
const int sensorreadDelay = 2500; //sensor read delay in milliseconds
const int maxanalogValue = 1023; //highest integer given at max input voltage
const int sensormaxVoltage = 25; //highest input voltage of sensor being used

float analogVoltage = 0; //to store voltage value at analog pin

void setup() //setup routine runs once when reset or turned on
  Serial.begin(baudRate); //initializes serial communication
  pinMode(csPin, OUTPUT);

  // SD Card Initialization
  if (SD.begin())
    Serial.println("SD card is ready to use.");
    Serial.println("SD card initialization failed");

void loop() //loop routine runs over and over again forever
  analogVoltage = analogRead(voltageinputPIN); 
  analogVoltage = (analogVoltage/maxanalogValue)*sensormaxVoltage; //conversion equation
  Serial.print(analogVoltage); //prints value to serial monitor
  Serial.println("V"); //prints label

  myFile ="test.txt", FILE_WRITE);
  if (myFile)
    myFile.close(); // close the file
  // if the file didn't open, print an error:

    Serial.println("error opening test.txt");
  delay(sensorreadDelay); //delay in milliseconds between read values

After this do a

Thanks! i will try it now

seems to work now. thanks!

Why not use

you are just printing the float value as opposed to printing a string.

I would recomend using dtostrf() which is explained here

for your code, just before myFile ="test.txt", FILE_WRITE); use:

  char myStringToWrite[20];  //You can declare this globally at the top if you prefer
  dtostrf(analogVoltage, 7, 3, myStringToWrite);
  strcat(myStringToWrite, "\n");

and then, instead of myFile.print(analogVoltage); use myFile.print(myStringToWrite);

Also, just as a bit extra, I would recommend using SdFat.h as opposed to SD.h as it is a more recent, quicker and less buggy version of SD.h as explained here

I just realised that you can do println() after I posted haha

I do not see what difference that would make. print is quite capable of printing a float as ASCII characters, and using a string does not automatically insert the line feed so does not provide a solution to the OP's question.

Yes, why make it simple when there is a much more complicated way of doing it. That is the way to teach beginners.

1 Like

hahaha yes, I am a beginner in all these. Still much to learn for me :pensive:

No shame in having stuff to learn, we all have stuff we don’t know. That is the fun about this subject. Even if you think you know it all it constantly changes and you will always have to learn new stuff.