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!
#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;
}