Hi guys, does anyone know how to measure the rapid changes of temperature rate using DHT11? For example, i want to compare previous and current rate to determine whether there is an abnormal environment changing happen or not.
Delta_G:
This is a math problem, not a programming problem.Read the temperature and record the value of millis(). Then do that again to get two new values of temp and time. Subtract the two temperatures, that's how much the temp changed. Subtract the two times, that's how much the time changed. Divide to get the rate of change.
1. roomTempA = k*dht.readTemp()
2. presentmillisA = millis()
3. roomTemB = k*dht.readTemp()
4. presentmillisB = millis()
5. diffTemp = roomTempB - roomTempA
6. diffmillis = presentmillisB - presentmillisA
7. Rate of Change = diffTemp/diffmillis
: Ans:
Thanks for help, btw i need to calculate the rate of change after certain period (for example 5 minute) . Just like the first temperature read is 35 c , after 5 minute is 29 . so the rate of change is -6 something like this. isit possible code like this way? The code i have currently is something like this.
void loop()
{
float t = readDHT();
float prev_t = t;
float current_t = (readDHT());
if((current_t<= 30)&&(prev_t>30))
{}
But what i need is compare current temp with the previous temp after 5 minute. Something like Fast temperature increase detection . Anyone can help me on this issue?
Let us make the life easy by converting the steps of Post#2 which is the direct translation of the descriptive steps of Post#1.
1. (a) Choose a sensor -- let us take it as dht11 (Temperature and Humidity Sensor)
(b) Choose a Library for dht11 -- SimpleDHT.h
(c) Include the Library in your Arduino IDE.
(d) Bring in an example from the IDE:
(e) Connect the dht11 sensor with the UNO as per wiring/connection diagram given in the example.
(f) Upload the example into the UNO.
(g) Let us view the temperature/humidity value on Serial Monitor at 2-sec interval.
float roomTempA = dht.readTemperature();
long presentmillisA = millis();
delay(1000);
float roomTempB = dht.readTemperature();
long presentmillisB = millis();
float diffTemp = roomTempB - roomTempA;
long diffmillis = presentmillisB - presentmillisA;
float roc = diffTemp/diffmillis;
Serial.print(roomTempA);
Serial.print(" ");
Serial.print(presentmillisA);
Serial.print(" ");
Serial.print(roomTempB);
Serial.print(" ");
Serial.print(presentmillisB);
Serial.print(" ");
Serial.print(roc);
Serial.print(" \n");
---------------------------------------------------->
RESULT
---------------------------------------------------->
31.00 270 31.00 1270 0.00
31.00 1271 31.00 2544 0.00
31.00 2545 31.00 3546 0.00
31.00 3547 31.00 4817 0.00
31.00 4819 31.00 5820 0.00
31.00 5821 31.00 7092 0.00
31.00 7094 31.00 8093 0.00
31.00 8095 31.00 9366 0.00
-------------------------------------------------->
However, can i display the real time temperature as usual yet getting a code run behind to calculate the rate of change of the temperature sensor after 5minute? For example
Current Temp : 16
Current Temp : 16
Current Temp : 16
Current Temp : 17
Current Temp : 18
Then for each 5 minute, a function will run behind to calculate the rate of change in this period
Please, put code tags across your codes of Post#5. Go to the Edit mode of Post#5. Select the code and then click on this sign </> of the tools bar. Your codes will appear like this:
float roomTempA = dht.readTemperature();
long presentmillisA = millis();
delay(1000);
float roomTempB = dht.readTemperature();
long presentmillisB = millis();
Where are your following functions? - the basic two functions of Arduino IDE. Your codes will be within these two functions.
void setup()
{
}
void loop()
{
}
Tasks that will be executed only for once or for a limited number of times will be within the setup() functions.
Tasks that will be executed again and again will be within the loop() function.
Tasks that will be called upon from the setup()/loop() function are usually placed below the loop() function with the following definition:
void userDefinedFunction()
{
}
Have you physically connected a DHT11 sensor with your Arduino? If so, we would have expected the following program running in your UNO.
#include <SimpleDHT.h>
// for DHT11,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11;
void setup() {
Serial.begin(115200);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11...");
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" H");
// DHT11 sampling rate is 1HZ.
delay(1500);
}
Yup i did, actually the library file i'm using is DHT sensor library. With this code, i able to get the temperature and humidity.
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C\t");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print(" \n");
}
To include the formula that you provided in post #2, i modify the code to be liked this
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
float roomTempA = dht.readTemperature();
long presentmillisA = millis();
delay(1000);
float roomTempB = dht.readTemperature();
long presentmillisB = millis();
float diffTemp = roomTempB - roomTempA;
long diffmillis = presentmillisB - presentmillisA;
float roc = diffTemp/diffmillis;
Serial.print(roomTempA);
Serial.print(" ");
Serial.print(presentmillisA);
Serial.print(" ");
Serial.print(roomTempB);
Serial.print(" ");
Serial.print(presentmillisB);
Serial.print(" ");
Serial.print(roc);
Serial.print(" \n");
float roomTempA = dht.readTemperature();
for (int i =0; i < 6 ; i++){
}
}
From this code, i able to get the current and previous temperature among 1 secs. Instead from display the real time data (alike #7 picture ) that you posted earlier, i need an equation which run behind the code to capture the differences of the temperature among each 5 minute to determine whether it was rapidly increase (maybe due to fire).
OK! Your and mine set up are now identical. Let us go back to Post#2 and convert those steps in programming codes using our own style. Necessary function/methods and declarations/definitions could be conveniently taken from the example program of IDE.
Let us write a small program of our own to display the temperature from DHT11 and show it on Serial Monitor with two digits after the decimal point. The temperature will be acquired at 2-sec interval. The 2-sec interval will be created not by calling upon the delay() function but using the millis() function.
What does millis() function do?
After uploading a sketch into the UNO, one of the Timer/Counter Modules it is probably TC1) of the MCU begins to increment an unsigned (holds only positive value) 32-bit counter at 1 ms interval. This 32-bit counter is made up of using four consecutive byte oriented memory locations. Total time that this counter can accommodate is 0xFFFFFFFF ms = 4294967295 ms ~= 4294967 sec ~= 71582 min ~= 1193 hrs ~= 49 days. millis() is a function which we can use at any time to know how much time the Timer/Counter has accommodated since the uploading time of the sketch or the last reset time of Arduino.
Let us begin writing our program codes and post the progress!