DHT

Hello!

Does anyone know how to use DHT11 or DHT22 without any library?

Yes, it can be done. But, more importantly, why would you want to do this?

You’d just be wasting time recreating code that has already been written and probably by a better programmer than you. You’re not going to save program space or find a benefit of any kind by not using a library.

Of course you could write your own driver but it seems you haven't looked at the data sheet all too carefully yet or you wouldn't even consider it...

A library is "just" pre-written code.

But, it may be written with assembly language (or using some Assembly language) and it may access internal CPU registers/functions that are not available in the regular-basic Arduino C++ language/library.

There's nothing you can't do in a regular .ino file that must be done in a library.

arduinotibor:
Hello!

Does anyone know how to use DHT11 or DHT22 without any library?

Have you tried reading the datasheet and implementing the protocol described therein? It would be pretty straightforward to implement by polling for state changes with "digitaRead()" and measuring intervals with "micros()".

MrMark:
Have you tried reading the datasheet and implementing the protocol described therein? It would be pretty straightforward to implement by polling for state changes with "digitaRead()" and measuring intervals with "micros()".

Having read the data sheet and having read the code of a DHT library I can tell you it's anything but straightforward. Timing is critical - and that makes it so hard.

Just implement the single-wire protocol described in the datasheet; it's not terribly hard provided you have some experience with low level programming of an AVR (I think the datarate is low enough that you don't need to use direct port reads even...). Having appropriate test equipment available of course would help in debugging. I did it in JS for Espruino....

But the more important question is why you feel it necessary to reinvent the wheel like that, when there are numerous DHT libraries that have been written by experienced programmers and which work beautifully.

wvmarle:
Having read the data sheet and having read the code of a DHT library I can tell you it’s anything but straightforward. Timing is critical - and that makes it so hard.

The timing that needs be resolved is distinguishing between nominal 27 microsecond pulses (“0” bits) and nominal 70 microsecond pulses (“1” bits). I expect that can be done via polling the pin and timing via “micros()”, but using the “pulseIn()” function is even easier. The code that follows just measures the positive pulse width and declares a “0” if it’s less that 50 microseconds and “1” if it is more than that.

Here’s a demo for the DHT11:

// Implementation of DHT11 without using DHT library 
// Uses pulseIn function to measure pulse widths
// https://www.mouser.com/ds/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf

int dhtPin = 2 ;
unsigned long ackWidth ;

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

void loop() {
  McuStartSignal() ;    // Generate MCU Start Signal per DHT11 datasheet

  // DHT11 Acknowledge Signal
  // Don't really care about the width, but measure it to keep place in signal stream
  unsigned long ackWidth = pulseIn(dhtPin, HIGH, 1000) ;

  // DHT11 Data Packet is 5 bytes
  byte intRH = readByte() ;   // Integer RH data byte
  byte decRH = readByte() ;   // Decimal RH data byte (always 0 on DHT11)
  byte intT = readByte() ;    // Integer T data byte
  byte decT = readByte() ;    // Decimal RH data byte (always 0 on DHT11)
  byte checksum = readByte() ;    // Checsum Byte (should be sum of 4 bytes above mod 256)
  
  // Print values read
  Serial.println() ;
  Serial.print("ackPulseWidth:") ;    Serial.println(ackWidth) ;
  Serial.print("intRH: ") ;           Serial.println(intRH) ;
  Serial.print("decRH: ") ;           Serial.println(decRH) ;
  Serial.print("intT: ") ;            Serial.println(intT) ;
  Serial.print("decT: ") ;            Serial.println(decT) ;
  Serial.print("checksum: ") ;        Serial.println(checksum) ;
  
  delay(2000) ;                 // Wait 2 seconds and repeat
}

void McuStartSignal() {
  // Pulse DHT signal low for 18 milliseconds to request data
  // Pin Mode changes between input and output to emulate open collector pin, 
  //   that is, it only drives LOW never HIGH as output
  digitalWrite(dhtPin, LOW) ;   // Set DHT value low before enabling output
  pinMode(dhtPin, OUTPUT) ;     // Pull DHT line low
  delay(18) ;                   // Hold DHT for 18 milliseconds
  pinMode(dhtPin, INPUT) ;      // Release DHT pin
}

byte readByte() {
  // Read one byte of serial data packet per DHT11 protocol
  // Measure positive pulsewidths, if > 50 uS shift "1" into LSB of byte
  byte value = 0 ;
  for (int k=0; k<8; k++){
    value = value << 1 ;
    if (pulseIn(dhtPin, HIGH, 1000) > 50){
      value = value + 1 ;
    }
  }
  return value ;
}

While Arduino libraries greatly simplify interfacing to sensors, the lesson is that serial data protocols generally aren’t all that complicated if one just follows the guidance of the device datasheet. This can be helpful when one moves to a device or microcontroller without library support.

You can do it, though you would probably need a logic analyzer to verify your timing. Nothing wrong with rolling your own.

arduinotibor:
Hello!

Does anyone know how to use DHT11 or DHT22 without any library?

I often find that when people as such a question, there is a reason. and that reason is not stated.
This is called an X/Y PROBLEM
if the library works, then you should not have any problems, needs, or wants.
if it is not working, or doing something you do not want, then explain what your needs or problems are.
if you know how to use tabs, you can copy the library into a tab. then it is no longer supported, but also, it will not be changed over time with upgrades. this can be both good and bad.
also, if there is a lot of fluff in a library that you never use, you can comment that out to make your sketch smaller.
it is axiomatic that if you have to ask, it is on your learning curve.
as others have stated, you can write your own sketch to do what the library already does.
and, as others have stated, many libraries have been created by people who have much more extensive knowledge of a thing, and I will add, have tested, failed, tested, failed and tested again, to get it to work.
I am curious as to what is your real reason for not wanting to use the library ?

XY Problem

I also want to use the code without using the existing libraries, as none of my 15 DHT sensors (10 DHT22 and 5 DHT11) seems to work with these, a few years back I had a similar issue, and to the extent I remember, it worked with downgrading the library version. But now, nothing is working. Wanna use the basic principles to find the actual issue. The message I receive is that they failed to read the DHT sensor reading.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.