LCD I2C Glitching on loop()

I have a LCD with a motor driver to make a simple line following robot
now the problem is that when I try to print the current state to the LCD it prints it but after some time it glitches and then comes back.
CODE:


#include <LiquidCrystal_I2C.h>

int enA = 8;
int in2 = 9;
// Motor B connections
int enB = 10;
int in4 = 11;

int L_I = A0;
int R_I = A1;

LiquidCrystal_I2C lcd(0x27, 16, 2);
String state = "Forward";

void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("W");
  delay(100);
  lcd.print("E");
  delay(100);
  lcd.print("L");
  delay(100);
  lcd.print("C");
  delay(100);
  lcd.print("O");
  delay(100);
  lcd.print("M");
  delay(100);
  lcd.print("E");
  delay(400);
  lcd.clear();
  lcd.print("Let's Go");
  delay(500);
  lcd.clear();
  // Set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in4, OUTPUT);

  // Turn off motors - Initial state
  digitalWrite(in2, LOW);
  digitalWrite(in4, LOW);
}

void loop() {
  bool left_input = (bool)digitalRead(L_I);
  bool right_input = (bool)digitalRead(R_I);

  Serial.println(left_input);
  Serial.println(right_input);

  if (left_input == 1 && right_input == 0) {
    turnLeft();
  } else if (left_input == 0 && right_input == 1) {
    turnRight();
  } else {
    forward();
  }
}

void forward() {
  if (state != "Forward") {
    state = "Forward";
    Serial.println(millis());
    lcd.clear();
    delay(40);
    lcd.print(state);
  }
  digitalWrite(in2, HIGH);
  digitalWrite(in4, HIGH);
  digitalWrite(enA, HIGH);
  digitalWrite(enB, HIGH);
}

void turnLeft() {  //turnLeft
  if (state != "Left") {
    state = "Left";
    Serial.println(millis());
    delay(40);
    lcd.clear();
    delay(40);
    lcd.print(state);
  }
  digitalWrite(in2, HIGH);  //Right Motor backword Pin
  analogWrite(in4, 100);    //Left Motor forword Pin
  digitalWrite(enA, HIGH);
  digitalWrite(enB, LOW);
}

void turnRight() {  //turnRight
  if (state != "Right") {
    state = "Right";
    Serial.println(millis());
    lcd.clear();
    delay(40);
    lcd.print(state);
  }
  analogWrite(in2, 100);    //Right Motor backword Pin
  digitalWrite(in4, HIGH);  //Left Motor forword Pin
  digitalWrite(enA, LOW);
  digitalWrite(enB, HIGH);
}

// This function lets you control speed of the motors
void speedControl() {
  // Turn on motors
  digitalWrite(in2, HIGH);
  digitalWrite(in4, HIGH);

  // Accelerate from zero to maximum speed
  for (int i = 0; i < 256; i++) {
    analogWrite(enA, i);
    analogWrite(enB, i);
    delay(20);
  }

  // Decelerate from maximum speed to zero
  for (int i = 255; i >= 0; --i) {
    analogWrite(enA, i);
    analogWrite(enB, i);
    delay(20);
  }

  // Now turn off motors
  digitalWrite(in2, LOW);
  digitalWrite(in4, LOW);
}

now I think the issue is either voltage or EMF interference.

Why are you delaying for 40mS after you clear the LCD? That will cause the LCD to appear to be blinking.

    lcd.clear();
    delay(40);
    lcd.print(state);

You don't really need to clear the display, just set the cursor position and overwrite the previous text (adding enough spaces to the end of the shorter texts so that you will fully overwrite the longest text).

Blinking is not the problem.The problem is that some glitched chars appear at random

Post a picture of the hardware used showing the wiring.

So you think the problem is hardware related, not software, but provide us with the code, and no schematic, or photo?

I think the mess of wires is a problem.

And the voltage maybe the problem too.
It consumes 400mA with 3 9v batteries in ||.

All of the above, plus no schematic, means you need a crystal ball, or help from a higher authority.
First priority, a proper schematic. Pen on paper, take a picture, no CAD software needed. Show us what's connected, how, label all parts.

Does the problem go away if you disconnect the motors?

Can you be a bit more inclusive with this schematic ?

Show us all the connections.

I moved your topic to an appropriate forum category @divyanshundley.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

I now changed the whole code and it works fine

When the motor is connected then the problem is arised;

A diode in parallel did the job.
Between the two terminals of the two motor I placed one diode each.

1 Like

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