Go Down

Topic: prblem to communicate with SD Card  (Read 359 times) previous topic - next topic

saif221984

Aug 21, 2019, 06:24 pm Last Edit: Aug 21, 2019, 09:48 pm by saif221984
hello all;
i have this data logger shield

http://arduinolearning.com/code/arduino-data-logger-shield-rtc-example.php?fbclid=IwAR0fO-LRCyy0oi4OZUZCqdOvpFHQJbx1ji8IWU7y9Q2KGy2eKe8XHmiYcL4

i try to connect dht22 to store temp. and humidity value
but i have face problem that Arduino mega 2650 cannot recognize the SD card and initialization failed

i follow these below steps
1-format SD card with FAT 32 extension
2-write below code
Code: [Select]
#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <DHT.h> // for the DHT sensor
#include <RTClib.h> // for the RTC
//define DHT pin
#define DHTPIN 7 // what pin we're connected to
// uncomment whatever type you're using
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 53;
// Create a file to store the data
File myFile;
// RTC
RTC_DS1307 rtc;
void setup() {
//initializing the DHT sensor
dht.begin();
//initializing Serial monitor
Serial.begin(9600);
// setup for the RTC
while(!Serial); // for Leonardo/Micro/Zero
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if(! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
// setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//open file
myFile=SD.open("DATA.txt", FILE_WRITE);
// if the file opened ok, write to it:
if (myFile) {
Serial.println("File opened ok");
// print the headings for our data
myFile.println("Date,Time,Temperature ÂșC");
}
myFile.close();
}
void loggingTime() {
DateTime now = rtc.now();
myFile = SD.open("DATA.txt", FILE_WRITE);
if (myFile) {
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(',');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(",");
}
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.println(now.day(), DEC);
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
myFile.close();
delay(1000);
}
void loggingTemperature() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius
float t = dht.readTemperature();
float h=dht.readHumidity();
// Read temperature as Fahrenheit
//float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) ) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//debugging purposes
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
//Serial.print(f);
//Serial.println(" *F\t");
myFile = SD.open("DATA.txt", FILE_WRITE);
if (myFile) {
Serial.println("open with success");
myFile.print(t);
myFile.println(",");
myFile.print(h);
myFile.println(",");
}
myFile.close();
}
void loop() {
loggingTime();
loggingTemperature();
delay(5000);
}

 the screenshot of problem and connection has been attached






pert

saif221984's 3.jpg:


Please post a link to where you bought the SD shield from. It's hard to tell from that low resolution picture, but it looks like it's missing the ICSP header, which is required for compatibility with the Mega 2560.

saif221984

https://imgur.com/X8IdTJW


https://imgur.com/Bx5Xjzv

pert

#3
Aug 22, 2019, 07:47 am Last Edit: Aug 22, 2019, 07:47 am by pert Reason: Embed image
saif221984's image from https://imgur.com/X8IdTJW:



OK, as I thought, there is no ICSP header soldered on that SD shield, so it's only compatible with the Uno. It's unfortunate that it was so poorly designed. There is a way to "hack" the shield to make it work with the Mega:

Unplug the shield from the Mega.

Bend the male pins 11, 12, 13 on the bottom of the shield outward so that when you put the shield back on the Mega they will not go into the female headers on the Mega. Be aware that if you bend the pins sharply multiple times it will weaken the metal and they will eventually break off. So be careful not to bend the pins any more than necessary and then if you ever want to restore the shield back to its original condition you will only need to bend the pins back in place.

Plug the shield back into the Mega

Use male jumper wires to make the following connections:

Shield | Mega
=============
11     | 51
12     | 50
13     | 52

saif221984

Ok thank you
 so i understand that this shield not compatible with  Arduino mega mega
 

pert

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per

Go Up