#include <LiquidCrystal.h>
#include "DHT.h"
#include <SPI.h>
#include <SD.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
const int rs = 8, en = 7, d4 = 6, d5 = 5, d6 = 4, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
DHT dht(DHTPIN, DHTTYPE);
File myFile;
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
dht.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(10)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
}
void loop() {
// Wait a few seconds between measurements.
delay(10000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
int h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Photoresistor
int value = analogRead(A0);
int l = (value / 1023) * 100000;
lcd.setCursor(0, 0);
lcd.print(F("T: "));
lcd.print(t);
lcd.print(F("C "));
lcd.setCursor(1, 1);
lcd.print(F(" Hum: "));
lcd.print(h);
lcd.println(F("% "));
lcd.setCursor(0, 1);
lcd.print("L: ");
lcd.print(l);
lcd.print("%");
if (myFile) {
Serial.print("Writing to SD as text.txt");
while (1) {
Serial.print("Humedad: ");
Serial.println(h);
myFile.print("Humedad: ");
myFile.println(h);
Serial.print("Temperatura: ");
Serial.println(t);
myFile.print("Temperatura: ");
myFile.println(t);
Serial.print("Luz: ");
Serial.println(l);
myFile.print("Luz: ");
myFile.println(l);
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
sorry I posted all the code, but the problem I´m having is that I start my arduino, go to my serial monitor an well it displays this "Initializing SD card... initialization failed" so what I do is remove the SD card from the module, then reinsert it, run the serial monitor again and it works, every time, has anyone had this problem and know how to fix it?
This is my schematic of the whole circuit