Hello,
I have an Arduino UNO board, Micro SD/LAN shield, a DHT21 sensor and a LCD 16x2 screen.
My problem is that whenever I initialize the sd card in my setup (...if (!SD.begin(chipSelect)) { ....) LCD crashes after this line and prints two lines of "33333". It is logging correctly though in my microSD. If I comment the line and not initialize my SD, LCD prints all the messages correctly. I checked the pins and that's why I connect the LCD to analog pins (used as digital) but still no luck. I am pretty sure I have no other device connected on the pins that SD card uses.
Any ideas?
Thank you in advance!
Here is my code:
#include <LiquidCrystal.h>
#include "DHT.h"
#include <SD.h>
#include <Time.h>
byte thermometer_icon[8] = //icon for termometer
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110
};
byte humidity_icon[8] = //icon for water droplet
{
B00100,
B00100,
B01010,
B01010,
B10001,
B10001,
B10001,
B01110,
};
#define DHTPIN 18 // what pin we're connected to
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Pins for the LCD display, as taken from arduino.cc
LiquidCrystal lcd(12, 11, 16, 17, 15, 14); //Pins changed so that the don't conflict with sd
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
const int chipSelect = 4;
char message[]= "Temp/Humi Logger";
int previous = 0;
int pos = 0;
void setup() {
dht.begin();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Temp/Humi Logger");
lcd.setCursor(0, 1);
lcd.print("by IRETETH-CERTH");
delay(4000);
Serial.begin(9600);
lcd.createChar(1,thermometer_icon);
lcd.createChar(2,humidity_icon);
lcd.clear();
lcd.print("Initializing SD...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
// don't do anything more:
return;
}
lcd.print("card initialized.");
delay(3000);
}
//a counter to increase after each reading
int counter = 0;
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
h = h - 5;
float t = dht.readTemperature();
t = t - 5;
float dew = dewPoint(t, h);
// t = t* 1.8 + 32;
// printLine(1);
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
lcd.setCursor(0, 0);
lcd.print("Failed to read from DHT");
Serial.println("Failed to read from DHT");
} else {
// lcd.clear();
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(2);
lcd.print(h,1);
lcd.print("% ");
lcd.write(1);
lcd.print(t,1);
lcd.print((char)223); //degree sign
lcd.print("C"); //degree sign
lcd.setCursor(0, 1);
// lcd.print("*C");
lcd.print("Dew:");
lcd.print(dew,1);
lcd.print((char)223); //degree sign
lcd.print("C"); //degree sign
// lcd.println("By IRETETH-CERTH");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println((char)223); //degree sign
// 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.print(counter);
dataFile.print(", ");
dataFile.print("Humidity: ");
dataFile.print(h);
dataFile.print(", Temperature: ");
dataFile.print(t);
dataFile.print(", Dew Point: ");
dataFile.print(dew);
dataFile.print("\n");
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
delay(3000);
counter++;
}
void printLine(int refreshSeconds){
//Check if the current second since restart is a mod of refresh seconds ,
//if it is then update the display , it must also not equal the previously
//stored value to prevent duplicate refreshes
if((millis()/1000) % refreshSeconds == 0 && previous != (millis()/1000)){
previous = (millis()/1000);//Store the current time we entered for comparison on the next cycle
lcd.setCursor(0, 1);//Set our draw position , set second param to 0 to use the top line
char lcdTop[16];//Create a char array to store the text for the line
int copySize = 16; // What is the size of our screen , this could probably be moved outside the loop but its more dynamic like this
if(strlen(message) < 16)
{
//if the message is bigger than the current buffer use its length instead;
copySize = strlen(message);
}
//Store the current position temporarily and invert its sign if its negative since we are going in reverse
int tempPos = pos;
if(tempPos < 0)
{
tempPos = -(tempPos);
}
//Build the lcd text by copying the required text out of our template message variable
memcpy(&lcdTop[0],&message[tempPos],copySize);
lcd.print(lcdTop);//Print it from position 0
//Increase the current position and check if the position + 16 (screen size) would be larger than the message length , if it is go in reverse by inverting the sign.
pos += 1;
if(pos +16 >= strlen(message))
{
pos = -(pos);
}
}
}
/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}
// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}