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.
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
This is just a guess but you have a 16×4 display but your lcdInit call specifies 20 columns.
try changing that to 16
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.
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
16×4 display 3 and 4 row corectly