LCD displays extra characters as compared to serial monitor

I am using Arduino Nano as a transmitter and Arduino Mega 2560 as a receiver, each has HC-12 connected. I am trying to send an output from a MPU6050 (connected to the nano) to the LCD (connected to the mega). LCD displays the output but also displays extra characters. Meanwhile, the output in the serial monitor of Mega accurately displayed the output. I can't find any solutions to the problem, an i am hoping someone can help me. Thanks a lot.

The code for nano:

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

LiquidCrystal_I2C lcd(0x27, 20, 4);
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
MPU6050 mpu;

float thresholdX = 0.5; 

void setup() {
  Serial.begin(9600);
  HC12.begin(9600);
  lcd.begin(20, 4);
  lcd.backlight();
  mpu.initialize();
}

void loop() {
  accelerometer();
  delay(1000);
}

void accelerometer() {
  int16_t accelX, accelY, accelZ;
  mpu.getAcceleration(&accelX, &accelY, &accelZ);

  float accelX_g = accelX / 16384.0;
  float accelY_g = accelY / 16384.0;
  float accelZ_g = accelZ / 16384.0;

  if (abs(accelX_g) > thresholdX) {
    
    if (accelX_g > 0) {
      HC12.println("water");
    } else {
      HC12.println("earth");
    }
    delay(2000);
    }
  }

and the code for the Mega:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#define BYTELENGTH 10


LiquidCrystal_I2C lcd(0x27, 20, 4);
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);
  HC12.begin(9600);
  lcd.begin(20, 4);
  lcd.backlight();
}

void loop() {
  if (HC12.available()) {
    String dataString = HC12.readString();

    lcd.setCursor(0, 0);
    lcd.print(dataString);
    delay(2000);
    lcd.clear();
   
    Serial.println(dataString);
  }
}

this is the serial monitor in Mega

and this is the lcd
355179657_1955891294745596_7983743883716914153_n

it is the ln part. new line characters. you want to use print

ah yeah thanks, it's the ln in the nano. thank you

this will allow any endings, like new line, carriage return, or even Zero.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#define BYTELENGTH 10

LiquidCrystal_I2C lcd(0x27, 20, 4);
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);
  HC12.begin(9600);
  lcd.begin(20, 4);
  lcd.backlight();
}

void loop() {
  if (HC12.available() > 0) {
    char dataChar = HC12.read();
    if (dataChar >= ' ') {
      lcd.print(dataChar);
      Serial.println(dataChar);
    }
    else {
      while (HC12.available() > 0)HC12.read();
      lcd.clear();
    }
  }
}

Your title says: "LCD displays extra characters as compared to serial monitor".

You also had an 'extra character' in the serial monitor output as evidenced by the
extra blank line after each line of data.

Don

1 Like

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