LCD 16x2 via I2C Hello World Wire library only

Hi,

Struggling with hardware problems I've been using a test sketch employing just Wire library. The purpose is clear: one can trace, at least, I2C errors.

#include <Wire.h>

void WirebeginTransmission() { Wire.beginTransmission(0x27); }

const char *DecodeEndTransmission(unsigned errorCode) {
  switch (errorCode) {
  case 1:
    return "data too long to fit in transmit buffer";
  case 2:
    return "received NACK on transmit of address";
  case 3:
    return "received NACK on transmit of data";
  case 4:
    return "other error";
  case 5:
    return "timeout";
  default:
    return (errorCode ? "UNKNOWN" : "ZERO");
  }
}

void WireendTransmission(char *msg) {
  unsigned code = Wire.endTransmission();
  if (code > 0 && msg != NULL) {
    Serial.printf("endTransmission: \"%s\" %s\n", msg,
                  DecodeEndTransmission(code));
  }
}

void setup() {
  Serial.begin(9600);
  while (!Serial)
    ;
  Wire.begin();
  Wire.setClock(400000);
  WirebeginTransmission();
  Wire.write(0x01); // Clear display

  WireendTransmission("setup");
}

unsigned count = 0;

void loop() {
  Serial.printf(">>%d<<\n", count++);
  WirebeginTransmission();
  Wire.write(0x80); // Set cursor to the first line
  Wire.write("Hello, World!");
  WireendTransmission("loop");
  delay(3000);
}

The remaining problem: the backlight is always off. Well, not quite: it blinks for a split second, one can even guess some characters there. How can one turn on the backlight?

There are several variants of I2C LCD interfaces. You need to figure out which one you have and which pin is the backlight. Your other problem is you didn't show any code that initializes the LCD. Also the backpacks operate in 4 bit mode so you need to split the commands into two transmissions. This is just the start of your problems.

I would suggest using the hd44780 library and the diagnostics if you think you have a hardware problem.

It depends on your code and your Hardware.
If you have a pcf8574 port expander, and you switch on the pin assigned to the LED (which one?) you must keep the state of the pcf8574 pin even if you send out the next command to the LCD.

Most libraries do that.

Yes but some backpacks are wired to turn on the backlight with the PCF8574 output pin set to HIGH and some are wired to turn on the backlight with the PCF8574 output pin set to LOW.

--- bill

I would second this.
I2Cexpdiag will perform a series of tests and report any issues it finds.

--- bill

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.