LCD displays weird characters

I was trying a project, and I'm a beginner in arduino. I tried copying a project and its code, however the lcd was displaying a weird character, opposite of what I saw from the project source.

The project was about data logging using dht11, rtc, and sd card module while it could display both to the serial monitor and the lcd

The code:

//LCD Module
#include <LiquidCrystal_I2C.h>
//Temperature&Humidity Module
#include <dht.h>
//SD Card Module
#include <SPI.h>
#include <SD.h>
//Clock Module
#include <Wire.h>
#include "RTClib.h"

// LCD Module
LiquidCrystal_I2C lcd(0x27,16,02);
uint8_t percent[8] = {0x00, 0x19, 0x1B, 0x06, 0x0C, 0x1B, 0x13, 0x00};
uint8_t celcs[8] = {0x00, 0x10, 0x07, 0x04, 0x04, 0x04, 0x07, 0x00};

// Temperature&Humidity Module
#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;

// SD Card Module
const int chipSelect = 10;
File myFile;

// Clock Module
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// LED Indicator Light
int led = 8;

void setup()
{
  Serial.begin(9600);
  delay(500);//Delay to let system boot
  Serial.println("DHT11 Humidity &  temperature Sensor\n\n");
  delay(1000);//Wait before accessing Sensor
  pinMode(led, OUTPUT);

  lcd.begin(16,2);
  lcd.backlight();
  lcd.createChar(1, percent);
  lcd.createChar(2, celcs);

   

  //SD Card Segment
  Serial.print("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  //Clock Module
  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (rtc.lostPower())
  {
    Serial.println("RTC lost power, lets set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop()
{
    // Humidity&Temperature Module
    DHT.read11(dht_apin);
    Serial.print("Humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("% ");
    Serial.print("Temperature = ");
    Serial.print(DHT.temperature);
    Serial.println("C ");

    //Clock Module
    DateTime now = rtc.now();
    Serial.print("Current Time: ");
    Serial.print(now.hour());
    Serial.print(':');
    Serial.print(now.minute());
    Serial.print(':');
    Serial.println(now.second());

    //SD Card Module
    myFile = SD.open("Datalog.txt", FILE_WRITE); // Opening The File
    if (myFile)
    {
    Serial.print("Writing to Datalog.txt...");
    myFile.print(now.hour(), DEC); // Time and Date
    myFile.print(":");
    myFile.print(now.minute(), DEC);
    myFile.print(":");
    myFile.print(now.second(), DEC);
    myFile.print(":"); //
    myFile.print(DHT.humidity); // Humidity
    myFile.print(":"); // 
    myFile.println(DHT.temperature); // Temperature
    myFile.close(); // Close the File:
    Serial.println("\n");
    }else
    {
      // if the file didn't open, print an error:
      Serial.println("error opening Datalog.txt");
    }

    lcd.setCursor(0,0);
    lcd.print("State: RCDNG");
    lcd.setCursor(1,1);
    lcd.print("TMP:");
    lcd.print(DHT.temperature, 0);
    lcd.write(2);
    lcd.setCursor(9,1);
    lcd.print("HMD:");
    lcd.print(DHT.humidity, 0);
    lcd.write(1);

    digitalWrite(led, LOW);
    delay(60000);
    digitalWrite(led, HIGH);
  
}
    

The LCD that displayed weird characters:

Can someone help me?

Welcome to the forum

Have you tried any of the examples that were installed with the LCD library ?

The LiquidCrystal_I2C libraries which come with the IDE are not compatible with the currently sold I²C backpacks. The fmalpartida library is usable if you understand how to wrangle it.

You are advised to install Bill Perry/s "HD44780" library in the IDE and having done so, work through the examples associated with it and having done so, incorporate it into your code.

+1 for the hd44780 library. It will take the frustration out of getting an I2C display to work by automatically determining the I2C address and the LCD to I2C backpack pin mapping. Install the library through the IDE library manager.

Clearly the I²C address is correct, and it is apparently initialising, but the data is garbled.

It will be interesting to see what the HD44780 code finds. :grin:

please try the "hello world" example delivered with the used library.
Does that show correct data?

When you get garbled displays, it is pretty much always due to the LCD receiving garbled instructions.

Multiple things can cause this; the most common are:

  1. i2c signal integrity can from poor i2c signal wiring
  2. missing i2c pullups
  3. The LCD pin signal integrity can have issues from poor soldering
  4. The library may be using incorrect pin mappings for connections between the LCD and the PCF8574
  5. power glitches that create signal noise on either i2c signals or noise on E signal

If it initially works, but then fails, then you don't have issue #4

If you have issue #4, then you will not be able to use the LiquidCrystal_I2C library as it is shipped as it is hardcoded for only one of the PCF8574 backpack designs.
You would either have to modify the LiquidCrystal_I2C library source code or switch to another library like the hd44780 library which can auto detect the backpack you have and self configure itself to use it.

The nice thing about using the hd44780 library hd44780_I2Cexp i/o class is it comes with a diagnostic tool (I2CexpDiag) that can help diagnose the issue and works with all the PCF8574 backpacks regardless of pin mappings used.

--- bill

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