Notifications
Clear all

setting up a temp sensor with an i2c 16x2 lcd

12 Posts
4 Users
1 Likes
4,550 Views
Pakabol
(@pakabol)
Member
Joined: 5 years ago
Posts: 232
Topic starter  

hello all this morning iwoke up and started playing with the DHT22 and ive got it working and all 

64972811 363030304356679 517162011453292544 n

but when i went in and added the lcd screen im getting about 80% correct then 20% garbage 

65500898 637611966756072 2836324324137238528 n

im wondering if its the adafruit lib. the dont have a degree symbol  and thats what is causing it or if its in my pasted together code   

 

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
lcd.begin(16,2);
dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}

// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));

lcd.print(F("Humidity: "));
lcd.print(h);
delay(4000);
lcd.print(F("% Temp: "));
lcd.print(t);
lcd.print(F("°C "));
lcd.print(F("°F H.I.: "));
lcd.print(hic);
lcd.print(F("°C "));
lcd.print(hif);
lcd.println(F("°F"));

}

 

 

thanks again i have a bit of yard work to do so i thought i would toss this out while i have non fun stuff todo 🙂


   
Quote
Pakabol
(@pakabol)
Member
Joined: 5 years ago
Posts: 232
Topic starter  

also i would love it if the text would scroll or a way to get it to post only one of the humi. temp. and heat indexes singly   

so very new at this still 

thank you for your patience

Jon 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

What seems to be missing is that you must set the cursor before sending the print command as in the function snippet below.

Notice that the LCD is always cleared before new data is written. This to stop the text being appended to the previous text displayed. 

void updateDisplay(String str_temp, String str_humid){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: "); lcd.print(str_temp); lcd.print("C"); //lcd.print(fHeit(temperature)); lcd.print("F");
lcd.setCursor(0, 1);
lcd.print("Hum: "); lcd.print(str_humid); lcd.print("%");
}

Steve

   
Pakabol reacted
ReplyQuote
(@dronebot-workshop)
Workshop Guru Admin
Joined: 5 years ago
Posts: 1081
 

Steve has the correct solution, his code has the two elements you're missing:

  1. The display needs to be cleared before every update. Otherwise, characters from the previous reading will stay on the display, causing oddball readings.
  2. You need to set a cursor to the beginning of the first line, write to the display and then reposition the cursor on the next line and then write that line.

In your example, the text is randomly placed onto the display and is not erased between readings.  So that's why you have junk on your display.

I'm also a bit unsure why you have a 4-second delay inserted between printing the humidity and printing the temperature.  Is there a reason for that?

Bill

 

 

"Never trust a computer you can’t throw out a window." — Steve Wozniak


   
ReplyQuote
Pakabol
(@pakabol)
Member
Joined: 5 years ago
Posts: 232
Topic starter  

thank you very much steve ill give that a try 


   
ReplyQuote
Pakabol
(@pakabol)
Member
Joined: 5 years ago
Posts: 232
Topic starter  

 

I'm also a bit unsure why you have a 4-second delay inserted between printing the humidity and printing the temperature.  Is there a reason for that?

Bill

 

 

i was just playing around testing stuff out.

Jon 


   
ReplyQuote
Pakabol
(@pakabol)
Member
Joined: 5 years ago
Posts: 232
Topic starter  

wanted to thank you all again its up and running

working

   
ReplyQuote
wintech
(@wintech)
Member
Joined: 5 years ago
Posts: 32
 

Really nice!!


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

Well done for getting up and running!

Now you need to think about improving the sketch. The first thing I would do is copy the working sketch to an archive and get rid of all the comment lines.

Then I would move all the DHT polling code to separate function so all you have in the loop() is a two second delay and a call to the function. And test that it is still working.

Like this:

void loop(){

delay(2000);

myFunction();

}

 

Then you need to get rid of the delay() function. Learn how to use the millis() function. The delay function is just there for amateurs/beginners. All it does is stop the microcontroller completely, where it could be doing something else.

And once you have mastered millis(), you could start looking at interrupts i.e. polling the DHT with a momentary button.

The reason I like to put the number crunching in functions and the control (loops and conditionals) code in the main loop is, that it is easier to debug and you can get at the functions for other sketches by just copy and paste.


   
ReplyQuote
Pakabol
(@pakabol)
Member
Joined: 5 years ago
Posts: 232
Topic starter  

thank you again for all this. im planing on building a weather station. so i have getting getting all the components and setting each one on their own to learn how they all operate.

you have given me so ideas on where to get started.

i live just out side St Louis and we get some great storms here so i just think it would a nice project 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

If that is the case I would start by buying a boat. ? 


   
ReplyQuote
Pakabol
(@pakabol)
Member
Joined: 5 years ago
Posts: 232
Topic starter  

yeah for real my friend had a river house on Mississippi (had) just out side elberry, MO and my brother lived in foley, MO and he lost almost everything and decided to move back to oregon  


   
ReplyQuote