To begin with, wiringPi
is a C library mainly used to interface with the Raspberry Pi's GPIO. The library actually includes a LCD header (it is part of the wiringPi dev library, so you must include it as wiringPiDev
). However, what wiringPi
doesn't include is a LCD header for I2C-backmounted LCDs. Most of the time, the I2C backpack module is just a GPIO expander that communicates with the LCD in 4-bit mode (that is, it only uses 4 data pins instead of 8. While data are sent at half speed, they are still set so fast that you can't tell if an LCD operates in 4-bit or 8-bit mode). We are assuming your LCD can operate in 4-bit mode and that the schematic of your I2C backpack is similar to that of https://manualzz.com/doc/8253437/i2c-lcd-backpack-schematic.
- Firstly, check if the
wiringPi
library is already installed by typing:
gpio readall
to the terminal. If you get something like:
Unable to determine board type
then type the following and retry:
cd /tmp
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
If you get a large table, then you are good to go.
Before we start coding, type sudo raspi-config
, go to interface -> I2C and enable it (you might have to restart you Pi) .Then, mount the LCD (VCC goes to 5V, GND TO GND, SDA to GPIO 2 and SDC to GPIO3). Finally, type i2cdetect -y 1
(if you are using an old Pi or get an error saying Could not open file '/dev/i2c...
, then change the 1
to 0
). If you are only having the LCD connected to the I2C interface, note the number you see. Else, unmount the LCD, run the command, mount it again and check for any differences
- Create a new file with the text editor of your preference. I prefer using
nano
, but you can use whatever you want. On the very top include:
#include <stdio.h>
#include <wiringPi.h>
#include <pcf8574.h>
#include <lcd.h>
This includes the standard IO library, the wiringPi
library, as well as the code needed to control the LCD and the PCF8574, the GPIO expander your I2C backpack uses to communicate with the LCD.
Then, define a BASE
constant (the BASE
value can be anything above 64):
#define BASE 100
Next, make the main function:
int main() {
return 0;
}
Initiate the wiringPi
library by inserting wiringPiSetupGpio()
on the very top of the function (Note: you can use any setup option you want (wiringPiSetup
, wiringPiSetupPhys
, etc...)). Also, setup the PCF8574
wiringPiSetupGpio();
pcf8574Setup(BASE, 0xADDRESS);
where ADDRESS
is the value you noted earlier, when you run the i2cdetect
command
Change pin 1 of the PCF8574 to LOW so that the LCD doesn't send back any signals that could harm it or the Raspberry Pi. Also, change pin 3 to HIGH to turn the backlight on (you can change this later if you want the backlight blink or not use it at all if you want)
digitalWrite(BASE+1, LOW); // Change the R/W bit of the LCD to LOW so that the LCD only receives data
digitalWrite(BASE+3, HIGH); // Turn the backlight of the LCD on (not necessary, but helps you read the LCD text)
Now, it is time to initiate the LCD:
int fd = lcdInit(2, 16, 4, BASE, BASE+2, BASE+4, BASE+5, BASE+6, BASE+7, 0, 0, 0, 0);
if (fd == -1) {
printf("Error while trying to initiate LCD screen. Exiting...\n");
return 1;
}
Here, we initiate the LCD in 4-bit mode using the PCF8574's pins. If something goes wrong, the lcdInit
function returns -1, so we have a check for that.
Now, you can use the LCD as you would do normally. There are many functions to try, if you wanna use them, check the official wiringPi documentation here (Handle is the value stored in the fd
variable)
- Lastly, compile the C file by typing
gcc -o output.exe cfile.c -lwiringPi -lwiringPiDev
. This command compiles thecfile.c
intooutput.exe