Printing temp sensor data to serLCD

I have had the hardest time getting the data from my DHT22 sensor to print on my lcd. I spent 2 days trying to get this :stuck_out_tongue:
I have a sparkfun serial lcd v2.5 with serial backpack (https://www.sparkfun.com/products/9067#comment-530fcbcf757b7fb65d8b4567) and am using the Temp Sensor here (https://www.sparkfun.com/products/10167). I the lcd on the rx pin and the temp data line on pin 2. No errors but it prints unreadable code to humans (Weird symbols).

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include <SoftwareSerial.h>
#include "DHT.h"

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

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

// Connect pin 1 (on the left) of the sensor to +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

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600); 
  Serial.write("DHTxx test!");
 
  dht.begin();
}

void loop() {
  // 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();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.write("Failed to read from DHT");
  } else {
    Serial.write((byte)dht.readHumidity()) ;
    Serial.write((byte)dht.readTemperature()) ;
  }
}

You are sending the data to the hardware serial port, which will show up on your Serial Monitor on the PC, unless your LCD unit is connected to pins 0 amd 1 on the Arduino. In your sketch, you import the SoftwareSerial library, but you never use it.

After uploading your sketch, bring up the Serial Monitor (Tools->Serial Monitor, or hit the little magnifying glass icon in the upper right of the Arduino IDE). You should see repeated output. Unfortunately, you will see incorrect values because you are reading into bytes.

Ok, I plugged the tx and tx to the one data line of the serial lcd and it printed temps on the lcd. Why do I need both if all I am doing is writing? And is it really that simple (or is there more)? I thought I had to do something like this: Serial.write(t)? Thanks

What does the sensor return when you read it? You have two lines that read the vaues into floats, and you do nothing with them. Then you read values into two bytes. I made the (possibly incorrect) assumption that the sensor returns a float, and if that is the case, reading a byte loses data.

If the LCD works as-is, and you have no problem uploading the sketch, then there is no problem with doing itthat way.

As for the garbage characters, try Serial.print(). If that doesn't work make sure of what sort of data you get back from the sensors, and convert to characters if necessary.

So I uploaded it and plugged my serLCD to TX and it prints them but it never stops and constantly gets readings so it basically becomes a lcd screen of death. you can't read it because it re-reads and prints every second? How do you control how it looks on the lcd? Like on the top bar first line it says "Temp: 23(degrees) CC" and the bottom bar first line it says "Humid: 33%"?Thank you so much :)

ilovearduinosomuch:
So I uploaded it and plugged my serLCD to TX and it prints them but it never stops and constantly gets readings so it basically becomes a lcd screen of death. you can’t read it because it re-reads and prints every second? How do you control how it looks on the lcd? Like on the top bar first line it says “Temp: 23(degrees) CC” and the bottom bar first line it says “Humid: 33%”?Thank you so much :slight_smile:

Well don’t read and print the values so often. See the example code that came with your IDE. Blink Without Delay will show you a good way to do something at any interval you want. Check the datasheet at https://www.sparkfun.com/datasheets/LCD/SerLCD_V2_5.PDF for information on sending cursor positioning command, clearing the screen, and so on.

But again, I urge you to print to the Serial Monitor and get that working first. Figure out what the sensor returns, get it printing whatever you want it to print. Then, and only then, attach the LCD and get IT working with a separate sketch. Once you can print stuff to the LCD, you will have both things working, but separately. Then all you need to do it to use both techniques in one sketch.