Serial LCD displays extra characters

Hi, I am using the sparkfun 16x2 serial enabled LCD (10097) and I can not figure out why am getting a series of dashed lines at the beginning of line two and a 1 at the end of line two.

An example of the display looks like this:
" Temp F "
"--- 90 F 1"

I know my sensor calibrations are off. these are just place holders until I can verify the actual sensors.
Thank you for any input you can give on the display!


```cpp
#include <SoftwareSerial.h>

// Define Software Serial: Pin 9 is TX, RX is not used
SoftwareSerial mySerial(8, 9); // RX, TX

// Define analog sensor pins
const int sensorPin1 = A0;
const int sensorPin2 = A1;
const int sensorPin3 = A2;
const int buttonPin = 7; // Assumes button on digital 7

int currentSensor = 1; 
int buttonState = 0;
int lastButtonState = 0;

void setup() {
  // Start Serial at 9600 baud
  mySerial.begin(9600);
  
  // Set up the button pin
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // Read the state of the button
  buttonState = digitalRead(buttonPin);

  // Check if the button is pressed
  if (buttonState != lastButtonState) {
    if (buttonState == LOW) { // LOW because of INPUT_PULLUP
      currentSensor++;
      if (currentSensor > 3) {
        currentSensor = 1;
      }
      mySerial.print(0x01); // Clear Screen command for serial LCDs
    }
    delay(50); // Debounce
  }
  lastButtonState = buttonState;

  int sensorValue = 0;
  String line1 = "";
  String line2 = "";
  

  switch (currentSensor) {
    case 1:
      sensorValue = map(analogRead(sensorPin1), 0, 1023, -15, 30);
      line1 = "     Boost!  ";
      line2 = String(sensorValue) + " psi     ";
      break;
    case 2:
      sensorValue = map(analogRead(sensorPin2), 0, 1023, -10, 250);
      line1 = "     Temp F  ";
      line2 = String(sensorValue) + " F      ";
      break;
    case 3:
      sensorValue = map(analogRead(sensorPin3), 0, 1023, 0, 100);
      line1 = "    Oil Press";
      line2 = String(sensorValue) + " psi     ";
      break;
  }

  // Send formatted data to serial
  mySerial.write(0xFE); // initiate LCD special command
  mySerial.write(0x80); // Move cursor command
  mySerial.write(static_cast<uint8_t>(0x00)); // Position 0
  mySerial.print(line1);
  mySerial.write(0xFE); // initiate LCD special command
  mySerial.write(0x80); // Move cursor command
  mySerial.write(static_cast<uint8_t>(0x16)); // Position 16
  mySerial.print(line2);
  
  delay(200); 
}

I checked the Sparkfun page and it is a little confusing. The example code is different from the github examples. It looks like the github examples are the correct way to program that device.
I think you also need to install that library at the Github link.

When you do this

mySerial.print(line1);

do this right after

 Serial.print("*");
 Serial.print(line1);
 Serial.println("*");

This will tell you if you are losing your mind or not… it will probably point to the extra commands your display needs.

You will have to

  Serial.begin(115200);

in setup() and use the serial monitor set to 115200 baud.

Do you have examples you are working from? Some of the code looks like you grabbed it from somewhere.

a7

1 Like
mySerial.print(0x01); // Clear Screen command for serial LCDs and it should be a **write** not a print

The clear screen (0x01) needs an 0xFE to be sent first and it needs to be a write not a print

This looks like previous values were left on the printing area... something like...

- 9000 F 1
-- 900 F 1
--- 90 F 1

Clear the printing area before printing new values.

1 Like

On a 16x2 LCD that should not be a problem at the beginning of the line, and the posted code has trailing spaces that would avoid any trailing characters at the end of the line.

The print(0x01) instead of write(0x01) pointed out by @jim-p would be a major problem, since print(0x01) would send the ASCII code which is 0x31.

1 Like

Just to be clear

      mySerial.write(0xFE);  // Initiate LCD special command
      mySerial.write(0x01);  // Clear Screen command for serial LCDs

not

mySerial.print(0x01); 
1 Like

Thanks! This seems to have fixed the issue with the errant characters. The back light on the LCD went out when I updated it and won't come back on, but at least this part is working now.

Thanks, I wasn't clearing the code correctly like jim-p pointed out.

Yes, I used some example code to get started on this project, It's probably a bad habit, but I'm just a little lazy sometimes.

You never turned it on. You should read the datasheet for the display and learn how to use it.

The default backlighting is full power. I would need to turn it down if I wanted, but it would revert back to full brightness if power is cycled.

The LED backlight control command is 0x80 followed by a byte command between 0 and 255.

The display also uses 0x80 to position the curser when placed after a 0xFE command.

I tried cycling power and then wrote a basic sketch to reset the display brightness. neither worked. I can jump power direct to the led and it works, but I think the driver in the LCD control module has failed on me.

With no base resistor between the pin and transistor base, isn't PB1 being asked to pass way too much current when it's set to be HIGH? Or am I missing something?

Good question. I would assume the there is an internal resistor in the LCD controller. The LED is PWM controlled, so I am assuming the controller is simply setting the default duty cycle at 100%.

I'm not that great with the programming as I'm fairly new to Arduino. Maybe someone here could figure out a way to read the LED PWM output back through the serial output. I think there is a way to connect another wire to a TX pin on the LCD controller.

Maybe you just have a bad solder connection. You could also replace the 2N3904 transistor.
They provide the source code on the github page. You could modify it and reprogram the 328 to do whatever you want.. You will need a USB-UART adapter to do that.

From the source code:

/* ----------------------------------------------------------
  setBacklight() is called from SpecialCommands(). It receives
  a backlight setting between 0 and 255. The backlight is set
  accordingly (via analogWrite()). Before exit the new backlight
  value is written to EEPROM.
  ----------------------------------------------------------*/
void setBacklight(uint8_t backlightSetting)
{
  analogWrite(BLPin, backlightSetting);
  EEPROM.write(LCD_BACKLIGHT_ADDRESS, backlightSetting);
}