Can't get serial to display on LCD

Discussion in 'The Projects Forum' started by FlyingCow, Feb 7, 2018.

  1. FlyingCow

    Thread Starter Member

    Dec 30, 2017
    72
    9

    I'll look that up and see if it will get me where I want to go. Thanks!
     
  2. FlyingCow

    Thread Starter Member

    Dec 30, 2017
    72
    9
    The custom characters sketch just calls for it to display and print all keycodes. So I don't think it is really looking for data on the bus, it is just printing from the program files.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,645
    2,925
    I don't know what this means but you shouldn't have to worry about custom characters right now.


    Look at the tutorial I referenced. It should include sending the value to the display.
     
  4. FlyingCow

    Thread Starter Member

    Dec 30, 2017
    72
    9

    No Sir Spinnaker, That tutorial only prints to the serial monitor, no where else. I can do that all day long. I can't get it from there to the LCD.
     
  5. FlyingCow

    Thread Starter Member

    Dec 30, 2017
    72
    9
    I am not worried about custom characters. I am saying I can print all kinds of custom characters to the LCD by using that sketch. It works! I can't send characters from the the DHT senser to the LCD but I can send them to the serial monitor. I know they are on the bus, I just don't know how to get them and send them to the LCD. I am currently looking at a sketch Mark Hughes wrote for the HC12. He had buffers in it and so I am in the process of trying to add those to my sketch.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,645
    2,925
    Take a look at this thread
    http://forum.arduino.cc/index.php?topic=125946.0

    Look at DeepZ's answer. Instead of printing to the serial port in a loop. You would do something like this.

    f loat F = 1.25; // Get your sensor value here
    char buffer[16];

    dtostrf(F,5,2,buffer);
    lcd.print("Temp : ");
    lcd.println(buffer);

    If that doesn't work and it over writes the display


    float F = 1.25;
    char buffer[16];
    char buffer2[16];

    dtostrf(F,5,2,buffer);
    sprintf(buffer2,"Float : %s",buffer);
    lcd.println(buffer2);
     
  7. FlyingCow

    Thread Starter Member

    Dec 30, 2017
    72
    9
    Thanks Spinnaker,
    I'll give that a try first thing tomorrow. I'm gonna call it a day as I got this feeling my brain is saturated for now. I have tried a lot of stuff today without any luck. What you are talking about sounds like what I need to do. I was looking at Mark's sketch, but I am having a difficult time implementing it in my own sketch. What you are saying is much less involved. Since I only have a small amount of data, I would hope I can keep it extremely basic. I'll let you know when I get something done with your suggestion. Have a great evening!
    Mitch
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    6,645
    2,925
    Best thing to do is start small. Don't worry about the sensor for now. Just write something that displays the value of a float variable on the LCD. You can use the snippet I posted to get things started.
     
  9. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,647
    317
    Your using something a little different library wise.
    but here is with a lcd reading temp and Humidity.

    Code (Text):
    1. #include <Wire.h>
    2. #include <LiquidCrystal_I2C.h>
    3. #include "DHT.h"
    4. #define DHTPIN 2     // what digital pin we're connected to
    5. // Uncomment whatever type you're using!
    6. #define DHTTYPE DHT11   // DHT 11
    7. //#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
    8. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
    9. LiquidCrystal_I2C lcd(0x27,20,4);// set the LCD address to 0x27 for a 20 chars and 4 line display
    10. DHT dht(DHTPIN, DHTTYPE);
    11. //LiquidCrystal_I2C lcd(0x38, BACKLIGHT_PIN, POSITIVE);  // Set the LCD I2C address
    12. void setup()
    13. {
    14.   lcd.begin(16,2);               // initialize the lcd
    15.  
    16.   lcd.home ();                   // go home
    17.   lcd.print("Humidity Temp ");
    18.   lcd.setCursor ( 0, 1 );        // go to the next line
    19.   dht.begin();
    20.   delay ( 1000 );
    21. }
    22. void loop()
    23. {
    24. // Wait a few seconds between measurements.
    25.   delay(2000);
    26.  
    27.  
    28.   int x = dht.readHumidity();
    29.   int t = dht.readTemperature();
    30.   lcd.setCursor ( 1, 1 );
    31.   lcd.print(x);
    32.   lcd.setCursor ( 10, 1 );
    33.   lcd.print(t);
    34. }
    35.  
     
    Last edited: Feb 11, 2018
  10. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,647
    317
    I changed the code it should work with what you have and you can then mold it to what you want
    This convert Celsius to Fahrenheit

    Code (Text):
    1.  
    2. #include <Wire.h>
    3. #include <LiquidCrystal_I2C.h>
    4. #include "DHT.h"
    5. #define DHTPIN 2     // what digital pin we're connected to
    6. // Uncomment whatever type you're using!
    7. #define DHTTYPE DHT11   // DHT 11
    8. //#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
    9. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
    10. LiquidCrystal_I2C lcd(0x38);  // Set the LCD I2C address
    11. DHT dht(DHTPIN, DHTTYPE);
    12.  
    13. void setup()
    14. {
    15.   lcd.begin(16,2);               // initialize the lcd
    16.  
    17.   lcd.home ();                   // go home
    18.   lcd.print("Humidity Temp ");
    19.   lcd.setCursor ( 0, 1 );        // go to the next line
    20.   dht.begin();
    21.   delay ( 1000 );
    22. }
    23. void loop()
    24. {
    25.  // Wait a few seconds between measurements.
    26.   delay(2000);
    27.  
    28.  
    29.   int x = dht.readHumidity();
    30.   int y = dht.readTemperature();
    31.   int t = 0;
    32.   t = (y * 9 /5 + 32);
    33.   lcd.setCursor ( 1, 1 );
    34.   lcd.print(x);
    35.   lcd.setCursor ( 10, 1 );
    36.   lcd.print(t);
    37. }
    38.  
    39.  
     
    Last edited: Feb 11, 2018
  11. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,647
    317
    This give a little better look I couldn't fine my 4 line LCD
    But it will work with yours you can change lines. to match what you want.
    Code (Text):
    1. #include <Wire.h>
    2. #include <LiquidCrystal_I2C.h>
    3. #include "DHT.h"
    4. #define DHTPIN 2     // what digital pin we're connected to
    5. // Uncomment whatever type you're using!
    6. #define DHTTYPE DHT11   // DHT 11
    7. //#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
    8. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
    9. LiquidCrystal_I2C lcd(0x38);  // Set the LCD I2C address
    10. DHT dht(DHTPIN, DHTTYPE);
    11.  
    12. void setup()
    13. {
    14.   lcd.begin(16,2);               // initialize the lcd
    15.  
    16.   lcd.home ();                   // go home
    17.   lcd.print("Humidity Temp ");
    18.   lcd.setCursor ( 0, 1 );        // go to the next line
    19.   dht.begin();
    20.   delay ( 1000 );
    21. }
    22. void loop()
    23. {
    24. // Wait a few seconds between measurements.
    25.   delay(2000);
    26.  
    27.  
    28.   int x = dht.readHumidity();
    29.   int y = dht.readTemperature();
    30.   int t = 0;
    31.   t = (y * 9 /5 + 32);
    32.   lcd.setCursor(0, 1);
    33.   lcd.print("H%");
    34.   lcd.setCursor(3, 1);
    35.   lcd.print(x);
    36.   lcd.setCursor(7,1);
    37.   lcd.print("F%");
    38.   lcd.setCursor(10, 1);
    39.   lcd.print(t);
    40. }
    41.  
    LcdBackpack.jpg
     
  12. ebeowulf17

    Well-Known Member

    Aug 12, 2014
    2,125
    358
    I'm still not clear on what you're expecting to have happen here. Do you want to be able to type things into your terminal / serial monitor on your computer and have them show up on the LCD, or are you just trying to get the DHT values to print?

    If you're wanting the section of code below to print values from the DHT sensor to your LCD screen, then you need to get rid of the IF and WHILE conditions. As it is currently written, your code is actively preventing you from seeing DHT values.

    You don't need to do anything with Serial to get DHT data. You're getting that already, earlier in the code, with "... = dht.read..."

    If you're trying to get the LCD to display messages you've sent the Arduino by typing into your computer, then I would've expected your code to do a very jumbled version of that. The way I read it, any time there's serial communication available, your code would read just one character in, write it to the LCD, and then immediately print H, T, and F on the LCD also. It would then loop back around, get just one more character, then print H, T, and F values again, etc. So, if the goal is displaying serial terminal communication, you should eliminate the six lcd.print commands right after the lcd.write command.

    One last note, I'm not entirely clear on when lcd.write is preferred over lcd.print, but my cursory reading on the subject makes me think you should use lcd.print. There's a slim chance that lcd.write is causing odd behavior.

    Code (Text):
    1. if (Serial.available()) {
    2. // wait a bit for the entire message to arrive
    3. delay(200);
    4. // clear the screen
    5. lcd.clear();
    6. // read all the available characters
    7. while (Serial.available() > 0) {
    8. // display each character to the LCD
    9. lcd.write(Serial.read());
    10. // Wait a few seconds between measurements
    11.  
    12. lcd.print("H: ");
    13. lcd.print(h);
    14. lcd.print(" %\t");
    15. lcd.print("T: ");
    16. lcd.print(f);
    17. lcd.println(" *F\t");
    18.  
    19. }
    20. }
    21.  
    22.  
    23.  
     
  13. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,647
    317
    You No I posted all this how to send data from serial to LCD how to print to a LCD

    lcd write() will return the number of bytes written, though reading that number is optional
    lcd print() will return the number of bytes written, though reading that number is optional

    Those make sense lol nope thats arduino tho.

    Now here what they do.
    lcd.print
    data: the data to print (char, byte, int, long, or string)

    lcd.write
    It writes text characters to write to the display
    Code (Text):
    1.  
    2. void setup()
    3. {
    4.   Serial.begin(9600);
    5. }
    6.  
    7. void loop()
    8. {
    9.   if (Serial.available()) {
    10.     lcd.write(Serial.read());
    11.   }
    12. }
    Then it's even more crazy they give example for using print that dosen't show how to use numbers int char floats bytes longs

    But shows text

    Print is for numbers 10 255 1023 etc
    write is for acii2 text 49 shows as a 1 and so on.
     
    Last edited: Feb 13, 2018
  14. ebeowulf17

    Well-Known Member

    Aug 12, 2014
    2,125
    358
    Sorry for any confusion. I intended to reply to the thread starter, not to you specifically, but I didn't make that clear. I know you know how to get this stuff done.

    I was just trying a different approach in explaining it to the thread starter... and also looking for a little clarification, as I really can't tell from earlier posts whether the thread starter wants the LCD to display Serial data, DHT data, or both.
     
Loading...