#define ntc_pin A0 // Pin,to which the voltage divider is connected
//#define vd_power_pin 2 // 5V for the voltage divider
#define nominal_resistance 10000 //Nominal resistance at 25⁰C
#define nominal_temeprature 25 // temperature for nominal resistance (almost always 25⁰ C)
#define samplingrate 5 // Number of samples
#define beta 3950 // The beta coefficient or the B value of the thermistor (usually 3000-4000) check the datasheet for the accurate value.
#define Rref 10000 //Value of resistor used for the voltage divider
#include <DHT.h>
#include <DHT_U.h>
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
int samples = 0; //array to store the samples
void DHT(){
// 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.println(h);
Serial.print(F("DHT Temperature: "));
Serial.print(t);
Serial.println(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop(void) {
uint8_t i;
float average;
samples = 0;
// take voltage readings from the voltage divider
// digitalWrite(vd_power_pin, HIGH);
for (i = 0; i < samplingrate; i++) {
samples += analogRead(ntc_pin);
delay(10);
}
// digitalWrite(vd_power_pin, LOW);
average = 0;
average = samples / samplingrate;
Serial.println("\n \n");
Serial.print("ADC readings ");
Serial.println(average);
// Calculate NTC resistance
average = 1023 / average - 1;
average = Rref / average;
Serial.print("Thermistor resistance ");
Serial.println(average);
float temperature;
temperature = average / nominal_resistance; // (R/Ro)
temperature = log(temperature); // ln(R/Ro)
temperature /= beta; // 1/B * ln(R/Ro)
temperature += 1.0 / (nominal_temeprature + 273.15); // + (1/To)
temperature = 1.0 / temperature; // Invert
temperature -= 273.15; // convert absolute temp to C ---> Kelvin To Celsius
Serial.print("NTC Temperature ");
Serial.print(temperature);
Serial.println(" *C");
delay(2000);
DHT();
}