16x2 LCD Serial communication Showing strange character

when i send any character my lcd prints it but one strange character (4 horizontal lines) appears after that. what is the problem in code pls correct me.

#include <LiquidCrystal.h>


const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
 
  lcd.begin(16, 2);
 
  lcd.print("hello, world!");
  Serial.begin(9600);
  lcd.setCursor(0, 1);
}

void loop() {
  
  if(Serial.available()>0)
  {
   
   lcd.write(Serial.read());
  }
}

a)
please edit your post and post your code in code tags

b)
I assume you close your serial input with a linefeed and/or Carriage return.
These might lead to the lines.

either don't send linefeed/Carriage return or just don't print non printable characters

if(Serial.available()>0)
{
  char c = Serial.read();
  if (c > 0x20) lcd.print(c);
}

untested.

1 Like

thanks friend. it worked.
but can u explain me this "if (c > 0x20)". why did it solved my problem.

google for "ASCII Table".

You will see that the lowest printable character is the blank. it is dec 32 or 0x20 in hex

and now I see, you should use

c >= 0x20)

to include also the blanks. As alternative you could write

c >= ' ')

that will do the same.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.