Go Down

Topic: Arduino Uno stops sending sensor information (Read 773 times) previous topic - next topic

Jechu182

Hello guys! This is my first time on this forum, i hope i put this in the right place and sorry for my bad english...

Im very new to Arduino, i bought my first one today, an Arduino Uno R3 along with a LCD and a DHT11.

But i have a problem, i uploaded a sketch allowing me to monitor the sensors information, it does so 2 or 3 times then it stops. The TX LED on the Arduino stops blinking and everything, and I have to open up serial monitor again so it displays it a couple of more times, but then it stops again like before, and so on. I tried resetting the Arduino, but its still the same. For now i just have the sensor connected, but i would like to put the LCD also, can anyone help me? Is there something wrong with the following sketch? I found it on the internet. Or is it my board or software problem?

Thanks in advance to anyone!

Code: [Select]
#define dht_dpin 2 // put here the pin where the sensor is plugged

byte bGlobalErr;  //for passing error code back from complex functions.
byte dht_dat[5];  //Array to hold the bytes sent from sensor.

void setup(){
  InitDHT();  // Initialize the pin used to read the sensor
  Serial.begin(9600);
  delay(300);  //Let system settle
  Serial.println("Humidity and temperature\n\n");
  delay(700);  //Wait rest of 1000ms recommended delay before
               //accessing sensor
}

void loop(){
  ReadDHT(); // Read sensor an store result in global variables
  switch (bGlobalErr){
     case 0:
Serial.print("Current humdity = ");
Serial.print(dht_dat[0], DEC);
//Serial.print(".");
//Serial.print(dht_dat[1], DEC); dht resolution is 1% RH
Serial.print("%  ");
Serial.print("temperature = ");
Serial.print(dht_dat[2], DEC);
//Serial.print(".");
//Serial.print(dht_dat[3], DEC); dht resolution is 1ÂșC
Serial.println("C  ");
        break;
     case 1:
        Serial.println("Error 1: DHT start condition 1 not met.");
        break;
     case 2:
        Serial.println("Error 2: DHT start condition 2 not met.");
        break;
     case 3:
        Serial.println("Error 3: DHT checksum error.");
        break;
     default:
        Serial.println("Error: Unrecognized code encountered.");
        break;
  }//end "switch"
  delay(800);// wait up to 1s. for next reading
}


// Initilize pin for reading
void InitDHT(){
        pinMode(dht_dpin,OUTPUT);
        digitalWrite(dht_dpin,HIGH);
}

void ReadDHT(){
  bGlobalErr=0;
  byte dht_in;
  byte i;
  // Send "start read and report" command to sensor....
  // First: pull-down i/o pin for 18ms
  digitalWrite(dht_dpin,LOW);
  delay(18);
  delay(5);//TKB, frm Quine at Arduino forum
  //Next line: Brings line high again,
  //   second step in giving "start read..." command
  digitalWrite(dht_dpin,HIGH);
  delayMicroseconds(40);//DHT22 datasheet says host should
    //keep line high 20-40us, then watch for sensor taking line
    //low. That low should last 80us. Acknowledges "start read
    //and report" command.

  //Next: Change Arduino pin to an input, to
  //watch for the 80us low explained a moment ago.
  pinMode(dht_dpin,INPUT);
  delayMicroseconds(40);

  dht_in=digitalRead(dht_dpin);

  if(dht_in){
    bGlobalErr=1;
    return;
  }
  delayMicroseconds(80);

  dht_in=digitalRead(dht_dpin);

  if(!dht_in){
    bGlobalErr=2;
    return;
  }

  /*After 80us low, the line should be taken high for 80us by the
  sensor. The low following that high is the start of the first
  bit of the forty to come. The routine "read_dht_dat()"
  expects to be called with the system already into this low.*/
  delayMicroseconds(80);
  //now ready for data reception... pick up the 5 bytes coming from
  //   the sensor
  for (i=0; i<5; i++)
    dht_dat[i] = read_dht_dat();

  //Next: restore pin to output duties
  pinMode(dht_dpin,OUTPUT);

  //Next: Make data line high again, as output from Arduino
  digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);

  //Next see if data received consistent with checksum received
  byte dht_check_sum =
       dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
  /*Condition in following "if" says "if fifth byte from sensor
       not the same as the sum of the first four..."*/
  if(dht_dat[4]!= dht_check_sum)
    bGlobalErr=3;
  };

byte read_dht_dat(){
  //Collect 8 bits from datastream, return them interpreted
  //as a byte. I.e. if 0000.0101 is sent, return decimal 5.

  //Code expects the system to have recently entered the
  //dataline low condition at the start of every data bit's
  //transmission BEFORE this function is called.

  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){
      //We enter this during the first start bit (low for 50uS) of the byte
      //Next: wait until pin goes high
      //      signalling end of start of bit's transmission.     
      while(digitalRead(dht_dpin)==LOW);

      //Dataline will now stay high for 27 or 70 uS, depending on
      //whether a 0 or a 1 is being sent, respectively.
      delayMicroseconds(30);//AFTER pin is high, wait further period, to be
        //into the part of the timing diagram where a 0 or a 1 denotes
        //the datum being send.

      //Next: Wait while pin still high
      if (digitalRead(dht_dpin)==HIGH)
   result |=(1<<(7-i));// "add" (not just addition) the 1
                      //to the growing byte
    //Next wait until pin goes low again, which signals the START
    //of the NEXT bit's transmission.
    while (digitalRead(dht_dpin)==HIGH);
    }
  return result;
}

Nick Gammon

What does your debugging tell you (the serial monitor output)?
http://www.gammon.com.au/electronics

Jechu182

It shows me:

Humidity and temperature


Current humdity = 61%  temperature = 26C 
Current humdity = 61%  temperature = 26C

Sometimes it gives me more lines, but it always stops...

wildbill

There are a couple of while loops that have the potential to freeze your program if there is an issue with the timing in the communication with the sensor. As a novice, I'd be reluctant to try debugging it, but you might try the ladyada tutorial and library instead - there have been recent threads with people using it successfully. http://learn.adafruit.com/dht/downloads

Docedison

Yes it works very well and is really easy to use. I use it on an AMS2302 (DHT22) sensor and compare temp results very favorably with a  Bosch BMP085 Baro/Temp sensor.

Bob
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

colinspence

Hi I am new to Arduino as well, and have the same problem and more.
The data flow stops after a few lines
I can't get the serial monitor and the LCD to work together which I want
Where does the phototransitor go?
And can I have direction for the code for three temperature sensors operating off digital i/o 2

Sorry about the list please help.

Jechu182

Thanks to everyone, i have successfully connected the lcd and DHT11 to my arduino, it is working great, I just decided to use a new sketch along with the LCD. I will use the ladyada when i connect it to the pc and see if it works good w/o LCD. Thanks again!

colinspence: I used this circuit and sketch http://www.hobbyist.co.nz/?q=documentations/booting-up-the-weather-station


colinspence

Thanks Jec182

Did you solve the freeze problem. I used the sketch you pointed me to and got it going but still freezes on the 5 -10 line of output. Dis you find where the phototransistor goes that comes with the kit?

Go Up