I’ve looked around and didn’t find anything directly related to this so I thought I would try posting. Sorry if it has been covered a bunch of times, I’ll claim newb.
I am trying to get the Serial LCD to display the temp and humidity from a DHT 22 and am having issues. I can get each to work independently with example sketches (from adafruit for the DHT 22 and sparkfun for the serial lcd) but when i try to combine them, I only get seemingly random numbers displayed for the temp and humidity. I think the issue is somehow related to not being able to convert the float to a string but a little help would be greatly appreciated if anyone has a minute. Thanks!!
Here is my code:
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 4 // 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);
// SparkFun Serial LCD example 2
// Format and display fake RPM and temperature data
// This sketch is for Arduino versions 1.0 and later
// If you're using an Arduino version older than 1.0, use
// the other example code available on the tutorial page.
// Use the softwareserial library to create a new "soft" serial port
// for the display. This prevents display corruption when uploading code.
#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)
char tempstring[10], hstring[10]; // create string arrays
void setup() {
dht.begin();
mySerial.begin(9600);
delay(500);
mySerial.write(254); // cursor to beginning of first line
mySerial.write(128);
// mySerial.write("DHTxx test!");
mySerial.write("TEMP: "); // clear display + legends
mySerial.write("HUM: ");
}
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 = (9*dht.readTemperature())/5+32;
sprintf(tempstring,"%4d",t); // create strings from the numbers
sprintf(hstring,"%4d",h); // right-justify to 4 spaces
mySerial.write(254); // cursor to 7th position on first line
mySerial.write(134);
mySerial.write(tempstring); // write out the t value
mySerial.write(254); // cursor to 7th position on second line
mySerial.write(198);
mySerial.write(hstring); // write out the h value
}