SD Card Logging for Ultrasonic Distance Sensor Data

Hey guys,

I really hope someone here can help me!

I have set up a simple circuit on my breadboard: I have connected an ultrasonic distance sensor (HC-SR04) to my ATmega2560 (to see how Trigger and Echo Pins are connected to the IO pins, see code given below).

I have furthermore connected an SD-Card reader/writer to the Arduino. I am using this device for the first time. Apparently, it is connected via SPI. I have followed a tutorial on setting this circuit up and I hope that it is connected correctly. I am not specifying any other pin than the chipSelect pin in the code given below. That’s why I assume a mistake in how I connected the SD-Card reader to my Arduino:

CS - Pin 53
MOSI - Pin 51
SCK - Pin 52
MISO - Pin 50

I want my sensor to measure the distance towards an obstacle and write this measured value to my SD card. In order to know if the condition “mySensorData” in the code below holds, I am also printing to the Serial Port.

However, when I run this code on my Arduino, nothing gets printed to the Serial Port. Therefore, I know already that it is not working.

I cannot figure out what the error might be. If somebody sees something, please let me know!

Here is my code:

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

int chipSelect = 53;
File mySensorData;

int trigPin = 7;                                                    //ultrasonic-sensor trigger pin is connected to IO 7
int echoPin = 6;                                                    //ultrasonic-sensor echo pin is connected to IO 6
float roundTrip;                                                    //round-trip-time (RTT)
float distance2target;
float speedOfSound = 343;                                           //speed of sound at 20°C in m/s 



void setup() {

  Serial.begin(9600);
  pinMode(trigPin,OUTPUT);
  pinMode(echoPin,INPUT);
  pinMode(10,OUTPUT);                                               //reserve IO 10 as an output - this is required by the SD.h library
  SD.begin(chipSelect);                                             //initialize SD-card

}

void loop() {
  
  //the ultrasonic-sensor starts its measurements upon arrival of a trigger pulse
  //its response to the microcontroller is a pulse whose length corresponds to the RTT
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2000);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(15);
  digitalWrite(trigPin,LOW);
  delayMicroseconds(15);

  roundTrip = pulseIn(echoPin,HIGH);                                //measure RTT in microseconds
  roundTrip = roundTrip/1000000;                                    //RTT in seconds
  distance2target = speedOfSound * roundTrip/2;                     //distance to target in m 
  distance2target = distance2target * 100;                          //distance to target in cm


  mySensorData = SD.open("HCSR.txt", FILE_WRITE);
  if(mySensorData) {
    Serial.print("Distance to target: ");
    Serial.print(distance2target);
    Serial.println(" cm");

    delay(1000);

    mySensorData.print(distance2target);
    mySensorData.println(",");
    mySensorData.close();
  }
  
  }

And btw, here is a hyperlink to the SD card reader that I am using: SD Memory Card Module Slot SPI Reader - Eckstein Komponente, 2,08 €
There is a circuit drawing given at the end.

I think that maybe I need to connect the +3.3V of the SD card reader as well. I figured this would not be necessary since I have already connected the +5V.
So, how do I figure out the resistance I need when connecting the +3.3 V ? R=U/I, but what is the desired current?

IMG_1030.JPG

Please post your code, in code tags.

done. So, do you see anything that might be the issue? Do you think it is possibly due to the missing connection of the +3.3V of the reader ?

Luk_11:
That’s why I assume a mistake in how I connected the SD-Card reader to my Arduino:

CS - Pin 53
MOSI - Pin 51
SCK - Pin 52
MISO - Pin 50

Your picture says otherwise:
a902e4c212355e86f2a81428bb69412bbcea6a44.jpg
Try actually connecting the SD module to the pins as you claimed and see if the problem is solved.

hey,

so sorry, the image was the first set up. I then realized later that this set up was only correct for the Arduino Uno. That's why I have changed the connection in the way you can see on img01, img02 and img03. I have just uploaded these images.

Unfortunately the problem is still the same. Everything seems to be connected in the correct way, so I can't get my head around what might be the issue.

I recommend adding something like this:

Serial.println("Starting program");

In your setup() after the call to Serial.begin(), just to make sure that serial output is working correctly.