DHT11 Gather Data without library

Hello,

I am trying to use the DHT11 with a Nano.
Datasheet: https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
I have read the datasheet on the sensor from Mouser and it states that it is Single-wire Two-way (Serial Interface).

It transmits 40 bits of data in a burst. I would like to capture that data and print it out to console in a meaningful way.

I am getting confused on how to accomplish this. What built-in library would be best for accomplishing this? Do you have any advice on how to approach this problem?

I am confused as to whether you want to use a library or not. The topic title says no but your post says yes

Which is it ?

I only want to use the built-in libraries. Not the DHT11 library that is external.

Then start by looking at the source code of the existing DHT11 library that you don't want to use. Figure out how it works. Then write your own version of the code.

Seems like a strange thing to want to do. But, have at it.

I have reviewed that code but it is confusing to me so I came to this forum to try to reduce the problem to it's base case to help aid in my understanding of the DHT11 library. I think capturing a 40 bit stream would be a simple base case but some people love to run to prepackaged libraries that do everything for you.

I have looked into Serial.available but did not want to bias anyone in their advice to me.

Here is my current code where I prime the DHT to send the 40 bits so far. DHT-fun/DHTSerialDanTest.ino at main · danielreyes61/DHT-fun · GitHub

I am just unsure what type of timing is required to make sure that I capture all the bits as it seems like the DHT seems to choose when it wants to send it without much of a preamble or anything.

The DHT11 data sheet has all the information you need to successfully read the device. It does not "choose" what or when to send.

Why do you think a preamble would be useful?

I thought a preamble would be useful in setting timing on the Microcontroller to match the DHT. I am unsure of how the Microcontroller would naturally set timing to be able to read the burst successfully without the risk of reading the same bit twice.

This is what you need to be studying:

Ok, I will keep at it

Ok, I am looking at DHT11.cpp file on github now and it makes a lot more sense this time. I just wanted to give it a solid college try before using the library.

Because the timing is slightly variable, your program needs to determine time differences between rising and falling edges and allow a little slop in what is determined to be a 1 or 0.

Ok, one part of the code that I don't undertand here dht11/dht11.cpp at master · adidax/dht11 · GitHub

is the purpose of the loopcnt 10000 that appears a lot in the code. I don't understand why this number is used.

Arbitrarily chosen timeout period.

"Mystery constants" are generally viewed as a sign of bad programming practice, and should not be emulated.

Ok, thank you!

Perhaps it was empirically chosen. I'd think it might cause issues dependent of processor speed. The RobTillaart library adjusts the loop counts based on F_CPU. You should check that out and also Adafruit's implementation.

Ok I will take a look at those too

/*
  DHT11 Communication Process
  Arduino Pull Low at least 18ms, then Arduino Pause 20-40microseconds for DHT's response

*/


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

void loop() {
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  delay(18);
  pinMode(2, HIGH);
  delay(40); // Delay waiting for DHT's response, must wait 20-40us
  pinMode(2, INPUT);

  while(true)
    Serial.println(digitalRead(2));
}

I feel like this should be working but all I am getting is 0's. I understand that 0 or 1 will be represented my length of time at VDD or GND but I am never seeing the signal go high at all.

This delays for 40 milliseconds, not microseconds. Use delayMicroseconds() instead.

It's interesting.

/*
  DHT11 Communication Process
  Arduino Pull Low at least 18ms, then Arduino Pause 20-40microseconds for DHT's response

*/


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

void loop() {
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  delay(18);
  pinMode(2, HIGH);
  delayMicroseconds(40); // Delay waiting for DHT's response, must wait 20-40us
  pinMode(2, INPUT);

  while(true)
    if(digitalRead(2)>0){ 
        Serial.println("Greater found");
      }
}

I wonder what I am doing wrong. It doesn't seem to be responding.

What value pullup resistor are you using on the DHT11 data line?