Jump to content
L293D

LadyAda's DHT library + LCD

Recommended Posts

Using the DHT Lib and an LCD I am about to get correct values to display on the LCD for between 2 seconds and 10 seconds, then the characters disappear.

 

In the DHT library I had to change return NAN to either return 0 or return ERR to get it to work.

 

I also had to change cli() and sei() to noInterrupts() and interrupts() respectively.

 

I am controlling the contrast on the LCD using PWM fed through a capacitor.

 

I am using the MSP430g2452 chip in a Rev 1.5 Launchpad.

 

(I am at work at the moment, or I would post all the code.)

 

My question is pretty simple: In setup(), I have something like -

 

pinMode(14, OUTPUT);

analogWrite(14, 90);

 

Since I am doing the contrast control in the setup function, and not the loop, could something to do with the noInterrupts() be disabling the PWM doing the contrast control? I would just check this when I get home, but I am stuck here for another 3 hours...and it is driving me NUTS :-)

Share this post


Link to post
Share on other sites

Here is the .cpp for the library I am using. While polling the DHT11, interrupts are disabled, and then re-enabled at the end: (highlighted in red) - also remember I have modified the cli and sei calls to noIntterupts and interrupts otherwise it does not compile in the Energia IDE.

 

 

/* DHT library

 

MIT license

written by Adafruit Industries

*/

 

#include "DHT.h"

 

DHT::DHT(uint8_t pin, uint8_t type) {

_pin = pin;

_type = type;

firstreading = true;

}

 

void DHT::begin(void) {

// set up the pins!

pinMode(_pin, INPUT);

digitalWrite(_pin, HIGH);

_lastreadtime = 0;

}

 

//boolean S == Scale. True == Farenheit; False == Celcius

float DHT::readTemperature(bool S) {

float f;

 

if (read()) {

switch (_type) {

case DHT11:

f = data[2];

if(S)

f = convertCtoF(f);

 

return f;

case DHT22:

case DHT21:

f = data[2] & 0x7F;

f *= 256;

f += data[3];

f /= 10;

if (data[2] & 0x80)

f *= -1;

if(S)

f = convertCtoF(f);

 

return f;

}

}

Serial.print("Read fail");

return NAN;

}

 

float DHT::convertCtoF(float c) {

return c * 9 / 5 + 32;

}

 

float DHT::readHumidity(void) {

float f;

if (read()) {

switch (_type) {

case DHT11:

f = data[0];

return f;

case DHT22:

case DHT21:

f = data[0];

f *= 256;

f += data[1];

f /= 10;

return f;

}

}

Serial.print("Read fail");

return NAN;

}

 

 

boolean DHT::read(void) {

uint8_t laststate = HIGH;

uint8_t counter = 0;

uint8_t j = 0, i;

unsigned long currenttime;

 

// pull the pin high and wait 250 milliseconds

digitalWrite(_pin, HIGH);

delay(250);

 

currenttime = millis();

if (currenttime < _lastreadtime) {

// ie there was a rollover

_lastreadtime = 0;

}

if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {

return true; // return last correct measurement

//delay(2000 - (currenttime - _lastreadtime));

}

firstreading = false;

/*

Serial.print("Currtime: "); Serial.print(currenttime);

Serial.print(" Lasttime: "); Serial.print(_lastreadtime);

*/

_lastreadtime = millis();

 

data[0] = data[1] = data[2] = data[3] = data[4] = 0;

 

// now pull it low for ~20 milliseconds

pinMode(_pin, OUTPUT);

digitalWrite(_pin, LOW);

delay(20);

cli();

digitalWrite(_pin, HIGH);

delayMicroseconds(40);

pinMode(_pin, INPUT);

 

// read in timings

for ( i=0; i< MAXTIMINGS; i++) {

counter = 0;

while (digitalRead(_pin) == laststate) {

counter++;

delayMicroseconds(1);

if (counter == 255) {

break;

}

}

laststate = digitalRead(_pin);

 

if (counter == 255) break;

 

// ignore first 3 transitions

if ((i >= 4) && (i%2 == 0)) {

// shove each bit into the storage bytes

data[j/8] <<= 1;

if (counter > 6)

data[j/8] |= 1;

j++;

}

 

}

 

sei();

 

/*

Serial.println(j, DEC);

Serial.print(data[0], HEX); Serial.print(", ");

Serial.print(data[1], HEX); Serial.print(", ");

Serial.print(data[2], HEX); Serial.print(", ");

Serial.print(data[3], HEX); Serial.print(", ");

Serial.print(data[4], HEX); Serial.print(" =? ");

Serial.println(data[0] + data[1] + data[2] + data[3], HEX);

*/

 

// check we read 40 bits and that the checksum matches

if ((j >= 40) &&

(data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {

return true;

}

 

 

return false;

 

}

Share this post


Link to post
Share on other sites

Also - I just wrote this from scratch, but this should be identical to what I was running at home:

 

 

#include <LiquidCrystal.h>

#include <DHT.h>

 

 

#define DHTPIN 12 // what pin we're connected to

 

 

#define DHTTYPE DHT11

 

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal lcd(3,4,5,6,7,8);

 

void setup() {

pinMode(14, OUTPUT);

lcd.begin(16, 2);

analogWrite(14, 90);

dht.begin();

}

 

void loop() {

 

float h = dht.readHumidity();

float t = dht.readTemperature();

 

 

// check if returns are valid, if they are NaN (not a number) then something went wrong!

lcd.setCursor(0,0);

lcd.clear();

if (isnan(t) || isnan(h)) {

lcd.print("Bad reading");

} else {

lcd.print("RH: ");

lcd.print(h);

lcd.setCursor(0,1);

lcd.print("Temp: ");

lcd.print(t);

lcd.print(" *C");

 

}

delay(100);

}

Share this post


Link to post
Share on other sites

Did a little more searching on this forum and found the answer. Can't believe it was something so simple. In the library, the timing is off. In this section:

 

// read in timings

for ( i=0; i< MAXTIMINGS; i++) {

counter = 0;

while (digitalRead(_pin) == laststate) {

counter++;

delayMicroseconds(1);

if (counter == 255) {

break;

}

 

 

 

I change delayMicroseconds(1); to delayMicroseconds(3); and it is perfectly stable.

 

Great forum. Thanks for the help. I hope to be able to contribute soon.

Share this post


Link to post
Share on other sites

I have not. I will take a look. I didn't mean to step on anybodies toes. I was just doing what bluehash asked me to do.

 

Your setup looks cool. I will check it out.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×