SD card

Hi, I am planning to use an SD card on one of my projects. I want to write to a text file on the sd card a 8 digit number on the first line depending on the inputs. Then go to the second line and print the next eight digit number, then go to the next line and so on. Then I want to read each digit. At first I want to go to the first line and read the first digit, then read the second digit and so on. At the end of the line, I want to go to the next line and so on. I know the basics of writing and reading data on/off the sd card. But I do not know how to go to a specific line and read each character etc. So please tell me how can i do it.

Its very simple wen look at two examples shown in the libraries which are
data logger and files.

the code that i modified from that is given bellow.

#include <SD.h>

File myFile;

void setup()
   while (!Serial) 

  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(10, OUTPUT);

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
  Serial.println("initialization done.");

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  else {
    Serial.println("example.txt doesn't exist.");

  Serial.println("Creating example.txt...");
  myFile ="example.txt", FILE_WRITE);
  if (myFile) {
    myFile.println("dataString");// in the inverted braces put any string you want..
    // print to the serial port too:
  else {
    Serial.println("error opening datalog.txt");

void loop()
  // nothing happens after setup finishes.

You want lines? The file is just a long row of bytes so you need 1 or more bytes to mark End Of Line. Normally this is ASCII character 10 (0x0A in hex) which is LF or Line Feed or ASCII character 13 (0x0D in hex) which is CR or Carriage Return, or both as 0x0D, 0x0A (think manual typewriter actions). And of course all your digits are ASCII '0' to '9'.

So you might write 8 digits and a LF for each line and expect the same when read to the point of handling error on any difference. World is not perfect, world through wires even more so, always check for errors when logically possible to have one.

You might also use ',' as a separator but then technically you won't have lines then and you won't see lines if you drop the file into an editor. With CRLF both most editors will display lines.

In fact, why not prepare a data sample using your favorite editor and read it with the Arduino to see what it is made of?

Just tell me how the coding part for reading off the second line...

We're not just going to tell you unless you do some research of your own. Make a code based on the samples provided and others that are on the Internet. Look for simple ones that allow you to read and write character(s) onto the SD card. Once you've done that and are still stuck, come back, post your code in full, and then we will try to help you.

I will make two posts about this. This post is just about pushing ahead straight towards the project goal and the next will be about a not-direct, learn the simple first approach. But read this so your sub-conscious can work on it once you take the longer, easier path.

This is the straight-up-the-hill approach:

Try a different view?

This is how it works in a perfect world where incoming data is always right because thinking ahead about errors might only confuse you when you can’t puzzle out what you are doing.

You are getting 8 digit numbers all from 0 to 99,999,999. That range of possible values will take a 32 bit integer, the variable total will need to be type long int,

long int total = 0L; // the L is there to help the compiler know that 0 is to be type long
// I hope you know 16-bit integers from 32-bit long integers? if not, homework reading time!

in setup() you initialize pins and variables, set total=0

loop() goes round and round like a small wheel turning very, very fast

each time it checks for serial available
if no serial byte is ready then let loop() finish, it will run again

if a serial byte is available then read that
if it is a digit then {
total *= 10
total += digit - ‘0’
else if it is an end of line marker {
total is now the value of the number, do whatever you do with your data that you are reading
set total=0 for the next number to start at zero
else if you have reached end of file {
do whatever your program does when the file is finished reading, including
serial print to tell the user (You) it is finished.
make a never-ending while loop to stop the code, it looks like this;

while( 1 ); // this is a never-ending loop, the code just goes around doing nothing


and that is the way, each time through loop() the code in loop() treats everything that -can- happen on a what-now basis. In a not perfect world you have to add if checks for errors that are not shown above – what if a 9th digit is read because the end of line was missed? Or if you get 2 end of line characters or a serial byte is misread and is not a digit or end of line character.

Code for perfect world doesn’t look for errors because in a perfect world errors never happen. Most new programmers code only for expected things and have enough trouble doing that but in so doing learn enough to understand where errors may happen, so they can next learn to handle errors. It is very few who start out thinking ahead at every step so don’t worry if you are not doing that, you will learn once you bang your head against the wall enough times. Just call me flat-face, okay?

Second post. Now you have seen more about the hill you chose to climb, are you ready to try something a bit easier to learn with?

Make a text file using an editor (NotePad?) on the PC that looks like what you want. Maybe....

00000000 11111111 22222222 33333333

Here you have 4 lines with 8 digits each that you KNOW what they are. Copy that file to your SD card and put the card into your SD adapter/module/shield.

Write your Arduino program to read the file byte by byte to the end and to print each byte as a number value, not a text letter. You do that because some bytes in the file do not print as text characters and you want to know what serial is reading, not just how it looks.

Text '0' value is 48, '1' is 49, but what your editor uses for end of line and end of file are things you won't be sure of until you see. So write code to read the file bytes and show you what is there.

Once you have done that, you will know what to expect (I don't know until I check) and have better ideas how you will climb the project-hill. BUT you have to do it. If you just take code from others, you still won't understand until you do the work. This is the simpler path.