I2c Problem Random Characters on LCD not values

I am getting random characters not values on my lcd display. Any ideas? Please see below and attach photos of lcd and setup with flowmeter

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

volatile int flow_frequency; // Measures flow sensor pulses
unsigned int gallons_min; // Calculated gallons/min
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
flow_frequency++;
}
void setup()
{
lcd.begin (20, 4); //CHANGE THIS IF YOU NEED TO
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
lcd.home();
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
Serial.begin(9600);
attachInterrupt(0, flow, RISING); // Setup Interrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print gallons/hour
if (currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 60Q, Q is flow rate in gallons/min
gallons_min = (flow_frequency * 100 / 60); // (Pulse frequency x 1 min) / 60Q = flowrate in gallons/min
flow_frequency = 0; // Reset Counter

Serial.print(gallons_min); // Print gallons/min
Serial.println(“gallons/min”); delay(100);

lcd.print(“TEXT”);
lcd.print(“TEXT”);
lcd.clear();
lcd.setCursor(0, 0);

}
}

You got an I2C backpacked lcd display.
So you need the I2C library, ok.

But why do you also include the “normal” LCD library ?
Delete

#include <LCD.h>

and see what happens then.

If it doesn’t work then we have to search for more issues.

LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

You have defined the backlight pin but you haven't passed that information to the I2C library. Since you haven't identified which I2C library you are using (there are several, all with the same name) we cannot determine if this is a problem or not. It probably is.

Don

rpt007:
You got an I2C backpacked lcd display.
So you need the I2C library, ok.

But why do you also include the “normal” LCD library ?
Delete

#include <LCD.h>

and see what happens then.

If it doesn’t work then we have to search for more issues.

It is not helpful to suggest trying somewhat random things searching for an answer.
Given <LCD.h> is used and that is part of fm’s library but not part of the many other libraries named “LiquidCrytal_I2C.h”, and begin() is being called vs init() and begin() is used in fm’s library but init() is used in the other LiquidCrystal_I2C libraries, it is likely that fm’s library is being used.
In fm’s library, <LCD.h> is used by <LiquidCrystal_I2C.h>
It is very much needed/required; however, it is included by <LiquidCrystal_I2C.h> so it is not necessary to include in the sketch.
But having it in the sketch will not hurt and removing it will do nothing since it is always being used regardless of whether the sketch includes it.

Don, the sketch is using the old API for setting up the pin rather than including it in the constructor.
It calls

lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);

To set up the backlight pin and polarity.
It then incorrectly uses setBacklight(dimvalue) by passing in HIGH which has a value of 1. This value of HIGH/1 is trying to set the backlight to be as dim as possible rather than telling the library to just turn it on using lcd.backlight()


mgdnation,
The issue here is more than likely that the pin mappings you passed into the constructor are incorrect for that particular backpack as that is the behavior you get when they are wrong.

My suggestion would be to switch to my hd44780 library as it will automatically figure out the i2c address and the pin mappings for you. It is also faster than the other libraries.
hd44780 can be installed using the IDE library manger.
You can read more about it here: GitHub - duinoWitchery/hd44780: Extensible hd44780 LCD library

The i/o class you will want to use is hd44780_I2Cexp
This class also includes a diagnostic sketch (I2CexpDiag) to test the i2c pins and the LCD memory.
I would recommend running the diagnostic sketch to verify that everything is working.

— bill

mgdnation.

I think you can try this code.

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

LiquidCrystal_I2C lcd(0x27,20,4);   // 20, 4 OR 16, 2

volatile int flow_frequency; // Measures flow sensor pulses
unsigned int gallons_min; // Calculated gallons/min
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;

void flow () // Interrupt function
{
flow_frequency++;
}
void setup()
{
  lcd.init();  
  lcd.clear();           // clear the screen
  lcd.backlight();

pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
Serial.begin(9600);
attachInterrupt(0, flow, RISING); // Setup Interrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print gallons/hour
if (currentTime >= (cloopTime + 1000))
{
  cloopTime = currentTime; // Updates cloopTime
  // Pulse frequency (Hz) = 60Q, Q is flow rate in gallons/min
  gallons_min = (flow_frequency * 100 / 60); // (Pulse frequency x 1 min) / 60Q = flowrate in gallons/min
  flow_frequency = 0; // Reset Counter

Serial.print(gallons_min); // Print gallons/min
Serial.println("gallons/min"); delay(100);

lcd.setCursor(0, 1);
lcd.print("gallons/min :");
lcd.println(gallons_min);

}
}

It is not helpful to suggest trying somewhat random things searching for an answer.

Bill, this was by far not my intention; but as you can learn from all of those similar repeated discussions - there is a big confusion still dealing with LCD displays.
Unfortunately there are so many look-a-like and same-named libraries that a newbie is lost - me included sometimes.

Thanks to your excellent new library I will stick to that and I am in midst of the process to clean up my old sketches piece by piece and exchanging all previously included other libraries by yours. I do so as I expect that over time older libraries might run into an outdate situation with newer libraries while I trust your library to survive the new IDE waves :slight_smile:

The main problem with Bill’s library is that searching for it by name is futile when you lose the link.

Don

floresta:
The main problem with Bill's library is that searching for it by name is futile when you lose the link.

Don

Not quite sure how to resolve that.

It is big issue if trying to use a search engine like google given the name.
Although if you search for "duinowitchery" you can find it but that is a very obtuse search term.
(other libraries will be coming in the future to the duinowitchery repo)

It isn't too bad if you search for it in the IDE library manager.
If you put "hd44780" in the library manager search field, only a few show up so it is easy to find.
At this point, many Arduino users would be far better served searching in the library manager for libraries rather than using google.
At least at this point in time when grabbing a library from the library manger it will be fairly current and extremely unlikely to have Arduino 1.x issues.

I'm going to create a feature request for the Library manager for some additional search capabilities.
As of now, the search does not look in the Author or Maintainer fields.
It would be nice to be able to search for libraries by a particular author/maintainer
It might also be nice to be able to add a "tags" field for searching.
I think I'm going to try to push them to add a new field called "keywords" to the .properties fie that is also searchable.
This specific field name is good as it is compatible with platformio and the platformio system can use it to directly populate this field when it automatically builds a platformio.json file from an arduino library.

In the mean time I'm going to add "bperrybap" and "duinowitchery" to the paragraph description so that searches in the library manager can also find it using these terms.

--- bill

Not quite sure how to resolve that.

What about asking @TerryKing to test your library and if he is convinced he could recommend it in his arduino-info about LCD-Blue-I2C as it might need an update.

rpt007:
What about asking @TerryKing to test your library and if he is convinced he could recommend it in his arduino-info about LCD-Blue-I2C as it might need an update.

I'm aware of that web page and have talked with Terry a few times in emails offline and even got him to do some updates to that page in the past, including providing some of that example code.
We have exchanged a few emails about hd44780 since back in July 2016 and he offered to send me some his LCD h/w.
He commented in another I2C/LCD thread recently that he had interest in hd44780 and getting it integrated into that page.
I sent an email to him a few weeks ago about it, but no response.
He seems interested in it but also seems very busy and/or distracted with other things so nothing has happened yet.

BTW, just released an update (0.8.3) that should make it easier to find in the IDE library manager so it will now show up if you search for my name, hd44780_I2Cexp, hd44780_pinIO, duinowitchery, LiquidCrystal_I2C and a few other related keywords.
It should at least make it easier to find in the Arduino IDE library manager.
The next release will include a platformio .json file with keywords so that it will be easier to find for users that are using platform io tools instead of the Arduino IDE.

--- bill