//Code incorporates the following sources (Author. Source Title. URL):
//Arduino instruction book. "Crystal Ball."
//Druhi_C. "Temperature and Humidity sensor with LCD 1602 I2C display." https://projecthub.arduino.cc/Druhi_C/temperature-and-humidity-sensor-with-lcd-1602-i2c-display-a2861e
//codebender_cc. "How to Use DHT-22 Sensor - Arduino Tutorial." https://www.instructables.com/How-to-use-DHT-22-sensor-Arduino-Tutorial/
//arduino-libraries, “SD/examples/Datalogger/Datalogger.ino at master · arduino-libraries/SD,” GitHub, 2015. https://github.com/arduino-libraries/SD/blob/master/examples/Datalogger/Datalogger.ino (accessed Feb. 26, 2024).
//Libraries
#include <DHT.h>;
#include <LiquidCrystal.h>
#include <SPI.h>
#include <SD.h>
//Constants
#define DHTPIN 3 // Which pin DHT sensor is connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
const int chipSelect = 6; // Which pin SD card is written to
//Initialize
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
LiquidCrystal lcd(12,11,10,9,8,7); // generates an instance in the lcd
//Variables
int chk;
float hum; //Stores humidity value
float tempC; //Stores temperature value in C
float tempF = (tempC * 9.0 / 5.0) + 32.0;
void setup()
{
Serial.begin(9600);
dht.begin();
lcd.begin(16,2);
SD.begin(chipSelect);
///////////////////////////////////////////////
while (!Serial);
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("1. Is a card inserted?");
Serial.println("2. Is your wiring correct?");
Serial.println("3. Did you change the chipSelect pin to match your shield or module?");
Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
while (true);
}
Serial.println("initialization done.");
//////////////////////////////////////////////
}
void loop()
{
//Read data and store it to variables hum and tempC for DHT sensor.
hum = dht.readHumidity();
tempC= dht.readTemperature();
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(tempC);
Serial.println(" Celsius");
delay(1000); //Delay 1 sec.
//Read humidity and temperaure data and display it on the LCD.
lcd.setCursor(0,0);// set the cursor on the first row and column
lcd.print("Hum=");
lcd.print((float)dht.readHumidity());//print the humidity
lcd.print("%");
lcd.setCursor(0,1);//set the cursor on the second row and first column
lcd.print("T=");
lcd.print((float)dht.readTemperature());//print the temperature in C
lcd.print("C|");
lcd.print((float)dht.readTemperature() * (9.0 / 5.0) + 32.0);//print the temperature in F
lcd.print("F");
delay(1000);
///////////////////////////////////////
// Make a string for assembling the data to log to the SD card:
String dataString = "";
// read three sensors and append to the string:
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
//Use
File dataFile2 = SD.open("wokwi.txt", FILE_WRITE); //Use "wokwi.txt" for simulations. Else, use datalog.text for IRL.
// if the file is available, write to it:
if (dataFile2) {
dataFile2.println(dataString);
dataFile2.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
/////////////////////////////////////////////////
}