Arduino UNO with LCD 2x16. Weird characters after some time in operation

Hi Arduino Forum,

I'm new to the Arduino world and to the world of coding.

The problem I'm having is that after some time of normal operation the screen goes weird. Random letters, numbers, and characters move on the screen really fast.

My setup is a flow meter that meters our water usage in the house, connected to the meter is the Arduino Uno and the display shield. All Powered by 230V to 5V USB wall socket adapter. We sometimes have power spikes in our area and at first I immediately thought that this was the cause, but apparently not, because I've tested and external battery to power the system and the problem still occurs.

My components are as follows: Arduino Uno (Factory assembled) ARDLCD216 Display shield (Factory assembled) Adafruit Brass flow meter (Factory assembled)

What I've tried so far: Three different Uno's (Factory assembled) Two different displays (Factory assembled) http://www.partco.biz/verkkokauppa/product_info.php?cPath=5&products_id=10155&language=en External battery source (Self made) Different cables between meter and Arduino. On breadboard and off breadboard.

Now I don't know much about coding, but I did manage to get a percentage text and a math equation into the code and the code to display it on the screen. I would really appreciate if somebody had the time to glance at the code and see if there are any mistakes or find ways of doing it better.

Here is the code that I've copied from adafruit.com:

#include "LiquidCrystal.h"
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


#define FLOWSENSORPIN 2

// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
  uint8_t x = digitalRead(FLOWSENSORPIN);
  
  if (x == lastflowpinstate) {
    lastflowratetimer++;
    return; // nothing changed!
  }
  
  if (x == HIGH) {
    //low to high transition!
    pulses++;
  }
  lastflowpinstate = x;
  flowrate = 1000.0;
  flowrate /= lastflowratetimer;  // in hertz
  lastflowratetimer = 0;
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
  }
}

void setup() {

  lcd.begin(16, 2);
   
   pinMode(FLOWSENSORPIN, INPUT);
   digitalWrite(FLOWSENSORPIN, HIGH);
   lastflowpinstate = digitalRead(FLOWSENSORPIN);
   useInterrupt(true);
}

void loop()                     // run over and over again
{ 
  lcd.setCursor(0, 0);
  //lcd.print(flowrate);
    
  float liters = pulses;
  liters /= 8.1;
  liters -= 6;
  liters /= 60.0;
  
  float procent = liters/10000*100;

  lcd.setCursor(0, 0);
  lcd.print("Litres:"); lcd.print(liters);
  lcd.setCursor(0,1);
  lcd.print("Procent:"); lcd.print(procent);
  delay(100);
}

Someone somewhere said that the LCD should be cleared with the lcd.clear(); command before writing new data to the screen. This I have not yet tested.

I've done some Googling on the subject but can seem to find any answer or other people are not willing to share the correction after finding the error.

Best Regards,

Aslak

The problem I’m having is that after some time of normal operation the screen goes weird. Random letters, numbers, and characters move on the screen really fast.

The implication here is that it is time related when most likely it is event related. What else is happening just prior to the screen going weird? Are there any solenoids, relays, or motors nearby that you haven’t told us about?

Someone somewhere said that the LCD should be cleared with the lcd.clear(); command before writing new data to the screen. This I have not yet tested.

This generally is poor advice. You should avoid using lcd.clear(), especially in loop(), unless you enjoy looking at a flickering screen.

Don

Hi Don,

The Arduino + meter is located inline with the house supply just after our pressure vessel where the water is buffered awaiting demand. There is no pump on the pressure vessel, but there is a relay on the vessel that controls the remote pump when the water pressure gets low. Distance is about 20cm. I've sat in the boiler room a followed the relay and pressure on the vessel operate, but find no connection to the weird characters. I'm able to move the Arduino around and have had it sitting on the relay box but I see no difference in this.

All other equipment is at least 50cm away.

I'll have to test, so that the only the Arduino and the shield is connected, and see if the problem occurs. This test will be done in-house not in the boiler room.

Best Regards,

Aslak

[Update]

The Arduino has now been sitting on my desk powered on for ~13 hours and no weird characters have appeared on the screen. So there is probably nothing wrong with the Arduino or the screen, I guess. I suspect that the problem is either with the code that does the calculation (some kind of over flow happening?), the meter or the boiler room is making some kind of noise / interference.

How are you powering the arduino, and the LCD. Is there common power supplies with other things in the room?
It may help to add some capacitors across the power supply pins on all components.

[quote author=jack wp link=topic=186218.msg1380968#msg1380968 date=1378421663] How are you powering the arduino, and the LCD. Is there common power supplies with other things in the room? It may help to add some capacitors across the power supply pins on all components.

[/quote]

Hi Jack wp,

The Arduino + LCD + Meter are powered by a wall socket adapter, no other components share that same power supply.

I'll look into the capacitors and try to figure out what ones I need. This is some thing very new to me so the learning curve is steep :)

Regards,

Aslak

So, to make it work what did you do? disconnect the meter? Move it to a new room? Use a different power supply (or did you take the one you were using to the other room?

Try doing the things in reverse order. Take arduino/LCD back to the original room, but don't hook up the meter yet. ETC.

Small caps are good , say .1 mf range.

Hi,

Sorry for the long reply. I've done some testing, and I've gotten the system to work a lot better when I move the Arduino + LCD further away from the pressure vessel. I still have to test out Jack wp's suggestion with the ´caps.

Regards, Aslak

You have the possibility of a divide-by-zero error in your code.

This line: flowrate /= lastflowratetimer; // in hertz

lastflowratetimer may be 0, which could cause all sorts of odd behaviour at seemingly random times. This may be the cause of your strange problem.

Hi arduinodlb,

Thanks for the pointer, how would I fix this issue? Maybe add an IF ? As I've said, I'm very new to this and have no idea of coding :~

Regards,

Aslak

An IF is a very simple and efficient way to make sure you are dividing by a valid number, go for it.

Yep, an if will do it.

I'd have to work out exactly what you were doing to give you a definitive answer, but this will at least stop the divide-by-zero from occurring:

if (lastflowratetimer != 0) flowrate /= lastflowratetimer; // in hertz else flowrate = 0; // Or some other value??

@floresta

Are there any solenoids, relays, or motors nearby that you haven't told us about?

Hi! I have a similar problem. I have 3 screens working perfectly when are direct connected to the Arduino , but when I connect to what is supposed to be their permanent site, a slot car circuit, the screens show the random weird numbers.

I didn't know anything about motors or solenoids, but, could be possible that the symbols are caused for the three cars passing beside the arduino? (the arduino is attached under the table)

Thanks!

Sure. Those cars are powered by these sliding contacts which creates tons of interference. Use very good filtering and shielding in your project.

@MAS3 Thanks for the reply. I understand that I should use such a case or something for the arduino, but what do you mean with a very good filtering? Do you know any way to solve this?

As a final solution if not, I will have to change the place of the board.

Shielding doesn't stop at a casing. Wires can be shielded too, take a look at that. Also don't use longer wires than absolutely necessary, and don't run those wires along the slot car traces. If you need to cross those traces, cross them in a 90 degree angle.

Filtering starts with applying capacitors in power lines. Add a set of capacitors (let's say 100 µF electrolytic plus a 100 nF capacitor) as close to the power pins of your LCD as possible. Do that at all power lines (Arduino, display, sensors and so on). There's more filter techniques available (using coils and/or diodes for instance), but this is what you can start with and relatively easy. You can't use µF range capacitors in data lines, they'll destroy your data.

Racetracks are notorious for putting out general interference, as well as putting interference on the power lines.

In terms of the power supply, the easier way to check if this is the problem is to power your Arduino with a battery. This isolates the power supply to the Arduino, and has no AC ripple. Use a short cable to minimise local interference. Longer term, if this proves that the power supply is the problem, you'd have to look at filtering the power supply as mentioned in this thread.

Reading above post, i assumed in my answers that the Arduino has it's own power supply. But that wasn't stated anywhere so if the Arduino shares the power supply with the track, that's a big no-no.

Sorry, I didn't mean to imply they actually used the same power supply. I mean if the power supplies are plugged into the same wall socket, or even just the same circuit (which is quite likely) racetracks are probably putting noise on the power line.

@ MAS3

Did you ever get your setup to work?

Mine is still on hold, been busy with other stuff.