Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

NodeMCu Interface with DHT11

Status
Not open for further replies.

ajit_nayak87

Member level 5
Joined
Oct 30, 2017
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
981
I am testing NodeMCu Interface with DHT11 unit. I have used arduino uno board where i am getting proper reading for temp & humidity.
I have attached output Serial for reference.
Code:
#include "dht.h"

dht DHT;

#define DHT11_PIN 7

void setup(){
  Serial.begin(115200);
}

void loop()
{
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}
Now I am testing same code using NodeMCu with SimpleDHT11 library. But Here i wont get Reading from DHT sensor. I tried Power 3.3v with seprate regulator with common ground. But result are same

Code without library . I get an timeout error.

Code:
// DHT11 and DHT22 sensor reading demo
// by 'jurs' for Arduino Forum

// DHT functions enumerated
enum {DHT11_SAMPLE, DHT22_SAMPLE, DHT_TEMPERATURE, DHT_HUMIDITY, DHT_DATAPTR};

// Begin of user configuration area
#define DHT_SAMPLE DHT11_SAMPLE // must be DHT11_SAMPLE or DHT22_SAMPLE
#define DHT_PIN 2
// End of user configuration area

// DHT error codes enumerated
enum {DHT_OK=0, DHT_ERROR_TIMEOUT=-1, DHT_ERROR_CRC=-2, DHT_ERROR_UNKNOWN=-3};

void setup()
{
  Serial.begin(115200);
  Serial.println("DHT Temperature and Huminity Measurement");
}

// DHT sensor pinout from left to right looking at the gridded side
// 1-VCC  2-DATA  3-NC  4-GND

int dhtCall(byte pin, byte function)
// input parameters are the data pin and one of the DHT functions
// return value is DHT error code with function DHT11_SAMPLE or DHT22_SAMPLE
// alsways do sampling with DHT_OK result before calling other functions
// return value is temperature with function DHT_TEMPERATURE
// return value is humidity with function DHT_HUMIDITY
// return value is pointer to byte array containing raw data with function DHT_DATAPTR
{
  static int temperature=-999;
  static int humidity=-999;
  static byte data[5]; // 5 bytes to receive 40 data bits
  unsigned int loopCnt; // loop counter
  byte sum;  // checksum
  #define DHT_LOOPS 1800
  int triggerTime;
  switch (function)
  {
    case DHT11_SAMPLE: // REQUEST DHT11 SAMPLE
    case DHT22_SAMPLE: // REQUEST DHT22 SAMPLE
      if (function==DHT11_SAMPLE) triggerTime=20000; // 20000µs trigger time for DHT11
      else triggerTime=1000; // 1000µs trigger time for DHT22
      pinMode(pin, OUTPUT); 
      digitalWrite(pin, LOW);
      delayMicroseconds(triggerTime); 
      pinMode(pin,INPUT_PULLUP);
      loopCnt = DHT_LOOPS;
      while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
      loopCnt = DHT_LOOPS;
      while(digitalRead(pin) == LOW) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
      loopCnt = DHT_LOOPS;
      while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
      for (byte bitNum=0;bitNum<40;bitNum++) // try reading 40 bits
      {
        loopCnt = DHT_LOOPS;
        while(digitalRead(pin) == LOW) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
        delayMicroseconds(32);
        boolean dhtBit=digitalRead(pin);
        bitWrite(data[bitNum/8],7-bitNum%8,dhtBit);
        loopCnt = DHT_LOOPS;
        while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
      }
      sum = data[0] + data[1] + data[2] + data[3];  
      if (data[4] != sum) return DHT_ERROR_CRC;
      if (function==DHT11_SAMPLE)
      {
        humidity=data[0];
        temperature=data[2];
      }
      else
      {
        humidity=data[0]*256+data[1];
        temperature= (data[2] & 0x7F) * 256 + data[3];
        if (data[2] & 0x80) temperature= -temperature;
      }
      return DHT_OK;
    case DHT_TEMPERATURE:
      return temperature;
    case DHT_HUMIDITY:  
      return humidity;
    case DHT_DATAPTR:
      return (int)data;
    default:
      return DHT_ERROR_UNKNOWN;
  }  
}

void loop()
{
  byte* b; // byte pointer for showing raw data
  switch (dhtCall(DHT_PIN, DHT_SAMPLE)) // always request a sample first
  {
    case DHT_OK: // only if DHT_OK is true, get temperature, humidity and possibly raw data
      Serial.print("Humidity: ");
      Serial.print(dhtCall(DHT_PIN, DHT_HUMIDITY));
      Serial.print("   Temp: ");
      Serial.print(dhtCall(DHT_PIN, DHT_TEMPERATURE));
      Serial.print("     raw data:  ");
      b=(byte*)dhtCall(DHT_PIN, DHT_DATAPTR);
      for (int i=0;i<5;i++)
      {
        Serial.print(b[i]);Serial.print('\t');
      }
      Serial.println();
      break;
    case DHT_ERROR_TIMEOUT:
      Serial.println("Timeout Error");
      break;
    case DHT_ERROR_CRC:
      Serial.println("CRC Error");
      break;
    default:
      Serial.println("Unknown Error");
  }
  delay(2000); // minimum time between two DHT samples is two seconds
}

SimpleDHTlibrary

Code:
#include "SimpleDHT.h"

// for DHT11, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11;

void setup() {
  Serial.begin(115200);
}

void loop() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT11...");
  
  // read without samples.
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.println(err);delay(1000);
    return;
  }
  
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, "); 
  Serial.print((int)humidity); Serial.println(" H");
  
  // DHT11 sampling rate is 1HZ.
  delay(1500);
}
 

Attachments

  • Ard_uno_out.jpg
    Ard_uno_out.jpg
    111.6 KB · Views: 60
  • DHT version.jpg
    DHT version.jpg
    292.4 KB · Views: 72

Are you following the guidelines in the DHT11 data sheet, in particular the delay needed before initializing it and the need for a pull-up resistor and decoupling capacitor if the cable is long?

Brian.
 

Are you following the guidelines in the DHT11 data sheet, in particular the delay needed before initializing it and the need for a pull-up resistor and decoupling capacitor if the cable is long?

Brian.
I am testing same like wise used for arduino uno. It's working well with arduino uno. Only thing i changes is nodemcu v1. 0 used with simpledht11 library. I am getting time out error. Yes length is short. I have powered using external 3.3v with common ground. If I program without library also I get time out error.
 

It's working well with arduino uno. Only thing i changes is nodemcu v1. 0 used with simpledht11 library

You have defined the sensor pinout with the specific value 7:

Code:
#define DHT11_PIN 7

However, from the values defined for ESP8266 at the file pins_arduino.h this number is not assigned to any pin:

Code:
static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;

So, instead of using a numeral parameter, prefer using mnemonics Dn which turns the code more easy to debug, as well as to match with the pin numbering printed on the board:

N_MCU.png

BTW, for the NodeMCU board, you should be aware that the following pins have already built-in resistors that assure initial state to these pins during start-up, therefore unless you are plenty sure on how to share them with other signals w/o conflicting, select any others but these ones:

// PIN-27 // D3 : GPIO0 ; // PULL-UP
// PIN-26 // D4 : GPIO2 ; // PULL-UP
// PIN-20 // D8 : GPIO15 ; // PULL-DOWN
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top