I use the Auduino IDE with a UNo R3. I have a sketch which runs and uploads. The sketch uses two infared sensors to calculate speed and I can get a display in the serial monitor but not the LCD. The LCD
is a QPASS with backpack. I have tried several different I2C libraries from the GitHub without any luck. Any ideas what I am doing wrong? At one point I copied the I2C library and wire from two different examples onto the sketch (one at a time)and deleted the one that came with the sketch.
The problem is on line 42 of the sketch that you did not provide.
Understand now I get to look for the way to load the sketch maybe by tomorrow

/* MLRTrains - Model Railroad Speedometer using TCRT5000 Optical Sensor pair
* INCLUDES CODE FOR TM1637 LED Display and LCD Display ++++ADD LCD DISPLAY INFO HERE
*
* SAME AS ver06 but adds an LCD display for speed readout
* Removed un-needed comments and code from previous version
*/
//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int CLK = 9; // Set the CLK pin connection to the LED display
const int DIO = 8; // Set the DIO pin connection to the LED display
/*-----( FOR LCD DISPLAY )-----*/
// set the LCD address to 0x27 for a 16 chars 2 line display
// A FEW use address 0x3F
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address
//user variables
float distance = 4.00; //USER SETTING: Change number to measured distance between sensors in INCHES CDH
float scale=87.1; // USER SETTING: THIS IS FOR HO SCALE. Change as needed for other scales. CDH
int sensor1 = 4;
int sensor2 = 5;
int ledReady =10;
int ledDelay =11;
int started=0, finished=0;
bool s1Covered;
bool s2Covered;
float start, finish, elapsed, miles, hours, mph, scaleMPH;
void setup() {
Serial.begin(9600);
lcd.begin( 16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledReady, OUTPUT);
pinMode(ledDelay, OUTPUT);
digitalWrite(ledReady, HIGH);
lcd.backlight(); // LCD backlight on
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print(" MRL TRAINS");
lcd.setCursor(0,1);
lcd.print(" SPEEDOMETER");
}
void loop() {
// put your main code here, to run repeatedly:
s1Covered=digitalRead(sensor1);
if (s1Covered==0 && started==0){
start=millis();
started=1;
Serial.println("Started");
digitalWrite(LED_BUILTIN, HIGH); //Turns on LED 13 when sensor 1 is tripped. CDH
digitalWrite(ledReady, LOW); //Turns off Pin 10 LED when sensor 1 is tripped. CDH
lcd.clear ();
lcd.setCursor(0,0);
lcd.print(" > > > > ");
}
s2Covered=digitalRead(sensor2);
if (s2Covered==0 && started==1){
finish=millis();
finished=1;
digitalWrite(LED_BUILTIN, LOW); //Turns off LED 13 when sensor 2 is tripped. CDH
digitalWrite(ledDelay, HIGH); //Turns on Pin 11 LED when sensor 2 is tripped. CDH
Serial.println("Finished");
elapsed = finish-start; // millis
elapsed = elapsed /1000; // seconds
Serial.print("Seconds: ");
Serial.println(elapsed);
miles = distance / 63360; // miles
hours = elapsed / 3600; // hours
mph = miles / hours;
scaleMPH = mph * scale;
Serial.print("Scale MPH: ");
Serial.println(scaleMPH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(scaleMPH);
lcd.setCursor(13,0);
lcd.print("MPH");
delay(8000); // USER SETTING: Set the delay (in Milliseconds) before speed resets to 0. 1000 milliseconds = 1 second. CDH
digitalWrite(ledDelay, LOW); //Turns off Pin 11 LED after delay is complete. CDH
digitalWrite(ledReady, HIGH); //Turns on Pin 10 LED after delay is complete. CDH
started=0;
finished=0;
lcd.clear ();
lcd.setCursor(0,0);
lcd.print("READY");
}
}
Look at the examples for the LCD library you are using and see how the I2C display is set up there. I find it odd that the declaration of lcd and the lcd.begin statement both are specifying the 16x2 size of the display. Also, please verify that the example code will run on your display, not all I2C interfaces are wired to the LCD display the same way, if you have been changing libraries you may have loaded an incompatible one.
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address
...
...
...
lcd.begin( 16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
Look into the hd44780 library. It is the best library currently available for I2C character displays using the hd44780 controller. Some advantages of the hd44780 library are that it will auto detect the LCD's I2C address and the LCD to I2C expander backpack pin mapping.
The library is available in the Library Manager. Go to Library Manager (in the IDE, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.
The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.
In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.
david_2018:
I find it odd that the declaration of lcd and the lcd.begin statement both are specifying the 16x2 size of the display. Also, please verify that the example code will run on your display, not all I2C interfaces are wired to the LCD display the same way, if you have been changing libraries you may have loaded an incompatible one.LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address
...
...
...
lcd.begin( 16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
That is due to a combination of history and API compatibility.
Years ago, The bundled LiquidCrystal LCD library was not so popular and was just starting.
There was also LCD API proposed: Arduino Playground - LCDAPI
LCD API 1.0 proposed using a simple init() method to initialize the LCD hw instead of begin() method.
The intent was to have a simple function with no parameters that worked across all interfaces so that sketch code could be common.
The only difference between LCD devices would be in the constructor where the initialization parameters were provided.
Which makes sense as the object is for the physical device and the constructor could define all the needed parameters for the physical device.
The "LiquidCrystal_I2C" library implemented the LCD API 1.0 which put the rows and cols parameters in the constructor and used a simple init() method to initialize the h/w. It originally did not provide a begin() method at all.
But the LiquidCrystal library pushed a different direction.
It provided an init() function but it required parametrers.
Also, several years back many Arduino libraries starting pushing using a begin() method as did the LiquidCrystal library vs providing all the needed parameters in the object constructor.
LiquidCrystal implemented a begin(cols, rows, [charsize]) method for initialization.
Recently, the LiquidCrystal_I2C library added a begin(cols, rows, [charsize]) for better compatibility with LiquidCrystal.
So now with that library, init() or begin() can be called to initialize the LCD h/w.
There is difference.
init() will use the cols and rows from the constructor. It will also call Wire.begin() before initializing the LCD.
begin() will override the cols and rows from the constructor but does not call Wire.begin() before initializing the LCD.
Once begin() is called the original cols and rows may be lost as the cols/rows parameters to begin() overwrite the cols and rows specified in the constructor.
--- bill
Problem Solved I used groundFungus idea of using library and the sketch now works
delaware900:
Problem Solved I used groundFungus idea of using library and the sketch now works
As it should.
Bill - bperrybap who posted above - is the author.