DHT11 & Teensy 2.0

Status
Not open for further replies.

ian

Well-known member
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.
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:
I have the DHT22 and use the Adafruit library. This code works for me. Give it a try.
Code:
#include "DHT.h"

// Now using pin 8 on Teensy2
#define DHTPIN 8     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// RED is +5V
// YELLOW is the DHTPIN
// BLACK is GROUND
// It works without the 10k between +5V and DHTPIN but won't hurt
// 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");
  }
}

Pete
 
I bought one of these sensors a while ago. Not sure if it's the 11 or 22. It's still sitting in a box of stuff I'm intending to test. Looks like I need to give it a try.....
 
There seems to be a timing issue with the Adafruit library. It works if I set teensy to run at 8MHz.
Paul, the DHT11 is blue - and has DHT11 printed on the back ;)

Ian
 
The library at http://playground.arduino.cc/main/DHT11Lib worked for me with a Teensy 3.0:

Code:
#include <dht11.h>  // from http://playground.arduino.cc/main/DHT11Lib
dht11 dht;

// Plug DHT11 in adjacent to pins 0-3
#define DHTPWR 0     // +5 or +3.3
#define DHTDATA 1     // Data  (Worked w/o pullup resistor.
#define DHTNC 2      // Not used
#define DHTGND 3     // GND

void setup() {
  delay(2000);
  Serial.begin(9600); 
  Serial.println("DHT11 test!");
 
 // Make power connections for DHT:
  pinMode(DHTPWR,OUTPUT);  digitalWrite(DHTPWR,HIGH);  // +Vcc to DHT
  pinMode(DHTGND,OUTPUT); digitalWrite(DHTGND,LOW);   // GND to DHT
}

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)
  dht.read(DHTDATA);
  float h = dht.humidity;
  float t = dht.temperature;

  // 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");
  }
  delay(2000);
}


Also, editing the .../libraries/DHT/dht.cpp code to use a delayMicroseconds(4) instead of delayMicroseconds(1) per http://forum.pjrc.com/threads/16806-T3-FYI-Adafruit-DHT22-AM2302 works for a DHT11 on a Teensy 3.0.
 
Last edited:
Status
Not open for further replies.
Back
Top