DHTxx Example

From Pinguino
Jump to: navigation, search

Example for using a DHTxx sensor with a Pinguino. The library for this example can be downloaded from here

/*	-----------------------------------------------------------------------
Pinguino example to read DHT22 1wire humidity/temperature sensor
IDE	 Pinguino > b9.5

-----------------------------------------------------------------------
DHT22 wiring
-----------------------------------------------------------------------
pin 1: VDD (+5V)
pin 2: DQ (Data in/out) must be connected to the PIC
pin 3: NC
pin 4: GND
NB : 1-wire bus (DQ line) must have 10K pull-up 
----------------------------------------------------------------------*/

#define DHTPIN	18	 // DQ line	1

KSDHT22 dht;

u8 bErr; //para pasar el codigo de error de vuelta de las funciones
u8 DHTDAT[5]; //Array with 
u8 DHTCHECKSUM=0; 
u32 mil;

void setup()
{
  pinMode(USERLED, OUTPUT);
  pinMode(DHTPIN,INPUT);
  digitalWrite(USERLED,LOW); 
  delay(3000);
}

void loop()
{
  bErr=KS_DHT.read(DHTPIN,&dht); 
  switch (bErr)
  {
    case 0: //no error
      CDC.printf("Humidity: %02.2f ",dht.hum);
      if (dht.sign) CDC.print("-");
      CDC.printf("Temperature: %02.2f \r\n",dht.temp);
      break;
    case 1:
      CDC.print("start condition error 1.\r\n");
      break;
    case 2:
      CDC.print("start condition error 2.\r\n");
      break;
    case 3:
      CDC.print("no response signal.\r\n");
      break;
    case 4:
      CDC.print("preparation of data fails.\r\n");
      break;
    case 5:
      CDC.print("no data received.\r\n");
      break;
    case 6:
      CDC.print("data read error.\r\n");
      break;
    case 7:
      CDC.print("checksum error.\r\n");
      break; 
  } 

digitalWrite(USERLED,HIGH);
delay(2);
digitalWrite(USERLED,LOW);
delay(5000);
}