user19140: I am trying to save some data to the SD card using the memory shield module that I read from HC-SR04 Ultrasonic Module. The data that gets printed indicates error. This is how my SD card module is connected.

SD card attached to SPI bus as
  VCC -Arduino 5v
  GND- Arduino GND
  MOSI - pin 11
  MISO - pin 12
  CLK - pin 13
  CS - pin 4

Still, Arduino complains that there was an error opening the file in SD card. My sketch is this:  
#include <SPI.h>
#include <SD.h>
#define echoPin 6 // Echo Pin
#define trigPin 7 // Trigger Pin

int maximumRange = 250; // Maximum range needed
int minimumRange = 1; // Minimum range needed
long duration, distance; // Duration used to calculate distance
const int chipSelect = 4;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

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

void loop() {
  readDistance();
  if(distance>minimumRange && distance < maximumRange){
    String string = "distance is" + distance;
    // 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 = SD.open("datalog.txt", FILE_WRITE);
    // if the file is available, write to it:
    if( dataFile ){
      dataFile.println(string);
      dataFile.close();
      Serial.println( string );
    }else{
      // if the file isn't open, pop up an error:
      Serial.println("error opening datalog.txt");
      dataFile.close();
    }
  }else{
    Serial.println("Out of range...");
  }
  delay(50);
}

int readDistance(){
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;
}  

Output:  
rror opening datalog.txt
rror opening datalog.txt
error opening datalog.txt
rror opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt

How do I solve this to save the readings to SD card?

user78126: Is there any print out based on this;
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:
  return;
}
Serial.println("card initialized.");

Solution;
Try change
const int chipSelect = 4; // change value to 10

If not, try change the above code to;
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
  Serial.println("Initialization failed!");
  while (1);
}
Serial.println("Initialization done.");


19140: <p>I am trying to save some data to the SD card using the memory shield module that I read from HC-SR04 Ultrasonic Module. The data that gets printed indicates error. This is how my SD card module is connected.</p>

<blockquote>
  <p>SD card attached to SPI bus as<br>
  VCC -Arduino 5v<br>
  GND- Arduino GND<br>
  MOSI - pin 11<br>
  MISO - pin 12<br>
  CLK - pin 13<br>
  CS - pin 4</p>
</blockquote>

<p>Still, Arduino complains that there was an error opening the file in SD card. My sketch is this:  </p>

<pre><code>#include &lt;SPI.h&gt;
#include &lt;SD.h&gt;
#define echoPin 6 // Echo Pin
#define trigPin 7 // Trigger Pin

int maximumRange = 250; // Maximum range needed
int minimumRange = 1; // Minimum range needed
long duration, distance; // Duration used to calculate distance
const int chipSelect = 4;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

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

void loop() {
  readDistance();
  if(distance&gt;minimumRange &amp;&amp; distance &lt; maximumRange){
    String string = "distance is" + distance;
    // 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 = SD.open("datalog.txt", FILE_WRITE);
    // if the file is available, write to it:
    if( dataFile ){
      dataFile.println(string);
      dataFile.close();
      Serial.println( string );
    }else{
      // if the file isn't open, pop up an error:
      Serial.println("error opening datalog.txt");
      dataFile.close();
    }
  }else{
    Serial.println("Out of range...");
  }
  delay(50);
}

int readDistance(){
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration/58.2;
}  
</code></pre>

<p>Output:  </p>

<pre><code>rror opening datalog.txt
rror opening datalog.txt
error opening datalog.txt
rror opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
error opening datalog.txt
</code></pre>

<p>How do I solve this to save the readings to SD card?</p>
 78126: <p>Is there any print out based on this;</p>
<pre><code>Serial.print(&quot;Initializing SD card...&quot;);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
  Serial.println(&quot;Card failed, or not present&quot;);
  // don't do anything more:
  return;
}
Serial.println(&quot;card initialized.&quot;);
</code></pre>
<p>Solution;</p>
<p>Try change</p>
<pre><code>const int chipSelect = 4; // change value to 10
</code></pre>
<p>If not, try change the above code to;</p>
<pre><code>Serial.print(&quot;Initializing SD card...&quot;);
if (!SD.begin(10)) {
  Serial.println(&quot;Initialization failed!&quot;);
  while (1);
}
Serial.println(&quot;Initialization done.&quot;);
</code></pre>
