I2C freeze scanning for LCD

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.

1. Which Arduino are you using?
2. Upload the following class-less scanner sketch to see that your board is detected.


#include<Wire.h>
byte busStatus;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  for (int i2cAddress = 0x00; i2cAddress <= 0x7F; i2cAddress++)
  {
    Wire.beginTransmission(i2cAddress);
    busStatus = Wire.endTransmission();
    if (busStatus == 0x00)
    {
      Serial.print("I2C Device found at address: 0x");
      Serial.println(i2cAddress, HEX);
    }

    else
    {
      Serial.print("I2C Device not found at address: 0x");
      Serial.println(i2cAddress, HEX);
    }
  }
}

void loop()
{

}

3. Convert the skecth of 2. into three file class-based sketch and upload again.

4. I can't find your LCD20x4.h Librray in the web.

Thanks.

  1. An arduino Nano V3.0 (a clone, i think, from AZ-Delivery)

  2. This sketch does run through and finds the LCD at 0x27

  3. Sorry, I don't get your intent. Could you expand your idea?

  4. Should be in my post, but I'll send it again.

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

1.

Your sketch is a class-based program and contains a very unfamiliar library file LCD20x4.h. I am not sure if you have written this program tourself or not. If you are really interested to experiment how a class-based program is made and run in the IDE, you try to convert the my working scanner sketch into a three file (.ino, .h, and .cpp) program and run it. Also use the LiquidCrystal_I2C.h Librray.

2.
You may study and practice the following single file class-based sketch to get idea on the formation style of a class-based program. This program takes the radius and height of a cyclinder from the Serial Monitor and then computes the surface area.

class Cylinder {
  private:
    int radius;
    int height;
    long baudRate; // Stored safely inside the class as a 'long'
    
  public:
    Cylinder(long); // Constructor accepts 'long' for high baud rates
    void beginSerial(); // Method to start Serial using the stored baud rate
    void setRadius(int);
    void setHeight(int);
    float getSurfaceArea();
};

char myData[15];
Cylinder myCylinder(115200); // Pass Bd via constructor

void setup() {
  myCylinder.beginSerial(); // Initializes Serial at 115200
}

void loop() {
  byte n = Serial.available();
  if (n != 0) {
    byte m = Serial.readBytesUntil('\n', myData, sizeof(myData) - 1);
    myData[m] = '\0';

    char *tokenR = strtok(myData, ",");
    char *tokenH = strtok(NULL, ",");

    if (tokenR != NULL && tokenH != NULL) {
      int r = atoi(tokenR);
      int h = atoi(tokenH);

      myCylinder.setRadius(r);
      myCylinder.setHeight(h);

      float area = myCylinder.getSurfaceArea();
      Serial.println(area, 2);
    }
  }
}

// Constructor implementation
Cylinder::Cylinder(long bd) {
  myCylinder.baudRate = bd;
}

// Starts Serial communication
void Cylinder::beginSerial() {
  Serial.begin(baudRate);
}

void Cylinder::setRadius(int r) {
  radius = r;
}

void Cylinder::setHeight(int h) {
  height = h;
}

float Cylinder::getSurfaceArea() {
  return ((2 * 3.14 * radius * height) + (2 * 3.14 * radius * radius));
}

Your constructor is called before the hardware is initialised.
After the constructor is called, the hardware is initialised (before setup()) undoing all initialisation that you did in the constructor.

The correct approach is to have a begin() method in your class (as is done for e.g. Serial and Wire).
In that begin() method you will do the initialisation and in setup() call that begin() method.

That was it!

Thank you very much.

Here is the corrected code for all that are interested:

LMAB_test_minimal.ino

#include <Arduino.h>
#include "LCD20x4.h"

LCD20x4 logger;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Wire.begin();
  Wire.setWireTimeout(50000, true);
  logger.begin();
  
  Serial.println("B1");
}

void loop() {
  // put your main code here, to run repeatedly:
  logger.print();
  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();
    void begin( uint8_t i2cAddress = 0 );
    void print();
  private:
    LiquidCrystal_I2C* lcd = NULL;
    bool checkAddress(uint8_t i2cAddress);
    void setup();
};

#endif

LCD20x4.cpp

#include <Arduino.h>
#include "LCD20x4.h"

LCD20x4::LCD20x4(){}

void LCD20x4::begin(uint8_t i2c_address){
  if(!i2c_address){
    if(checkAddress(0x27)){
      lcd = new LiquidCrystal_I2C(0x27, 20, 4);
      setup();
    } else {
      if(checkAddress(0x3F)){
        lcd = new LiquidCrystal_I2C(0x3F, 20, 4);
        setup();
      }
    }
  } else {
    if(checkAddress(i2c_address)){
      lcd = new LiquidCrystal_I2C(i2c_address, 20, 4);
      setup();
    }
  }
}

bool LCD20x4::checkAddress( uint8_t i2cAddress ){
  Wire.beginTransmission(i2cAddress);
  uint8_t i2cError = Wire.endTransmission();
  if(i2cError == 0){
    return true;
  } else {
    return false;
  }
}

void LCD20x4::setup(){
  lcd->init();
  lcd->setBacklight(1);
  lcd->noCursor();
  lcd->leftToRight();
}

void LCD20x4::print(){
  lcd->clear();
  lcd->println("Hello, World!");
}