Problems with LCD showing random characters

Hi, I've just started using the Arduino and this is the first project I've done on my own so I'm having a lot of problems, but now I have one with the LCD screen that I can't fix:
as I run the code the screen seems to work but then it starts showing random characters.
Putting the code on an online simulator it seems to work so probably the problem is in the wiring or something but I don't know what it can be (I've read that there can be noise problems but I don't understand what I have to do).
What I've tried so far is to use a 100uF capacitor (I don't have 10) on the first two pins of the lcd with no results and also to put all the cables better in the breadboard.
Below I upload the code but simplified, given that the problems are already in the first part and perhaps solved there everything is resolved. I apologize if some parts of the code are not in English.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int rossoPin = 10;
int verdePin = 6;
int bluPin = 9;
int a=16;  //intensità led
int timep=0; //timer precedente
unsigned long timer=0;
unsigned long tinizio;
unsigned long tora;
int tempo=240;
int err=0; //errori commessi

void setup(){
  Serial.begin(9600); 

  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("Premere i 4");
  lcd.setCursor(0,1);
  lcd.print("pulsanti insieme");
  pinMode(rossoPin, OUTPUT);
  pinMode(verdePin, OUTPUT);
  pinMode(bluPin, OUTPUT);
  pinMode(8, INPUT);
  pinMode(7, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  
}

void loop(){
  
  if(digitalRead(A1)==1&&digitalRead(A2)==1&&digitalRead(7)==1&&digitalRead(8)==1){
    
    Serial.println("inizio  ");
    tinizio=millis();
    lcd.clear();
    delayt(3500);
    sequenza();
    sequenza();
    sequenza();
    delayt(1000);

    Serial.println("FINE");
    
  
  }
}

//esegue la fese delle sequenze con la verifica dell'errore o del successo

void sequenza(){
  randomSeed(analogRead(0));
  int sq1[6];
  int i;
  creasq(sq1);
  Serial.print("  "); 
  writet();

  // mostra sq1
  int risposta=1;
  do{
      for (i=1;i<=sq1[0];i++){
        colore(sq1[i]); 
        if (digitalRead(sq1[5])==1){
          risposta=0;
          lcd.setCursor(0,0);
          lcd.print("CORRETTO!");
          rgb(0,127,0);
          delayt(1000);
          lcd.setCursor(0,0);
          lcd.print("         ");
          break;  
        }
        else if ((digitalRead(A1)==1||digitalRead(A2)==1||digitalRead(7)==1||digitalRead(8)==1)&&digitalRead(sq1[5])!=1){
          lcd.setCursor(0,0);
          lcd.print("ERRORE!");
          err++;
          rgb(127,0,0);
          tempo=tempo-30;
          delayt(1000);
          lcd.setCursor(0,0);
          rgb(0,0,0);
          lcd.print("       ");
        }
      };
    delayt(1000);
    rgb(0,0,0);
    if (digitalRead(sq1[5])==1){  
      risposta=0;
      lcd.setCursor(0,0);
      lcd.print("CORRETTO!");
      rgb(0,127,0);
    }
    else if ((digitalRead(A1)==1||digitalRead(A2)==1||digitalRead(7)==1||digitalRead(8)==1)&&digitalRead(sq1[5])!=1){
      lcd.setCursor(0,0);
      lcd.print("ERRORE!");
      err++;
      rgb(127,0,0);
      tempo=tempo-30;  
      }
    delayt(1000);
    lcd.setCursor(0,0);
    lcd.print("          ");
    rgb(0,0,0);
  }while(risposta==1);
  delayt(1000);
}

//crea sequenza colore con anche pulsante corretto

void creasq(int sq[]){
  int i,n,l=random(2)+3;
  sq[0]=l;
  for(i=1; i<=sq[0];i++){
    n= random(5);
    sq[i]=n;
    Serial.print(n);   
  }
  Serial.print("  "); 
  writet();
  if(l==3){
    if(sq[1]!=3&&sq[2]!=3&&sq[3]!=3) //non c’è il colore rosso
      sq[5]=A2;
    else if (sq[3]==0) //se l'ultimo colore è il bianco
      sq[5]=8;
    else if (sq[1]==2) //se c’è il colore viola
      sq[5]=A1;
    else if (sq[2]==2)
      sq[5]=A2;
    else if (sq[3]==2)
      sq[5]=7;
    else              //Altrimenti
      sq[5]=7;
  }
  if(l==4){
    int ros=0;        //più di una volta il colore rosso
    if(sq[1]==3)
      ros=ros+1;
    if(sq[2]==3)
      ros=ros+1;
    if(sq[3]==3)
      ros=ros+1;
    if(sq[4]==3)
      ros=ros+1;
    if(ros>1){
      if(sq[4]==3)
        sq[5]=8;
      else if (sq[3]==3)
        sq[5]=7;
      else if (sq[2]==3)
        sq[5]=A2;
    }
    else if (sq[4]==1&&sq[1]!=3&&sq[2]!=3&&sq[3]!=3) //se l'ultimo colore è il giallo e non c’è il colore rosso
      sq[5]=A1;
    else{
      int bl=0;       //se c'è esattamente una volta il blu
      if(sq[1]==4)
        bl=bl+1;
      if(sq[2]==4)
        bl=bl+1;
      if(sq[3]==4)
        bl=bl+1;
      if(sq[4]==4)
        bl=bl+1;
      if(bl==1)
        sq[5]=A2;
      else{
        int vi=0;     //se c'è più di una volta il colore viola
        if(sq[1]==2)
          vi=vi+1;
        if(sq[2]==2)
          vi=vi+1;
        if(sq[3]==2)
          vi=vi+1;
        if(sq[4]==2)
          vi=vi+1;
        if(vi>1)
          sq[5]=8;
        else          //Altrimenti
          sq[5]=A2;
      }
    }
  }
  Serial.print(sq[5]);
}

//colore per LED RGB

void colore(int col){ 
  switch(col){
  case 0: 
  rgb(a-14, a+24, a+16); //bianco
  delayt(1000);
  rgb(0, 0, 0);
  delayt(200);
  break;
  case 1: 
  rgb(a-14, a+16, 0); //giallo
  delayt(1000);
  rgb(0, 0, 0);
  delayt(200);
  break;
  case 2: 
  rgb(a-8, 0, a+16); //viola scuro
  delayt(1000);
  rgb(0, 0, 0);
  delayt(200);
  break;
  case 3: 
  rgb(a, 0, 0); //rosso
  delayt(1000);
  rgb(0, 0, 0);
  delayt(200);
  break;
  case 4: 
  rgb(0, 0, a); //blu
  delayt(1000); 
  rgb(0, 0, 0);
  delayt(200);
  break;
  }
}

//Procedura per settare il colore

void rgb(int rosso, int verde, int blu){
  analogWrite(rossoPin, rosso);
  analogWrite(verdePin, verde);
  analogWrite(bluPin, blu);
}

//Delay ma con timer (delayt(0) cambia solo il timer)

void delayt(int d){
  int i, x= d%800;
  d=d-x;
  for(i=0; i<d/800;i++){
      delay(800);       
      writet();
  }
  delay(x);
  writet();
}

//Scrive timer su lcd

void writet(){
  int s,m;
  tora=millis();
  timer=(tempo-(tora-tinizio)/1000);
  if (tempo-(tora-tinizio)/1000==0 || err>2){ //esplosione
      writet();
      rgb(255,0,0);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("KA-BOOOM");
      Serial.println("Esploso");
      delay(1000000);
  }
  if(timer!=timep){
    s=timer%60;
    m=(timer-s)/60;
    lcd.setCursor(12,1);
    lcd.print(m);
    lcd.setCursor(13,1);
    lcd.print(":");
    if (s<10){
      lcd.setCursor(14,1);
      lcd.print("0");
      lcd.setCursor(15,1);
      lcd.print(s);
    }
    else{
      lcd.setCursor(14,1);
      lcd.print(s);
    }
  }
  timep=timer;
  
  
}

In this part of the code the LED simply shows a sequence of colors and according to it one of the four buttons must be pressed (on the Serial monitor one can see what must be pressed). you have to do it three times before the timer drops to 0 and without making more than 2 errors (plus each error takes 30 seconds off the timer)

(Obviously if the code can be improved in general I accept suggestions)

This is how the Arduino is wired:

The picture is not enough, especially because pin 3 and 4 wires are hard to follow due to overlapping and they're the same colour...
Anyway, always start testing a problem getting rid of other things. In this case it means "connect just the LCD and try a simple code", just printing something on setup() and nothing more.
For example, try THIS tutorial, and see what happens. If you suspect it's a wiring problem, double check connections (are you sure your breadboard is ok?). Do it and let us know (together with new code, and a photo of the wirings).

Just a final note: with LCDs I always use I2C interface, it has lesser and easier wirings. You can buy an I2C backpack and solder it to the LCD, then install LiquidCrystal_I2C library.

PS: non preoccuparti per i commenti, sono italiano anche io :wink:

Thanks for the reply! I forgot to mention that I already tried all the examples in the LCD library ("Autoscroll", "Blink"...) and the screen worked perfectly, also using the same wiring as the pins are the same . The breadboard should work, because I'm using a semi-new "starter kit" (semi-because it's actually been sitting closed gathering dust for a while). For these reasons I was thinking there might be issues with me using too many things at once or with the LED as I know when using motors there can be issues with LCD screens.

The wiring with better colours and in real life:

You are telling it to print random characters.

I thought "random(5)" would produce a number from 0 to 4, also I don't make the lcd display the variable "n"

The orange wire doesn't seem to be connected the same. In the diagram, it doesn't connect to anything.

1 Like

Fair assumption and since test example works, I likely think voltage "dips" due to Dupont jumpers, "elevated" ground connections due to current draw, and just the fact that motors produce noise in power systems.

  • Motor power should not go through breadboard
  • You can test motor RF using an old AM transistor radio ... low-end of band with no station: if you hear noise when motor runs, your Arduino can receive that noise via the jumper wiring *
  • *note on old trick:
    AM radio set to clear-spot can "hear" Arduino clock harmonics, boot-up sequences, program loading, serial activity, etc. due to RF 'hash' produced by digital electronics. With some experience, the eBay 9V powered specials can be a great diagnostic tool. But, start with a working circuit to learn the different patterns of noise.

I explained myself badly, I'm not using a motor but I know that using motors and lcd screens together can cause problems. What I was wondering is if it could be that in my case the RGB LED and the buttons were giving problems in the same way as a motor can give them.

Unlikely except in one area: current draw which really messes with Dupont cables that have some "resistance" and if those are in grounding roles, yea, could be an issue!

I built a testbed for Dupont jumpers that requires the jumper to sustain a 250mA constant load (filament bulb) and I find a few bad jumpers from time-to-time (even new ones.)

1 Like

Ok thanks, so if that's what's giving me problems what should I do to fix it?

Typical breadboard use may appear as:

Current in the Dupont jumpers is around 20mA or less - based on Ohm's Law. That means current in the source lead, the LED, the resistor, and the Ground Dupont. A "poor" connection anywhere will increase the resistance and correspondingly reduce the electrical current in all connected components resulting in the LED being dimmer.

Cheap Dupont cables:

  • often come from the factories defective
  • often have a waxy coating on the pin connector
  • often have poor crimping inside molded end-piece

Worst, the pins of cheap cables are steel and not copper and have a tin-plating; these are problematic because they tarnish when the tin is worn away with use. Visual inspection before use often shows a waxy coating that will contaminate the best breadboard connector strips.

Not-so-good jumpers (IMO):

Before Dupont cables, the exprementer would cut lengths of solid copper wire, strip it, and make connections in that manner. Unless corroded, even air-tarnished wire-ends being forced into the breadboard's spring steel clips will produce a good connection.

In the above, solid copper wire connects the servo motor to the breadboard power rails ... the short jumpers could have been eliminated but perhaps they were added for a reason - such as current measurements.

IMO:
Better grade jumper cables have a decent crimp construction of quality components with a hard shell:

image

Serious prototypers may even opt to build out their own jumpers:

Recommendations:

  • Use solid copper for cross-board power connections
  • Use solid copper for servo motor, relays, filament lights
  • Use heavy gauge or solid wiring to binding posts for power
  • Jumper all +V & Gnd connections back to breadboard Gnd rails; do not daisy-chain power or grounds
  • Inspect and test all jumpers before first use; repeat often
  • Keep breadboard dust-free and vacuum often pulling as much air as possible through holes to removes dust and microscopic metal particles.

Read: An Introduction to Ground: Earth Ground, Common Ground, Analog Ground, and Digital Ground - Technical Articles (allaboutcircuits.com)

The method of using single grounding points (or star grounds) looks great on paper. In practice, however, it can be very difficult to implement depending on the complexity of one's design. An alternative approach is to use a grounded bus bar.

I'll leave you with this thought: Never trust a jumper wire until you test

1 Like

Hm, do you mean you just loaded one of those examples into Arduino without doing anything else, with all the wires, leds, and the same LiquidCrystal library, and it works perfectly?
If the answer is yes, it won't (can't) be any hardware related issue, but something in your code, but I can't find anything wrong. In this case just try to empty the loop() function (and get rid of the lower unused functions), just let it run setup and show us what you get on the LCD (attach a photo of the "messed" LCD).

Otherwise (meaning you tested the LCD in another breadboard, with exactly the same pin wiring), try the same test I suggested before (clean the code up, just empty the loop function to keep just the LCD starting code) and see what happens. If LCS is still bad, power Arduino off and start removing one element each time (e.g. on the first try pull the RGB and resistors away, then on the second remove all the buttons). As soon as the LCS comes to life, the last item removed is the culprit and you can/should investigate why that specific thing prevents you from using the LCD, but it'd be a quite easy task as you narrowed the search.

PS: I still suggest you to always use I2C LCDs, they're easier to wire, require less pins, and apart from using a specific LiquidCrystal_I2C library you don't see any other changes.
But warning, there are many libraries with the same name, I used THIS one

1 Like

Thanks again for your reply and sorry I haven't written again for quite some time. Luckily during this time I discovered the cause of the problem and it is the LED. I can say this because running my code without the LED attached the screen works perfectly. I also tried modifying the test codes that come with the LCD library to make the LED light up and change color and the result was that the screen went nuts. Unfortunately what I haven't been able to find is a solution.

As for the I2C LCDs, I would buy it but the Arduino is not mine it is borrowed to do this project and therefore there is no point in buying components if I don't have the Arduino itself in the future.

Messed up LCD screen:

Good, most of the times isolating items in turn (as I suggested, "removing one element each time (e.g. on the first try pull the RGB and resistors away, then on the second remove all the buttons).") will give you the reason of the malfunctions.
In this case, the RGB LED could drain too much current out of Arduino, messing around LCD communications. So if you want to put it back in the project you better use a bunch of common 2N222 transistors to drive it from power supply instead of Arduino pins.

1 Like

I only have this two transistor and i don't really know how to use them...
Could it be that, with what I have, I can't do anything to make it work and I have to give up and use, for example, three different colored LEDs to simulate what the RGB LED would do but with less energy consumption?

Hi,
What are their part numbers?
An image of the front of them would help.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Here it is:

I'm sorry, those two aren't transistors you can use for this project. The bigger one is a good MOSFET (a "power transistor", just to briefly describe you its kind, and it's too big for your purpose), the little one is a temperature sensor. Anyway, you need three of them to drive three LEDs/colors.

I don't think so, even if there are a lot of different LEDs (you should check the specific datasheet to see the required currend ofr each one) the total energy is almost the same.
But I still don't know if you need to show either a gradient of colors (in this case you need an RGB LED, otherwise you can't easily "mix" the colors), yor just one of the three at the time (in this case you can have three distinct standard, low power LEDs). But in both cases I'd suggest you to drive them using transistors (2N222 are very common, and cheap).
PS: in the future, use Google to type the part number of the item to know more about it...

1 Like

Maybe I solved it... I read on the forum about similar problems but with a different LCD screen in a project with a motor and since I'm desperate I tried to see if I could solve my problem in the same way. What I've done is attach a 4.7kohm resistor and a 100uf capacitor (if I'm not mistaken) to the LCD connections (resistor from E to VDD and capacitor from E to VSS) and it seems to work for now.
My questions are: does what I did make sense? Could it be that it fixed the problem permanently and isn't it a coincidence that it worked flawlessly for quite a while? the capacitor I used is not polarized right? (I was afraid to connect it from the wrong side and I don't know if I had any luck to connect it from the right side or if there is no right side)

How the components are attached:
image
capacitor used:

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