Arduino DHT Sensor

#include "DHT.h"  //FOR DHT SENSOR#include <SPI.h>  //FOR SD CARD#include <SD.h>   //FOR SD CARD#include <OneWire.h>#include <DallasTemperature.h>

const int chipSelect = 4;  //Pin to SD card
#define DHTPIN 2     // Digital pin connected to the DHT sensor
// Data wire is plugged into port 7 on the Arduino for water temp probe#define ONE_WIRE_BUS 7// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.DallasTemperature sensors(&oneWire);
// Uncomment whatever type you're using!//#define DHTTYPE DHT11   // DHT 11#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321//#define DHTTYPE DHT21   // DHT 21 (AM2301)
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
void setup() {  // start serial port  Serial.begin(9600);
  dht.begin();  Serial.println(F("DHT AM2302 test!"));  sensors.begin();  Serial.println("Water Probe Sensor");    while (!Serial) {    ; // wait for serial port to connect. Needed for native USB port only  }
   Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:  if (!SD.begin(chipSelect)) {    Serial.println("Card failed, or not present");    // don't do anything more:    while (1);  }

 }
void loop() {
  // make a string for assembling the data to log:  //String dataString = "";
   // Wait a few seconds between measurements.  delay(9000);    sensors.requestTemperatures(); // Send the command to get temperatures  // After we got the temperatures, we can print them here.  // We use the function ByIndex, and as an example get the temperature from the first sensor only.  float tempC = sensors.getTempCByIndex(0);   // 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();  // 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;  }
  // Compute heat index in Fahrenheit (the default)  float hif = dht.computeHeatIndex(f, h);  // Compute heat index in Celsius (isFahreheit = false)  float hic = dht.computeHeatIndex(t, h, false);
  Serial.print(F("Humidity: "));  Serial.print(h);  Serial.print(F("%  Temperature: "));  Serial.print(t);  Serial.print(F("°C "));  Serial.print(f);  Serial.print(F("°F  Heat index: "));  Serial.print(hic);  Serial.print(F("°C "));  Serial.print(hif);  Serial.print(F("°F"));  Serial.print(F(" Water Temp: "));  Serial.print(tempC);  Serial.println(F("°C "));

    // 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(F("Humidity: "));    dataFile.print (h);  dataFile.print(F("%  Temperature: "));  dataFile.print(t);  dataFile.print(F("°C "));  dataFile.print(f);  dataFile.print(F("°F  Heat index: ")); dataFile.print(hic);  dataFile.print(F("°C "));  dataFile.print(hif);  dataFile.print(F("°F"));  dataFile.print(F(" Water Temp: "));  dataFile.print(tempC);  dataFile.println(F("°C "));    dataFile.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");  }

Donate

If you've enjoyed exploring my Arduino projects and want to see more amazing creations, your support can make a big difference! By contributing, you're helping me continue to innovate and bring even more exciting projects to life. Together, we can explore the endless possibilities of DIY electronics! Don't forget to like, subscribe, and follow for updates on the latest developments. Thank you for being a part of this journey! 

Click here to make a difference with your donation today!