read line by line float datas in SD Card

Hello everybody.

I have a .txt with many lines with something like this in a SD card:

36.5, 34.6, -2.3, 117.5
22.0, 12.4, 1.3, 290.1
.all lines has the same 4 float variables

I need to read all 4 variables of each line, after that to jump to the next line and to do the same using the same 4 variables to read.

Somebody has one sample with the same characteristics ? I really appreciate this.

Have a look at parseFloat() which reads the next float of a stream. If you know there are always 4, just do 4 successive myfile.parseFloat() into 4 variables.

(That link shows parseFloat() as being in Serial, but it works for the SD library too.)

The code below compiles but is not tested: can't find my SD card module right now.

Set your file name in the line marked ///<<<<<<<<<<<<<<<<<<<<<<<

It reads 4x floats into variables and prints them to the monitor. Then if there's more data it delay()s for a second then does the next line (edit: or more correctly the next 4 floats, regardless of lines), or else it ends.

(This test code is all in setup(). In real life you would probably want to move it into loop() and make it delay()-less.)

You didn't say what you want to do with the data, so you'll have to add that part yourself.


// parse 4x floats out of an SD card file
// meltDown 12 august 2019

  BASED ON SD card file dump

  This example shows how to read a file from the SD card using the
  SD library and send it over the serial port.

  The circuit:
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

  created  22 December 2010
  by Limor Fried
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.


#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;

int myVar1;
int myVar2;
int myVar3;
int myVar4;

void setup()
  Serial.println(".... parse floats out of sd file ....");
  Serial.print("Compiler: ");
  Serial.print(", Arduino IDE: ");
  Serial.print("Created: ");
  Serial.print(", ");

  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:
  Serial.println("card initialized.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile ="theDataFile.txt"); //<<<<<<<<<<<<<<<<<<<<<< set file name here

  // if the file is available, read from it:
  if (dataFile)
    while (dataFile.available())
      //parse the next 4 values out of the file:
      myVar1 = dataFile.parseFloat();
      myVar2 = dataFile.parseFloat();
      myVar3 = dataFile.parseFloat();
      myVar4 = dataFile.parseFloat();
      //print them to the monitor:

      //go back to the top for the next line...
      //or ....
    }//while data available
    Serial.println(".... no more data");
  }// if datafile
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening the data file");

void loop()

Errata: these should be float of course...

int myVar1;
int myVar2;
int myVar3;
int myVar4;

Fusion de un reactor.

I ´ll study your code. Really I appreciate your effort to help me.

thank you !.

Fusion de un reactor.


Really I appreciate your effort to help me.

thank you !.

You're welcome: If I can find my SD card module in my "filing system" I will test the code.

It works btw, with the fix from #3 of course.

Here's the data in notepad:


Here's the output:

.... parse floats out of sd file ....
Compiler: 4.9.2, Arduino IDE: 10805
Created: 10:44:35, Aug 13 2019
Initializing SD card...card initialized.
.... no more data