problema dht11

ciao, non riesco a leggere la temp e l'umidità di questo sensore
sul monitor seriale mi dice sempre read fail, come se non comunicasse o ricevesse valori nan

ho installato le librerie e copiato vari sketch , ma nessuno che mi funzioni

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11    


// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 
  dht.begin();
}

void loop() {
  // 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();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
  }
}

collegandolo a un lcd mi restituisce valori 0

Io uso questo sketch che ho preso da un sito:

//ReadHumTturDHT11alternate2
//ver 19Jly10

//This is a re-written DHT11/ DHT22 reading code.
//DHT stuff in subroutines.

//See for more information....
//http://sheepdogguides.som/arduino/ar3ne1humDHT11.htm

//N.B. "bit" is used in the narrow, computer "1 or 0"
//   sense throughout.

//"DHT" from sensor's names: DHT11, DHT22.
//DHT aka Aosong AM2302, and there's an AM2303 which
//seems to be in the same family.

//Comments on this based on Aosong AM2302, aka DHT22, datasheet.
//Believed to generally apply to DHT11 as well, except in the
//case of the DHT11, I believe the second and fourth bytes are
//always zero.

//***N.B.****
//The code WORKS... the comments may not yet be EXACTLY right.
//See the web-page cited above for latest news.

//This code works with a DHT11 humidity/ temperature sensing module
//from nuelectronics.com, complied with ver 0018 of the Arduino environment
//Sensor attached to P4 (nuelectonics shield)/ analog 0, aka digital 14.

//That "module", according to the
//nuelectronics site, and visual inspection simply provides for easy
//connection of an Aosong DHT11 unit to the nuelectronics datalogging
//shield. Only 3 wires are involved: Vcc, ground, and a single data
//line. One of the DHT11's 4 pins goes nowhere.

//You should not need to change anything except the next line to use
//the software with the sensor on a different line, or for a DHT22.

//Just "huffing" on the sensor from deeply filled lungs should show
//a near instant rise in humidity

//#define dht_PIN 0      //no ; here. deprecate ADC0...
//even though we are using it as a digital pin.
//Other parts of code restrict us to using
//ADC0-5, aka D14-19
#define dht_dpin 14 //no ; here. Set equal to channel sensor is on,
//where if dht_dpin is 14, sensor is on digital line 14, aka analog 0


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

void setup(){
  InitDHT();//Do what's necessary to prepare for reading DHT
  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
}//end "setup()"

void loop(){
  ReadDHT();//This is the "heart" of the program.
  //Fills global array dht_dpin[], and bGlobalErr, which
  //will hold zero if ReadDHT went okay.
  //Must call InitDHT once (in "setup()" is usual) before
  //calling ReadDHT.
  //Following: Display what was seen...
  switch (bGlobalErr){
  case 0:
    Serial.print("Current humdity = ");
    Serial.print(dht_dat[0], DEC);
    Serial.print(".");
    Serial.print(dht_dat[1], DEC);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(dht_dat[2], DEC);
    Serial.print(".");
    Serial.print(dht_dat[3], DEC);
    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);//Don't try to access too frequently... in theory
  //should be once per two seconds, fastest,
  //but seems to work after 0.8 second.
}// end loop()

/*Below here: Only "black box" elements which can just be plugged unchanged
 unchanged into programs. Provide InitDHT() and ReadDHT(), and a function
 one of them uses.*/

void InitDHT(){
  //DDRC |= _BV(dht_PIN);//set data pin... for now... as output
  //DDRC is data direction register for pins A0-5 are on
  //PORTC |= _BV(dht_PIN);//Set line high
  //PORTC relates to the pins A0-5 are on.
  //Alternative code...
  //        if (dht_dpin-14 != dht_PIN){Serial.println("ERROR- dht_dpin must be 14 more than dht_PIN");};//end InitDHT
  pinMode(dht_dpin,OUTPUT);// replaces DDRC... as long as dht_dpin=14->19
  digitalWrite(dht_dpin,HIGH);//Replaces PORTC |= if dht_pin=14->19
}//end InitDHT

void ReadDHT(){
  /*Uses global variables dht_dat[0-4], and bGlobalErr to pass
   "answer" back. bGlobalErr=0 if read went okay.
   Depends on global dht_PIN for where to look for sensor.*/
  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);//Was: PORTC &= ~_BV(dht_PIN);
  delay(18);
  delay(5);//TKB, frm Quine at Arduino forum
  /*aosong.com datasheet for DHT22 says pin should be low at least
   500us. I infer it can be low longer without any]
   penalty apart from making "read sensor" process take
   longer. */
  //Next line: Brings line high again,
  //   second step in giving "start read..." command
  digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
  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);//Was: DDRC &= ~_BV(dht_PIN);
  delayMicroseconds(40);

  dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);

  if(dht_in){
    bGlobalErr=1;//Was: Serial.println("dht11 start condition 1 not met");
    return;
  }//end "if..."
  delayMicroseconds(80);

  dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);

  if(!dht_in){
    bGlobalErr=2;//Was: Serial.println("dht11 start condition 2 not met");
    return;
  }//end "if..."

  /*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);//Was: DDRC |= _BV(dht_PIN);
  //N.B.: Using DDRC put restrictions on value of dht_pin

  //Next: Make data line high again, as output from Arduino
  digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
  //N.B.: Using PORTC put restrictions on value of 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;
  }//Was: Serial.println("DHT11 checksum error");
};//end ReadDHT()

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
    while(digitalRead(dht_dpin)==LOW);//Was: while(!(PINC & _BV(dht_PIN)));
    //signalling end of start of bit's transmission.

    //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. The "further period" was 30uS in the software
    //that this has been created from. I believe that a higher number
    //(45?) would be more appropriate.

    //Next: Wait while pin still high
    if (digitalRead(dht_dpin)==HIGH)//Was: if(PINC & _BV(dht_PIN))
      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);//Was: while((PINC & _BV(dht_PIN)));
  }//end of "for.."
  return result;
}//end of "read_dht_dat()"

Prova a vedere se funziona.

@destroyman94:
ciao e benvenuto.
Ti invito a leggere il regolamento del forum ed a rispettarlo per i prossimi post. :wink:

Potresti averlo collegato male. Fai uno schemino o una foto di come lo hai collegato.

Hertz:
Io uso questo sketch che ho preso da un sito:

//ReadHumTturDHT11alternate2

//ver 19Jly10

//This is a re-written DHT11/ DHT22 reading code.
//DHT stuff in subroutines.

//See for more information....
//http://sheepdogguides.som/arduino/ar3ne1humDHT11.htm

//N.B. "bit" is used in the narrow, computer "1 or 0"
//   sense throughout.

//"DHT" from sensor's names: DHT11, DHT22.
//DHT aka Aosong AM2302, and there's an AM2303 which
//seems to be in the same family.

//Comments on this based on Aosong AM2302, aka DHT22, datasheet.
//Believed to generally apply to DHT11 as well, except in the
//case of the DHT11, I believe the second and fourth bytes are
//always zero.

//N.B.*
//The code WORKS... the comments may not yet be EXACTLY right.
//See the web-page cited above for latest news.

//This code works with a DHT11 humidity/ temperature sensing module
//from nuelectronics.com, complied with ver 0018 of the Arduino environment
//Sensor attached to P4 (nuelectonics shield)/ analog 0, aka digital 14.

//That "module", according to the
//nuelectronics site, and visual inspection simply provides for easy
//connection of an Aosong DHT11 unit to the nuelectronics datalogging
//shield. Only 3 wires are involved: Vcc, ground, and a single data
//line. One of the DHT11's 4 pins goes nowhere.

//You should not need to change anything except the next line to use
//the software with the sensor on a different line, or for a DHT22.

//Just "huffing" on the sensor from deeply filled lungs should show
//a near instant rise in humidity

//#define dht_PIN 0      //no ; here. deprecate ADC0...
//even though we are using it as a digital pin.
//Other parts of code restrict us to using
//ADC0-5, aka D14-19
#define dht_dpin 14 //no ; here. Set equal to channel sensor is on,
//where if dht_dpin is 14, sensor is on digital line 14, aka analog 0

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

void setup(){
 InitDHT();//Do what's necessary to prepare for reading DHT
 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
}//end "setup()"

void loop(){
 ReadDHT();//This is the "heart" of the program.
 //Fills global array dht_dpin[], and bGlobalErr, which
 //will hold zero if ReadDHT went okay.
 //Must call InitDHT once (in "setup()" is usual) before
 //calling ReadDHT.
 //Following: Display what was seen...
 switch (bGlobalErr){
 case 0:
   Serial.print("Current humdity = ");
   Serial.print(dht_dat[0], DEC);
   Serial.print(".");
   Serial.print(dht_dat[1], DEC);
   Serial.print("%  ");
   Serial.print("temperature = ");
   Serial.print(dht_dat[2], DEC);
   Serial.print(".");
   Serial.print(dht_dat[3], DEC);
   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);//Don't try to access too frequently... in theory
 //should be once per two seconds, fastest,
 //but seems to work after 0.8 second.
}// end loop()

/Below here: Only "black box" elements which can just be plugged unchanged
unchanged into programs. Provide InitDHT() and ReadDHT(), and a function
one of them uses.
/

void InitDHT(){
 //DDRC |= _BV(dht_PIN);//set data pin... for now... as output
 //DDRC is data direction register for pins A0-5 are on
 //PORTC |= _BV(dht_PIN);//Set line high
 //PORTC relates to the pins A0-5 are on.
 //Alternative code...
 //        if (dht_dpin-14 != dht_PIN){Serial.println("ERROR- dht_dpin must be 14 more than dht_PIN");};//end InitDHT
 pinMode(dht_dpin,OUTPUT);// replaces DDRC... as long as dht_dpin=14->19
 digitalWrite(dht_dpin,HIGH);//Replaces PORTC |= if dht_pin=14->19
}//end InitDHT

void ReadDHT(){
 /Uses global variables dht_dat[0-4], and bGlobalErr to pass
  "answer" back. bGlobalErr=0 if read went okay.
  Depends on global dht_PIN for where to look for sensor.
/
 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);//Was: PORTC &= ~_BV(dht_PIN);
 delay(18);
 delay(5);//TKB, frm Quine at Arduino forum
 /*aosong.com datasheet for DHT22 says pin should be low at least
  500us. I infer it can be low longer without any]
  penalty apart from making "read sensor" process take
  longer. */
 //Next line: Brings line high again,
 //   second step in giving "start read..." command
 digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
 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);//Was: DDRC &= ~_BV(dht_PIN);
 delayMicroseconds(40);

dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);

if(dht_in){
   bGlobalErr=1;//Was: Serial.println("dht11 start condition 1 not met");
   return;
 }//end "if..."
 delayMicroseconds(80);

dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);

if(!dht_in){
   bGlobalErr=2;//Was: Serial.println("dht11 start condition 2 not met");
   return;
 }//end "if..."

/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);//Was: DDRC |= _BV(dht_PIN);
 //N.B.: Using DDRC put restrictions on value of dht_pin

//Next: Make data line high again, as output from Arduino
 digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
 //N.B.: Using PORTC put restrictions on value of 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;
 }//Was: Serial.println("DHT11 checksum error");
};//end ReadDHT()

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
   while(digitalRead(dht_dpin)==LOW);//Was: while(!(PINC & _BV(dht_PIN)));
   //signalling end of start of bit's transmission.

//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. The "further period" was 30uS in the software
   //that this has been created from. I believe that a higher number
   //(45?) would be more appropriate.

//Next: Wait while pin still high
   if (digitalRead(dht_dpin)==HIGH)//Was: if(PINC & _BV(dht_PIN))
     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);//Was: while((PINC & _BV(dht_PIN)));
 }//end of "for.."
 return result;
}//end of "read_dht_dat()"



Prova a vedere se funziona.

niente da fare , sul monitor seriale mi dice error 1 condition not met

l'ho collegato cosi http://learning.grobotronics.com/images/Tutorials/dhtwiring.gif
solo che il pin 2 è collegato al pin A0

Io ho usato sempre quella di Adafruits, mi sembra affidabile:

attualmente uso quella, cmq le ho provate tutte ma niente risultati

sabato mattina ho la maturità e sono nei guai

Mi viene da pensare che non sia un problema di codice, ma di hardware. Non conosci nessuno che abbia un altro Arduino per provarlo?

o un altro dht11...

Concordo con lesto: se hai fatto bene i collegamenti e hai caricato lo sketch che ti ho postato, poiché a me funziona dovrebbe funzionare anche a te almeno che non è come dice nid ma speriamo di no.
In bocca al lupo per la maturità comunque! :wink:

Ho comprato un altro dht11 ma niente
la porta com10 e' selezionata correttamente

Per verificare che non sia l' Arduino il problema prova ad esempio ad accendere un LED via seriale (giusto per provare qualcosa di più semplice), oppure prova a cambiare pin su cui colleghi il sensore.

Ad esempio nello sketch di test, dice dhtpin 2, ma come indico il pin di arduino a cui lho collegato? Adesempio a5 o d4

usa il codice della libreria postata da nid, ma fai attenzione a cambiare (in DHTtester)

//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)

in

#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)

infatti sia il tuo codice che la lib di default sono per la DHT22

destroyman94:
Ad esempio nello sketch di test, dice dhtpin 2, ma come indico il pin di arduino a cui lho collegato? Adesempio a5 o d4

la nujmerdazione segue qualla dei pin digitali
quindi d1 = 1, d2 = 2 etc.. a seguire quella dei digitali: a0=14, a1 = 15 etc.. però puoi scrivere "A0" o D1 al posto del numero, tanto sono variabili definite le core arduino apposta per non fare confuzione, quindi

dhtpin = D1;

ho reinstallato tutto e ora funziona

poi ho collegato lo schermo e ha ricominciato a non leggere....che stress

destroyman94:
ho reinstallato tutto e ora funziona

poi ho collegato lo schermo e ha ricominciato a non leggere....che stress

ahahahah beccato il problema! pin sharati??

Pubblica cosa non andava. Può essere utile ad altri. E se hai risolto puoi aggiungere [Risolto] al titolo del thread sul tuo primo post.