I tried to combine code from the I2C scanner example with LiquidCrystal_I2C commands, and managed to get a freeze.
My code should check if there is an LCD at 0x27 (or alternatively at 0x3F) or none at all, and initialise accordingly.
Now, the pure i2c_scanner example from the Wire library does work with my hardware, and so does the HelloWorld example that comes with the LiquidCrystal_I2C library. So I think I can rule out a hardware problem.
Here’s my code:
LMAB_test_minimal.ino
#include <Arduino.h>
#include "LCD20x4.h"
#define LED_LEFT_RED_PIN 9
#define LED_LEFT_GREEN_PIN 8
#define LED_LEFT_BLUE_PIN 7
#define LED_RIGHT_RED_PIN A2
#define LED_RIGHT_GREEN_PIN A1
#define LED_RIGHT_BLUE_PIN A3
//LCD20x4 logger;
LCD20x4 logger(0x27);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("B1");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("B2");
delay(1000);
}
LCD20x4.h
#ifndef LCD20x4_LIB_H_
#define LCD20x4_LIB_H_
#include <stdint.h>
#include <LiquidCrystal_I2C.h>
class LCD20x4{
public:
LCD20x4();
LCD20x4( uint8_t i2cAddress );
private:
LiquidCrystal_I2C* lcd = NULL;
bool checkAddress(uint8_t i2cAddress);
void setup();
};
#endif
LCD20x4.cpp
#include <Arduino.h>
#include "LCD20x4.h"
LCD20x4::LCD20x4(){
Wire.begin();
Wire.setWireTimeout(50000, true);
Serial.begin(9600);
// for(uint8_t addr = 1; addr < 0x26; ++addr){
// checkAddress(addr);
// }
if(checkAddress(0x27)){
lcd = new LiquidCrystal_I2C(0x27, 20, 4);
setup();
} else {
if(checkAddress(0x3F)){
lcd = new LiquidCrystal_I2C(0x3F, 20, 4);
setup();
}
}
}
LCD20x4::LCD20x4( uint8_t i2cAddress ){
Wire.begin();
Wire.setWireTimeout(50000, true);
Serial.begin(9600);
Serial.print("lcd pointer ");
Serial.println(reinterpret_cast<uintptr_t> (lcd));
Serial.println("C1"); Serial.flush();
lcd = new LiquidCrystal_I2C(i2cAddress, 20, 4);
Serial.println("C2"); Serial.flush();
setup();
Serial.println("C3"); Serial.flush();
}
bool LCD20x4::checkAddress( uint8_t i2cAddress ){
Serial.print("checking address 0x");
Serial.print(i2cAddress, HEX);
Serial.flush();
Wire.beginTransmission(i2cAddress);
Serial.print("...");
Serial.flush();
uint8_t i2cError = Wire.endTransmission();
if(i2cError == 0){
Serial.println(" device found");
Serial.flush();
return true;
} else {
Serial.print(" err ");
Serial.println(i2cError);
Serial.flush();
return false;
}
}
void LCD20x4::setup(){
Serial.print("lcd pointer ");
Serial.println(reinterpret_cast<uintptr_t> (lcd));
Serial.println("D1"); Serial.flush();
lcd->init();
Serial.println("D2"); Serial.flush();
lcd->setBacklight(1);
Serial.println("D3"); Serial.flush();
lcd->noCursor();
Serial.println("D4"); Serial.flush();
lcd->leftToRight();
Serial.println("D5"); Serial.flush();
}
If I choose the constructor with the scanner code, the execution freezes at the first Wire.endTransmisssion() (LCD20x4::checkAddress(), line 38).
If I choose the constructor without the scanner, the execution freezes at lcd→init() (LCD20x4::setup(), line 62).
My hardware is an Arduino Nano v3.0, with an 20x4 LCD attached by jumper cables about 20cm long. I’ve also tried swapping in an Arduino Mega 2560 R2, and a ZS-042 RTC module, to no avail.
On the software side, there is the Arduino IDE 2.3.10, Wire library 1.8.8, and LiquidCrystal_I2C 2.0.0, apparently from GitHub - markub3327/LiquidCrystal_I2C: LiquidCrystal Arduino library for the DFRobot I2C LCD displays · GitHub (the one that popped up first in the library manager).
Also I tried the Adafruit_LiquidCrystal library ( GitHub - adafruit/Adafruit_LiquidCrystal: A fork of my own LiquidCrystal library · GitHub ) with no changes in behaviour.
Why my approach?
-
I expect that the values I need to monitor will be better readable on the LCD than on an ever-scrolling serial monitor.
-
The LCD will be attached during development and debugging, but will not be part of the finished project.
-
I’ve had issues before with USB powered boards, when the rest of the circuits where powered from a battery (and an essential part of the project is to be switched on and off regularly)
-
And I’ve already dedicated the hardware serial port to an MP3 player module on a PCB that’s already been made.
Is there a plan B?
Use SoftwareSerial and an FTDI breakout board in place of the I2C bus.