//Code incorporates the following sources:
//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).
//“SD card read write - Wokwi ESP32, STM32, Arduino Simulator,” Wokwi.com, 2024. https://wokwi.com/projects/360531831170769921 (accessed Feb. 27, 2024).
//Additional Resources Used:
//“The Full Arduino Uno Pinout Guide [including diagram],” circuito.io blog, Aug. 12, 2018. https://www.circuito.io/blog/arduino-uno-pinout/ (accessed Feb. 27, 2024).
//Libraries
#include <SD.h>
#include <DHT.h>;
#include <LiquidCrystal.h>
#include <SPI.h>
//Pins
#define DHTPIN 2 // Which pin DHT sensor is connected to
const int chipSelect = 10; // Which pin SD card is written to
//Initialize
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
LiquidCrystal lcd(9,8,7,6,5,4); // 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() {
dht.begin();
//lcd.begin(16,2);
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect
}
Serial.print("Initializing SD card... ");
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed.");
return;
}
Serial.println("SD card initialized successfully.");
}
void loop() {
//Read data and store it to variables hum and tempC for DHT sensor.
hum = dht.readHumidity();
tempC= dht.readTemperature();
// Make a string for assembling the data to log to the SD card:
String dataString = "";
dataString += String(hum);
dataString += " %, ";
dataString += String(tempC);
dataString += " C, ";
dataString += String(tempF);
dataString += " F";
delay(2000); //Delay 2 sec.
//Open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// if the file is available, write to it:
File dataFile2 = SD.open("wokwi.txt", FILE_WRITE); // Open the file "data.txt" on the SD card for writing
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");
}
//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");
}