I'm trying to use a DHT11 sensor with Teensy 2.0
I've used this reliably with Arduinos but I'm struggling with Teensy. I was originally using an Adafruit library http://learn.adafruit.com/dht
This works fine with Arduino but returns garbage with Teensy.
I also tried some code from DFrobot (which is very similar - see attached) and I get the same result.
Ideally I'd like to use the same code with Teensy & Arduino as I have two groups of students, one group uses Arduino & the other Teensy, however getting this to work at all with Teensy would be useful.
Pullup resistors are often recommended for DHT11. I've not had a problem with or without on Arduino & it makes no difference to Teensy.
I've tried many different pins on Teensy & changed the defined dht11_pin accordingly.
I've used this reliably with Arduinos but I'm struggling with Teensy. I was originally using an Adafruit library http://learn.adafruit.com/dht
This works fine with Arduino but returns garbage with Teensy.
I also tried some code from DFrobot (which is very similar - see attached) and I get the same result.
Ideally I'd like to use the same code with Teensy & Arduino as I have two groups of students, one group uses Arduino & the other Teensy, however getting this to work at all with Teensy would be useful.
Pullup resistors are often recommended for DHT11. I've not had a problem with or without on Arduino & it makes no difference to Teensy.
I've tried many different pins on Teensy & changed the defined dht11_pin accordingly.
Code:
#define dht11_pin 14 //Analog port 0 on Arduino Uno
//#define dht11_pin 54 //Analog port 0 on Arduino Mega2560
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++)
{
while (!digitalRead(dht11_pin));
delayMicroseconds(30);
if (digitalRead(dht11_pin) != 0 )
bitSet(result, 7-i);
while (digitalRead(dht11_pin));
}
return result;
}
void setup()
{
pinMode(dht11_pin, OUTPUT);
digitalWrite(dht11_pin, HIGH);
Serial.begin(9600);
Serial.println("Ready");
}
void loop()
{
byte dht11_dat[5];
byte dht11_in;
byte i;// start condition
digitalWrite(dht11_pin, LOW);
delay(18);
digitalWrite(dht11_pin, HIGH);
delayMicroseconds(1);
pinMode(dht11_pin, INPUT);
delayMicroseconds(40);
if (digitalRead(dht11_pin))
{
Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
delay(1000);
return;
}
delayMicroseconds(80);
if (!digitalRead(dht11_pin))
{
Serial.println("dht11 start condition 2 not met"); //wair for second response signal:HIGH
return;
}
delayMicroseconds(80);// now ready for data reception
for (i=0; i<5; i++)
{ dht11_dat[i] = read_dht11_dat();} //recieved 40 bits data. Details are described in datasheet
pinMode(dht11_pin, OUTPUT);
digitalWrite(dht11_pin, HIGH);
byte dht11_check_sum = dht11_dat[0]+dht11_dat[2];// check check_sum
if(dht11_dat[4]!= dht11_check_sum)
{
Serial.println("DHT11 checksum error");
}
Serial.print("Current humdity = ");
Serial.print(dht11_dat[0], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht11_dat[2], DEC);
Serial.println("C ");
delay(2000); //fresh time
}
Last edited: