LCD How to Parse Carraige Return LF

I am connecting my Arduino with a 16x2 LCD display to an external serial device. My device sends the following message:
RxFr 12345678 CR LF
TxFr 12345678
Using the sample code from the Arduinic Basic LCD page my display shows the following:

RxFr 12345678==T
Second line blank
The equals signs are actually 4 lines.
How can I get the display to not show the CR LF and also go to the second line?

/*
LiquidCrystal Library - Serial Input

Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.

This sketch displays text sent over the serial port
(e.g. from the Serial Monitor) on an attached LCD.

The circuit:

  • LCD RS pin to digital pin 12
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2
  • LCD R/W pin to ground
  • 10K resistor:
  • ends to +5V and ground
  • wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplay

*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(9600);
}

void loop() {
// 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());
}
}
}

Please edit your post and place your code inside code tags, as explained in the mandatory introduction that you read.

No CR, No LF
in liquidcrystal.h
you have to position the cursor

lcd.setCursor(col, row)

LiquidCrystal - setCursor() - Arduino Reference

Like so (untested)?

void loop()
{
   // 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)
      {
         char input = Serial.read();
         // display each character to the LCD
         if (input != '/n' && input != '/r')
         {
            lcd.write(input);
         }
      }
   }
}

Not sure what you are trying to do. Why clear the display so often?

The LiquidCrystal library will not do line wrap so you will have to handle that yourself. The hd44780 library has line wrap that can be enabled. That library is available via the library manager.

See how the code is posted in code tags and formatted? Isn't the code easier to read and copy?
Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Almost all of the LCD tutorials have the wiring of the contrast pot wrong. It is a mistake that has been perpetuated through the years. The right way is to wire the one end of the pot to ground and the wiper to LCD pin 3 (V0). The other end of the pot is left disconnected or tied to the wiper. So the pot is a variable resistor. Actually I find that a 1K fixed resistor from ground to Vo gives me satisfactory contrast on virtually every LCD that I have tried.

What is the basis for that statement? Here is how the data sheet recommends that I wire my HD44780-compatible(*) LCD:

Capture\

(*) Crystalfontz Model Number CFAH2004K-YYH-JP#

Hard to argue with that.

@Paul_B ?

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(10);
    // clear the screen
    lcd.clear();
    uint32_t Number = 0;
    // read all the available characters
    while (Serial.available() > 0) {
      char Symbol = 0;
      if (Serial.available())Symbol = Serial.read();

      // display each character to the LCD
      if (Symbol > 31 && Symbol <= 127) lcd.write(Symbol);

    }
  }
  delay(1000);
}

what? show sketch from sender.
And are you sure you need to send data as decimal digit through UART?

Should that not be &&? 130 is > than 31 so the if would be true.

1 Like

I guess that that should be '\n' and '\r' :wink:

1 Like

Yes, oops. :crazy_face:

From the datasheet indeed.

A datasheet which omits to show the circuit of their module.

No, you already explained it very nicely.

So, perhaps we need the datasheet for the controller chip.

What do we find? Oh yes! What do you know? :face_with_raised_eyebrow:
Selection_073
Where's the potentiometer?

Problem is - "Crystalfontz" - well, do not manufacture anything. Just a retailer.

Crystalfontz America is the leading supplier of LCD, TFT, OLED and ePaper display modules and accessories.

Hardly authoritative in any regard. :roll_eyes: Albeit, a small step up on Aliexpress in general.

OK, so you invited me here. Sadly I have insufficient spare time to otherwise monitor this forum. :thinking:

Page 7 of 19 --
Interfacing an HD44780 2-line LCD display with the STM8SVLDISCOVERY

Yes, as I continue to say and @groundFungus has understood ...

This comes from engineers - yes engineers - blindly copying from previous designs again and again without bothering to understand how the pieces they are using, actually work.

This ten year old document is just another example, the STMicroelectronics team grabbed a generic "1602" module, basically the same as the ones we still get to this day from Aliexpress, copied whatever information they had on the module at that time and wrote an Application Note. They did not manufacture or design the module, they just used it because it was easy.

It's the classic case of saying "it must be right because I've been doing it that way all my life!" :roll_eyes:

It's that way on the Hitachi documents, too.

Which?


(Page 37)

V5 = Vo (aka VEE) does it?

If the 'offensive' pot, with regard to a complete module, is wired as a pot (with the wiper at V0 (VEE) and one end at +5 and the other at GND), what's wrong with that?
Is it basically a personal objection?

image
Scans-00119604.pdf (153.9 KB)

No, it is something generally called "engineering". :grin:

To quote:

The term engineering is derived from the Latin ingenium, meaning "cleverness" and ingeniare, meaning "to contrive, devise".

The implication is that you wish to do things in the most proper way ("optimum", most efficient).


There was a major mistake early on in the application of the HD44780 LCD driver chip where a designer copied something from a test circuit in the chip documentation where a potentiometer was used to set the contrast voltage. In fact, I have not been able to locate this misleading circuit, it is not in the present datasheet to hand which shows only the correct configuration (in figure 21, page 37) of a variable resistor in series with the negative supply to the contrast ladder. You may note that the negative 5 V supply is only required for the uncommon "extended temperature" version of LED displays, for almost all readily available, the contrast voltage is slightly less that 5 V and the resistor connects to ground.

Nevertheless, once made, this blunder became ingrained in almost all following designs and "instructional" literature, since few designers actually understood the magical workings of a multi-level multiplexed LCD - or cared to spend the time to do so.

The popular modules - the "1602", the "2004" and other variants - have the resistor ladder shown in figure 21, consisting of five 2k2 ("222") resistors "R1" to "R5", totalling 11k. R6 or "RF" is the clock oscillator of the HD44780 while R7 is 0 in the 5 V versions. R8, usually "101" as 100 Ohms is the LED resistor.


The usual optimum contrast voltage - the voltage on the ladder between Vdd and Vo is between 4.5 and 4.8 V, corresponding to between 0.2 and 0.5 V on Vo. This then is most readily set by a resistance between 200 Ohms and 1.2k - if the supply voltage is actually 5 V - as this resistance becomes the correct proportion of the resistor ladder.

With the incorrectly wired 10k potentiometer, this resistance would be set with the potentiometer in the first (ground) tenth of its range (so ten turn potentiometers are often used) however the additional part of the 10k connected to 5 V draws an additional and completely undesirable 500 µA or so requiring the potentiometer to be set within the first twentieth of its range.

This is an absurd situation; if you must use a 10k pot, then it should never be connected to 5 V; the "free" end may be connected to the wiper or even to the ground end as this actually reduces the value of the potentiometer and makes contrast setting easier.

The correct value of the potentiometer, wired as a variable resistor with wiper connected to one end is 1k or 2k. This spreads the usable contrast setting to the whole range of the potentiometer.

And why the additional 500 µA is particularly undesirable is where you are powering from a battery. While the backlight will draw about 25 mA (for the "1602") but can be turned off when not required, the LCD driver and contrast ladder draw less than a milliamp, so unnecessarily adding 500 µA is quite absurd.

Right, "engineering" vs. the evil forces of heresy, delusion, conspiracy.
The Hitachi document (post18) has the "accursed thing" - the affront to 'engineering' -- a '10k-20k pot'.
If you don't like it, then don't use it.
Where was it established that everything here (or anywhere) must be boiled down to wringing the very last out of a blasted battery?