Can't get serial to display on LCD

Thread Starter

FlyingCow

Joined Dec 30, 2017
72
Newbie here trying to print from DHT sensor to I2C, 20x4 display. I am combining two library sketches DHT tester and liquid crystal I2C. I know things have to be in the correct order but I am having difficulty getting that part right. I have removed much of the code to pare it down to just temp *F and humidity%. I have tried to make the line less than 20 characters and have also tried to make the display two separate lines thinking I may have issues because I sent too much code to the display. I get no response on the LCD so far no matter which order I place things in the code. I currently can still get data on the serial monitor from the DHT sensor. Earlier, I moved things around and lost that ability, but have never gotten anything to display on the LCD. I have no significant experience with an LCD either. I can currently still manually input data to the LCD by typing into the serial monitor.
I can make the serial display sketch work as designed by manually inputting data into the serial monitor, but I want to take it a step further and have it display data as it becomes available on the serial bus. Any advice would be appreciated.

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

#include "DHT.h"

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

// 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);


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display


void setup() {
  Serial.begin(9600);
  Serial.println("DHT");

  dht.begin();

  lcd.init();  // initialize the lcd
  lcd.backlight();

}

void loop() {


  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(f)) {
  Serial.println("Failed to read from DHT sensor!");
  return;
  }

  Serial.print("H: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("T: ");
  Serial.print(f);
  Serial.println(" *F\t");


  // when characters arrive over the serial port...
  if (Serial.available()) {
  // wait a bit for the entire message to arrive
  delay(100);
  // clear the screen
  lcd.clear();
  // read all the available characters
  while (Serial.available() > 0) {
  // display each character to the LCD
  lcd.write(Serial.read());
  // Wait a few seconds between measurements.


  }
  }
}
Mod edit: code tags
 
Last edited by a moderator:

BobaMosfet

Joined Jul 1, 2009
806
My first question is-- did you adjust contrast on the LCD so you can even see characters?

My first observation is-- lower your serial baudrate from 9600 to 300. You can always turn it back up, but let's just make sure you're not signalling to fast to the LCD.
 

Thread Starter

FlyingCow

Joined Dec 30, 2017
72
My first question is-- did you adjust contrast on the LCD so you can even see characters?

My first observation is-- lower your serial baudrate from 9600 to 300. You can always turn it back up, but let's just make sure you're not signalling to fast to the LCD.
@BobaMosfet The contrast is set with a pot on the back of the LCD. I have it adjusted well when viewing data sent by typing into the serial monitor. I will set the baud rate to 300 and get back.
 

Thread Starter

FlyingCow

Joined Dec 30, 2017
72
@BobaMosfet I changed the baud rate to 300. I still get nothing on the LCD but now I get jiberish out at the serial monitor no matter what baud rate I use. I tried them all. But, nothing printing to the LCD.

I am concerned if I have used the correct language for what it is I want to accomplish? I had hoped so since I am using two working sketches but I am too new to know for sure.
 

BobaMosfet

Joined Jul 1, 2009
806
Okay, set serial back to 9600baud for your other stuff. I think what is in order at this point is, we need more info on the LCD- what is it (Model?). Most LCDs are not serial, but are parallel and you have to know how to talk to them, like HD44780.

If you can tell us what the LCD is, we can likely find a datasheet for it.
 

Thread Starter

FlyingCow

Joined Dec 30, 2017
72
Okay, set serial back to 9600baud for your other stuff. I think what is in order at this point is, we need more info on the LCD- what is it (Model?). Most LCDs are not serial, but are parallel and you have to know how to talk to them, like HD44780.

If you can tell us what the LCD is, we can likely find a datasheet for it.
The LCD is a sainsmart iic 2004. It is a few years old.
 

JohnInTX

Joined Jun 26, 2012
3,809
The LCD is a sainsmart iic 2004. It is a few years old.
According to the datasheet, the display has an I2C backpack. That means that you have to use a library that talks to the LCD in I2C, not serial async or parallel IO interface. Look in the library header file LiquidCrystal_I2C.h to see if it contains routines with I2C in the name and use those to talk to the LCD.

https://www.sainsmart.com/products/20x4-iic-i2c-twi-lcd-module

Good luck!

Edit: here's some more info. It looks like your function names match those in the example. Reading further, it looks like the test code you described.. nuts.
 

Attachments

Last edited:

Thread Starter

FlyingCow

Joined Dec 30, 2017
72
@JohnInTX I am looking at all this right now. I found a different page for Sainsmart and the code would not work. This may help a bunch.

Nope, the example sketch still does not work. I'll keep looking everything else over and see if I stumble across an answer. Thanks so much.
 
Last edited:

BobaMosfet

Joined Jul 1, 2009
806
Couple of questions-
  • Are you able to use the LCD with anything else, or have ever used it before?
  • Have you worked with i2c/twi before?
 

spinnaker

Joined Oct 29, 2009
7,815
What is this?

Serial.print("H: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("T: ");
Serial.print(f);
Serial.println(" *F\t");

Seems to me that you are writing data to the serial port meant for your IC2 connected LCD display.

Your sensor is on the serial port correct? But I am not familiar with Arduino . Does the IC2 library some how interface to the serial port library?

What is throwing me is this.

while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
// Wait a few seconds between measurements.

I can't figure out why you would be writing to something you are reading.
 
Last edited:

Thread Starter

FlyingCow

Joined Dec 30, 2017
72
OK folks. I have read and reread everything posted on here. So far I have to say I think I have more working than is in any of these tutorials. I can use the Liquid Crystal I2C library and everything works great. It will print the custom character sketch till the cows come home. It does the Hello world without any hiccups. I get all sort of stuff printing to the LCD screen appropriately. This thing runs the DHT and prints it flawlessly to the serial monitor. At the same time, I can enter text into the serial monitor and hit send and that text shows up on the LCD at the same time. I just can't get the DHT data to print to the LCD. I have even tried to add a few lines here and there in my sketch that I have seen in other sketches but it added no functionality to my sketch. I know I am missing something small that is important, but I just can't seem to find it. This is a small part of a bigger project so I need to figure this out in order to continue with the next step. All I need to do is write incoming data from a device to the LCD screen as it becomes available on the serial bus. I have yet to find an example of a sketch where someone has done this. I would hope people are doing more with their LCDs than just printing Hello World? I'm just too new to see the error in my ways at this point. Thanks for all the advice so far. I have checked it all out and I may have missed the boat so if that is so, please feel free to redirect me down the correct path.
Thanks!
@be80be have you got any inspiration? :)
 

spinnaker

Joined Oct 29, 2009
7,815
Why are you writing the value you read form the DHT to the serial port and no the LCD? What is connected to the serial port?

float f = dht.readTemperature(true);

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

Serial.print("H: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("T: ");
Serial.print(f);
Serial.println(" *F\t");
 

be80be

Joined Jul 5, 2008
1,930
You are going to run into big problems here The is I2C built into the Lcd libraries that ship with the newer arduino Ide.
You need to first get the LCD to work

If you installed there code you will have problems they used the same name LiquidCrystal that's the same as with the Arduino
Hell I don't get why people think they have something new going on.
That LCD is using a PCF8574 I2C I/O Bus Expander IC nothing fancy.

What you have done is installed a new LiquidCrystal now theres was changed to use there LCD it didn't need to be.
But Your code is pointing to LiquidCrystal
 

Thread Starter

FlyingCow

Joined Dec 30, 2017
72
@spinnaker I have two working sketches, one for DHT, one for I2C LCD. I want to put incoming data to the LCD. I have never done this so That may be where I am going wrong.

@be80be I have the LCD working but only with the sketches in the library. It does those fine. I am just confused at how I can't get the incoming data from the DHT to print to the LCD even though it prints to the serial monitor. If I load a sketch from the library, it writes to the LCD perfectly every time.
 

be80be

Joined Jul 5, 2008
1,930
I depends On what your using post your woking LCD code

But you just use lcd.print
save your read from the DHT

Code:
if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();

        // say what you got:
        lcd.setCursor ( 0, 1 );
        lcd.print(incomingByte, DEC);
    }
}
 

Thread Starter

FlyingCow

Joined Dec 30, 2017
72
@be80be
I'm not sure what you mean there. Here is the code I now have. I added a few lines that I saw in other sketches, but they did not help my problem. I haven't removed them yet to keep this as simple as possible.

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

#include "DHT.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2,27); // RX, TX

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

// 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);

int incomingByte = 0;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display


void setup() {
lcd.init(); // initialize the lcd
dht.begin();
Serial.begin(9600);
Serial.println("DHT");


// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(3,0);
//lcd.print("Hello, world!");
lcd.setCursor(2,1);
//lcd.print("Ywrobot Arduino!");
lcd.setCursor(0,2);
//lcd.print("Arduino LCM IIC 2004");
lcd.setCursor(2,3);
//lcd.print("Power By Ec-yuan!");






// lcd.init(); // initialize the lcd
// lcd.backlight();
// lcd.setCursor(0,0);
}

void loop() {

delay(2000);



if (Serial.available() > 0) // send data only when you receive data:

incomingByte = Serial.read(); // read the incoming byte:

// 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(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.print("H: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("T: ");
Serial.print(f);
Serial.println(" *F\t");

// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(200);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
// Wait a few seconds between measurements

lcd.print("H: ");
lcd.print(h);
lcd.print(" %\t");
lcd.print("T: ");
lcd.print(f);
lcd.println(" *F\t");



}
}
}
 

Thread Starter

FlyingCow

Joined Dec 30, 2017
72
@spinnaker
So how would you write it straight to the LCD? That would be great if I could do that. I don't have to use the info anywhere else, I just want to display it. Later on, I will need to use some of the input to trigger other things, but I just need to learn how to get a working display of my input right now.
 

spinnaker

Joined Oct 29, 2009
7,815
How are you writing to it now? I don't know your LCD library but if you can't write a formatted string, then you could write to a buffer then write the buffer.

Something like sprintf(buffer,"Value = %f",v); Lookup sprintf and see if Arduino supports it.
 
Top