0

I am new at arduino. I want to write a sketch that would print the message if the temperature grows greater than 30 C. Here is my sketch can you please tell me what's wrong with it

#include "DHT.h"
#define DHTTYPE DHT11
#define dht_dpin D3

DHT dht(dht_dpin, DHTTYPE); 

void setup()
{
 Serial.begin(9600);
 }
void loop()
{
 float t = dht.readTemperature();  
 Serial.print("temp=");
 Serial.println(t);
 attachInterrupt(dht_dpin, IntCallback, CHANGE);  
}
void IntCallback()
{
  float t = dht.readTemperature();  
  if(t>30)
  {
    Serial.print("Alert");
  }
}
  • 1
    remove lines starting from the line with attachIterrupt until the line before if (t > 30) – Juraj Apr 4 at 12:36
1

You can't use an interrupt the way you are attempting to. Attaching an interrupt to a pin like that will cause the interrupt to trigger any time the digital pin's value changes. The DHT11 uses serial communication to send temp values to the Arduino. Serial communication uses lots and lots of on/off pulses on the signal line to send characters of data. Your interrupt will fire every time the DHT11 toggles the signal line.

You need to read the temp from the DHT11 in your loop, and then add an if statement that does what you want when the temp exceeds your threshold. (It looks like you just need to get rid of the attachInterrupt() call, and instead call IntCallback() every couple of seconds.

  • Hi.Thanks for helping but i am a noob in this. I don't know much about Interrupt and callback. So if you don't mind can you please modify the sketch for me. That would a great help. – Neeraj Chimwal Apr 5 at 8:26
0

You don't even need an interrupt.Modification:

#include "DHT.h"
#define DHTTYPE DHT11
#define dht_dpin D3

DHT dht(dht_dpin, DHTTYPE); 

void setup()
{
 Serial.begin(9600); 
 }
void loop()
{
 float t = dht.readTemperature();  
 Serial.print("temp=");
 if(t>30)
  {
    Serial.print("Alert");
  }
}
  • Let me explain my whole situation. My project is to make a gps tracking system plus using dht11 to give an alert through GSM if the temperature grows above 30 C. But the problem is after 1st or 2nd output, the dht11 is not giving any reading it shows "Nan" in serial monitor. It taking 28 to 30 seconds to give another reading. So I thought may be using Interrupt will work. Any suggestions for this?? – Neeraj Chimwal Apr 6 at 5:11
  • try using other library for DHT11 then. – Vaibhav Apr 6 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.