skip to Main Content

I’m trying to make a simple c++ code to display 4 text contents, each in 4 separate rows. I’m using the latest version of Raspberry Pi OS (Debian Buster), WiringPi 2.52 and 16×4 LCD Display(5v – 1604A LCD screen).

Here is my code.

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <cstdlib>
#include <wiringPi.h>
#include <lcd.h>
#include <string.h>

using namespace std;

#define LCD_RS 30 //reset pin
#define LCD_E 10  //enable pin 
#define LCD_D4 23 //data pin 4
#define LCD_D5 22 //data pin 5
#define LCD_D6 21 //data pin 6
#define LCD_D7 14 //data pin 7

int lcd;

int main()
{
    wiringPiSetup();
    lcd = lcdInit(4, 20, 4, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0, 0, 0, 0);
    lcdPosition(lcd, 0, 0);
    lcdPuts(lcd, "Test Row 1");
    lcdPosition(lcd, 0, 1);
    lcdPuts(lcd, "Test Row 2");
    lcdPosition(lcd, 1, 2);
    lcdPuts(lcd, "Test Row 3");
    lcdPosition(lcd, 1, 3);
    lcdPuts(lcd, "Test Row 4");
    cout << "LCD Displays" << endl;
    sleep(30);
    lcdClear(lcd);
    cout << "LCD Clear" << endl;
    return 0;
}

But when I run this code, it shows the result perfectly in line 1 & 2 in LCD Display except line 3 & 4.

I’ve put the result below to get an idea.

End Result in LCD Display

From the above image, the first two lines are print perfectly, but in line 3 & 4, when I trying to print them in column 2, it prints in column 6 (lcdPosition(lcd, 5, 2)). I can’t figure out the exact reason of this problem. Can someone help me to figure this out? Thanks.

2

Answers


  1. This is just a guess but you have a 16×4 display but your lcdInit call specifies 20 columns.

     lcd = lcdInit(4, 20, 4, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0, 0, 0, 0);
    

    try changing that to 16

     lcd = lcdInit(4, 16, 4, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0, 0, 0, 0);
    

    Digging into code this seems like a bug in lcd.c from WiringPi. I found people discussing similar issues on another library and looking at the code in lcd.c:84 it has the same offset and seems to have no handling for 16 vs 20 cols.

    As WiringPi is deprecated I leave this up to the individual developer how to best fix.

    Login or Signup to reply.
  2. I have the same problem with my 16×4 display, but I rezolved it.
    I declared the start point to -4 digits for the row 3 and 4.
    The library accept negative pozition for start.
    Here is my code that works well for 16×4 display

    //lcd.setCursor(4,0);
    //lcd.print("CNC-Pro");
    //lcd.setCursor(3,1);
    //lcd.print("Plasma THC");
    //lcd.setCursor(-4,2);
    //lcd.print("Andrei");
    //lcd.setCursor(-4,3);
    //lcd.print("Apr 2023");
    

    16×4 display 3 and 4 row corectly

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search