LCD 16x2, strange characters

Hello people, first than all, its awesome all this forum support for Arduino, im working with an LCD 16x2 and I'm doing some tests of my program but sometimes my LCD prints strange characters, sometimes it corrects by pushing the reset buttom but mostly of the time I need to disconect and conect the usb, do you know why? Its normal?

Start by posting how you connected it (and the rest of the project) and the code and libraries you use :slight_smile:

Hi,
This is usually a power supply problem or interference from switching other devices on and off.

LCD Information and example code we know works on THIS page

Thanks for your answers, I have an Arduino Mega 2560, I’m a SD card reader, keypad 4x4 and a LCD16x2, I follow the examples to conect them.

ICSP for the SD card reader, I followed this pic for the conection.

LCD 16x2 as the image, just I have them in pins 8 to 13 starting with RS,E,D4-D7, A to +5V with a resistor, K to ground.

keypad:
rows in pins 22, 23, 24, 25
cols in pins 26, 27, 28, 29

and this is my code, im testing because it’s not working at all yet, everything works fine, I want to know what’s making strange characters in my LCD that I need to conect and disconect it, I’m only using the USB for power supply, do I need more?

#include <LiquidCrystal.h>
#include <Keypad.h>
#include <SPI.h>
#include <SD.h>
char txt[150];
const int chipSelect = 4;
int i;
int ii;
int iii;
int v = ',';
const byte rows=4;
const byte cols=4;
int ledv= A2;
int ledr= A3;
char customKey;
char menu;

char keys[rows][cols]=  //crear el teclado como en fisico
{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowpins[rows]={22, 23, 24, 25};  //pines para las filas 1234
byte colpins[cols]={26, 27, 28, 29};  //pines para las columnas ABCD

//crea y nombra el teclado
Keypad customKeypad = Keypad(makeKeymap(keys),rowpins,colpins,rows,cols);
Keypad kpd = Keypad(makeKeymap(keys),rowpins,colpins,rows,cols);
LiquidCrystal lcd(8, 9, 10, 11, 12, 13); //inicializo lcd
//                rs,e,D4,D5,D6,D7
char pss1[4],pss2[4];
void setup()
{
  Serial.begin(9600);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  lcd.begin(16,2);
  analogWrite(ledv,LOW);
  analogWrite(ledr,LOW);

  //LCD: Saludo inicial
  lcd.begin(16, 2);
//  lcd.setCursor(6,0);
//  lcd.print("Anmo");
//  lcd.setCursor(2,1);
//  lcd.print("Technologies");
//  delay(3000);
//  lcd.clear();
//  delay(500);
//  lcd.setCursor(1,0);  
//  lcd.print("Buenas tardes");
//  lcd.setCursor(0,0);
//  delay(1500);
//  lcd.clear(); 


//Lectura del SD

 while(!Serial){
  ;
 }
 Serial.print("Initializing SD card...");
 if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
 }
 Serial.println("card initialized.");
 File dataFile = SD.open("pass.txt");

  // if the file is available, write to it:
  if (dataFile) {
    for (i = 0; i <= 150; i++)
      {
        txt[i] = dataFile.read();
        Serial.print(txt[i]);
        analogWrite(ledv,255);
      }
    }
  
  // if the file isn't open, pop up an error:
  else
  {
    Serial.println("error opening pass.txt");
  }

//  for (ii = 0; ii <= 150; ii++)
//  {
//    Serial.print(txt[ii]);
//        if (txt[ii] == v)
//        {
//          Serial.println("");
//          for (iii = 1; iii <= 4; iii++)
//          {
//            Serial.print(txt[ii+iii]);
//          }  
//        }
//  }
  delay(250);
  analogWrite(ledv,0);
  analogWrite(ledr,0);
}



void loop()
{  
  File dataFile = SD.open("pass.txt");
  int i=0,j=0,f=0,w=0,k=0;


    //pide que ingreses la contraseña
    digitalWrite(ledr,LOW);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("SISTEMABLOQUEADO");
    lcd.setCursor(6,1);
  
      for(i=1;i<=4;i++)
      {
        pss1[i]=kpd.waitForKey();
        lcd.print("*");
        delay(250);
      }
      
    lcd.clear();
    delay(100);
    lcd.setCursor(4,0);
    lcd.print("Buscando...");
    delay(1000);
    lcd.clear();
    //Lee el arreglo (txt)
    for (ii = 0; ii <= 150; ii++)
    {
      //Serial.println(txt[ii]);
      lcd.print(txt[ii]);
      delay(500);
      lcd.clear();
      //Si encuentra una coma en el txt
      if (txt[ii] == v)
      {
        lcd.clear();
        lcd.print("Coma encontrada");
        delay (400);
        lcd.clear();
        
        //Guarda los siguientes 4 valores del txt en el arreglo pass2[].
        for(iii = 1; iii <= 4; iii++)
        {
          pss2[iii] = txt[ii+iii];
        }

          lcd.clear();
          //Imprime pss1
          lcd.setCursor(0,0);
          lcd.print(pss1[1]);
          lcd.setCursor(1,0);
          lcd.print(pss1[2]);
          lcd.setCursor(3,0);
          lcd.print(pss1[3]);
          lcd.setCursor(4,0);
          lcd.print(pss1[4]);

          //Imprime pss2
          lcd.setCursor(0,1);
          lcd.print(pss2[1]);
          lcd.setCursor(1,1);
          lcd.print(pss2[2]);
          lcd.setCursor(3,1);
          lcd.print(pss2[3]);
          lcd.setCursor(4,1);
          lcd.print(pss2[4]);
          delay(2000);
        
        //Ciclo for para comparar las contraseñas, del txt con la introducida
        for(k=1;k<=4;k++)
        {
          //Compara las contraseñas, las del txt con la introducida, si coinciden los 4 caracteres suma 1 a w
          if(pss1[k]==pss2[k])
          {
            w++;
          }
          
          else
          {
            w = 0;
          }

        //compara la contraseña original vs la ingresada
        if(w==4)
        {
          lcd.clear();
          analogWrite(ledv,255);
          lcd.setCursor(2,0);
          lcd.print("PASS CORRECTO");
          lcd.setCursor(0,1);
          lcd.print("ACCESO PERMITIDO");
          delay(7000);
          lcd.clear();
          analogWrite(ledv,0);         
        }

        else
        {
          digitalWrite(ledr,HIGH);
          lcd.clear(); 
          lcd.setCursor(1,0);
          lcd.print("ERROR DE CODIGO");
          lcd.setCursor(0,1);
          lcd.print("INTENTE DE NUEVO");
          delay(2000);
        }
      }
    }
  }  
w = 0;
dataFile.close();
}