DHT22 with serial enabled LCD

I've been trying to use the code that allows you to use a serial enabled LCD to show the temperature and humidity percentage from a DHT22 sensor.

When I upload the code and power it on the screen I get random characters with 'Temp and CRH' among them. The actual temperature and humdity don't however show, so I've done something wrong although I can't figure out what.

Here is the code I used and I was hoping someone could let me know where I've gone wrong:

//Libraries
#include <DHT.h>;
#include <SoftwareSerial.h>

// Attach the serial display's RX line to digital pin 2
SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused)

//Constants
#define DHTPIN 7       // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino


//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{
    mySerial.begin(9600);
    delay(500);
	dht.begin();

}

void loop()
{
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp= dht.readTemperature();
    //Print temp and humidity values to serial monitor
    mySerial.write("Humidity: ");
    mySerial.write(hum);
    mySerial.write(" %, Temp: ");
    mySerial.write(temp);
    mySerial.write(" Celsius");
    delay(2000); //Delay 2 sec.
}

Thanks.

Did you observe the control code protocol for the display?

Please forgive my ignorance but can you explain what that means?

That it understands and interprets cursor positioning codes, like line feed, carriage return and so on...

Please post a link or some complete information about your display module.

I've just had a look and it's this LCD I think:

Have you read the manual?

No but I will do so now that you've suggested doing so. Thanks.

(deleted)

spycatcher2k:
You bought it, post a link to where you bought it from.

I have to ask why not? do you know the screen that well you don't need it?

Basics :
If you get a new piece of kit, read the manual/Datasheet and understand what you have read.

If there is a library for it, try the examples and read them, again understand what you have read.

If you need an item and not sure what to get, browse the usual sites, get the manuals/Datasheets, read .etc.

Electronics is 80% learning, 10% blowing stuff up :wink: and 10% actually making stuff by my reckoning.

I naively assumed that all serial enabled LCDs worked the same but obviously that doesn't make much sense thinking about it. It just didn't occur to me but since it was pointed out I've been reading it and I haven't had much success yet but I'll go over it again to see how I get on.

I still can't get it to work! It comes up with the humidity and temperature text but leaves out the actual humidity and temperature.

I still can't get it to work!

Geez, it's such a simple process. Open the sketch in the IDE. Use the Edit menu, and select the Select all menu item. Then, use the Edit + Copy menu item to copy the sketch.

Come back here, and use the </> icon on the top row to generate code tags. Paste your code between the tags.

Here it is:

//Libraries
#include <DHT.h>;
#include <SoftwareSerial.h>

// Attach the serial display's RX line to digital pin 2
SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused)

//Constants
#define DHTPIN 7       // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino


//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{
    mySerial.begin(9600);
    delay(500);
	dht.begin();

}

void loop()
{
    
    hum = dht.readHumidity();
    temp= dht.readTemperature();
    //Print temp and humidity values to serial monitor
    mySerial.write(254);//move cursor to beginning of first line
    mySerial.write(128);
    //Read data and store it to variables hum and temp
    mySerial.write("                "); // clear display
    mySerial.write("                ");
    mySerial.write(254); // move cursor to beginning of first line
    mySerial.write(128);
    mySerial.write("Humidity: ");
    mySerial.write(hum);
    mySerial.write(" %, Temp: ");
    mySerial.write(temp);
    mySerial.write(" Celsius");
    delay(2000); //Delay 2 sec.
    mySerial.write(hum);

The write() method is for sending binary data. I doubt that the mySerial device you have attached (I thought you said you had an LCD attached) understands how to deal with the binary value of the float.

Naming the instance LCD would be a far better idea. Using the print() method to send floats would be a better idea.

Thank you so much, it seems to be working now! I really appreciate your help :slight_smile: