Pages: 1 2 [3] 4   Go Down
Author Topic: yet another DHT11 Class for Arduino  (Read 14831 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Robtillaart,  Have you thought about using the (:source lang=c:) and (:sourcend:) for your code in the playground?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Krodal,

I have updated the DHT lib pages,  it looks def. better!

Thanks,


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

update to version 0.4.1 - http://arduino.cc/playground/Main/DHT11Lib

- added #define error codes
- use these in sample application

makes the code more readable, no functionality change.
« Last Edit: May 19, 2012, 03:18:21 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I am trying out your DHT11 librarys using both dht11_test1.pde and DHT11LIB_VERSION "0.4.1" but keep getting this error:

dht11\dht11.cpp.o: In function `dht11::read(int)':
C:\Program Files\Arduino-1.0\libraries\dht11/dht11.cpp:28: multiple definition of `dht11::read(int)'
dht11.cpp.o:C:\DOCUME~1\DrWil\LOCALS~1\Temp\build8921908579828605482.tmp/dht11.cpp:28: first defined here

Is dht11::read(int) being redefined in the temp build folder?

Arduino software version 1.0.1
Arduino hardware version Duemilanove w/ ATmega328

Cheers
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying out your DHT11 librarys using both dht11_test1.pde and DHT11LIB_VERSION "0.4.1" but keep getting this error

It is running fine with me.
Arduino 1.0.1.
The three files (not as library), and changed one thing in the example file:
Code:
#include <dht11.h>
into
Code:
#include "dht11.h"
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rob Tillaart,

I don't know if I open an old issue, but I'm not convinced that the Arduino should set the line high. I read the datasheet over and over again, and I think it is not very clear but I read the datasheet as if the resistor pulls the line high.

The datasheet tells that the "DHT pulls up voltage...", but I assume it is an open collector output. So they mean the pull-up resistor. So when the datasheet tells that the "MCU pulls up voltage...", I think they mean it in the same way.


Your code:

Code:
        pinMode(pin, OUTPUT);
        digitalWrite(pin, LOW);
        delay(18);
        digitalWrite(pin, HIGH);
        delayMicroseconds(40);
        pinMode(pin, INPUT);

But this works as well:

Code:
        // The line should be set as INPUT_PULLUP.
        // Make it output to pull it down.
        // Wait at least 18ms, so let's wait 25ms
        pinMode(pin, OUTPUT);
        digitalWrite(pin, LOW);
        delay(25);
        pinMode(pin, INPUT_PULLUP);
        // The DHT11 will react in 20 - 40us.
        // Let's wait 50 to be sure it has responded.
        delayMicroseconds(50);

I use the resistor to make the pullup. So there will be no shortcut when the DHT11 pulls the line down.
I also wait 25ms, since 18ms is the minimum.
And I wait 50us, to be sure that the DHT11 has already pulled the line down.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

from Arduino.h
Code:
#define HIGH 0x1
#define LOW  0x0

#define INPUT 0x0
#define OUTPUT 0x1
#define INPUT_PULLUP 0x2

HIGH and INPUT_PULLUP is clearly different values but digitalWrite handles them identically, so no difference in electrical behaviour.

from wiring_digital.c
Code:
void digitalWrite(uint8_t pin, uint8_t val)
{
uint8_t timer = digitalPinToTimer(pin);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *out;

if (port == NOT_A_PIN) return;

// If the pin that support PWM output, we need to turn it off
// before doing a digital write.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);

out = portOutputRegister(port);

uint8_t oldSREG = SREG;
cli();

if (val == LOW) {
*out &= ~bit;
} else {
*out |= bit;
}

SREG = oldSREG;
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The point is: In my code I let the Arduino never output 5V. The high level of the signal is only done by the resistors. This also prevents a shortcut when the Arduino outputs 5V and the sensor tries to pull it to ground.
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The same goes for the DHT lib, http://playground.arduino.cc/Main/DHTLib
The datasheet of the DHT22 tells: "...then MCU will pulls up and wait 20-40us for RHT03's response". The only way to wait for a response is to let the pull-up resistor make the signal high, and not by making the output high with the Arduino pin.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

updated  - http://arduino.cc/playground/Main/DHT11Lib - ( dewpoint only)

changelog:
+ added some comments + references explaining the dewPoint algorithm.
+ a small performance improvement in dewPointFast (from 5x -> 6.9x as fast)
+ a small test program to compare the output of the two dewPoint algorithms.
« Last Edit: August 04, 2013, 04:30:18 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I took the liberty to make a few minor modifications to the code that you provided, and I wanted to share them with you.  If you're interested, you can find my changes at:

https://github.com/adalton/arduino/tree/master/projects/Dht11_Library

Thanks,

Andy
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for sharing this version, I added a link to your work in the playground article.

Most interesting change is to give the datapin in the constructor, so you have more the OO approach iso functional approach. To support multiple sensors you will make one unique object per sensor, where my approach is to have one object to rule them all. 

some questions:
- Did you measure the effect of the code changes on the footprint (per object) ?

- Are you familiar with - http://playground.arduino.cc/Main/DHTLib - which supports the DHT22 too (and DHT21 under test) ?
  This is my preferred lib for DHT sensors, and I still want to rewrite it in OO with a base class and 3 derived classes.

- can you add a link to the playground in your code?

Final note, I see you used the goto statement to handle error conditions. Although you did it in a structured way to handle error conditions it is discouraged as goto's can disrupt code/stack/heap.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Rob,

True, I didn't make any fundamental changes to what you had.  I just reorganized it a bit.

To answer your questions:

- Did you measure the effect of the code changes on the footprint (per object) ?
- No, I haven't measured the footprint.  I imagine per object it should increase only by the sizeof(pin) (plus maybe some padding).  The program text size may have increased some as well, but again I haven't measured.

- Are you familiar with - http://playground.arduino.cc/Main/DHTLib - which supports the DHT22 too (and DHT21 under test) ?
  This is my preferred lib for DHT sensors, and I still want to rewrite it in OO with a base class and 3 derived classes.
- No, I wasn't familiar with it -- thanks for the pointer.  I took a stab at refactoring that version to the OO version that you mentioned, combined with the changes I introduced in the old version.  You can find the new version at:  https://github.com/adalton/arduino/tree/master/projects/Dht_Library.  This version more exemplifies why I liked having the pin be part of the object (see the readSensor() function in the driver).

- can you add a link to the playground in your code?
- Yes, absolutely!  I actually intended to do that up front, but I made these changes late at night and it slipped my mind.  I've already pushed the update.

Final note, I see you used the goto statement to handle error conditions. Although you did it in a structured way to handle error conditions it is discouraged as goto's can disrupt code/stack/heap.
I'm not sure that I follow.  Is there some flaw in the compiler that causes it not to properly handle gotos?  From a structured programming perspective, neither goto nor having multiple returns is great, but I prefer this over multiple returns.

Thanks again!

Andy
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13471
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Quote
Quote from: robtillaart on Today at 09:37:06 am
Final note, I see you used the goto statement to handle error conditions. Although you did it in a structured way to handle error conditions it is discouraged as goto's can disrupt code/stack/heap.
I'm not sure that I follow.  Is there some flaw in the compiler that causes it not to properly handle gotos?  From a structured programming perspective, neither goto nor having multiple returns is great, but I prefer this over multiple returns.

Thanks again!

Andy
No the biggest problem is programmers working further from your code.
Goto's jump in the code and if they aren't understood well a minor change can easily break the logic.
 Yes that is also true for goto-less code but in my experience code with goto's often become spaghetti.
Maintainable code looks more like lasagne (layered architecture) smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just got started with Arduino this week, and my DHT11 just came in tonight. This one stumped me, primarily because everything talks about a sensor with four pins however mine only had three. I was trying the same layout 5v, Data, Ground without any luck. Turns out the three pin sensor is Data, 5v, Ground; unfortunately only ground is labeled on the sensor.

In case anyone runs into the same problem this article helped clear up the details for my sensor. http://www.geeetech.com/wiki/index.php/Electric_thermometer_by_using_DHT11_sensor_module

Thanks for the great library!
Logged

Pages: 1 2 [3] 4   Go Up
Jump to: