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: https://eckstein-shop.de/SD-Memory-Card-Module-Slot-SPI-Reader
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?