How to control 20×4 I2C LCD from Raspberry Pi using named pipes?

Raspberry Pi

In this tutorial, I will show you, step by step, how to control 20×4 I2C LCD display from Raspberry Pi using named pipes.
Contrary to basic controllers, this solution is more flexible since it allows to control the LCD content from external scripts and programs by the means of named pipes (special file used as gateway to control the LCD).

Notice: This tutorial is based on this one. But, our solution is more powerful since it enables other scripts to control the content of the LCD through named pipes which could be accessed from various programs like python, C, C++, script shell, PHP, etc.

Prerequisites

You have to be able to:

  • Access the terminal of your Raspberry pi via SSH, VNC or directly (Standalone mode: keyboard + mouse + screen)
  • know how to solder electronic components
  • This tutorial assumes that the I2C interface is enabled in your Raspberry Pi. If not, please refer to the following Youtube tutorial to activate this interface: https://www.youtube.com/watch?v=0Ny_88I9gWM
  • In order to be able to control I2C interface from Python and debugging the I2C interface, you need to install “i2c-tools” using the following commands:


 

Necessary material

  • 1 Raspberry Pi Model B + or higher:

  • 1 LCD-module-20×4-model-2004

  • 1 IIC / I2C interface (to easily connect the screen to Raspberry)
    IIC-interface_module

  • 1 Logic Level Shifter  (Bi-Directional Four-channels)

  • Multiple female / female (or male / female) wires if you are using a breadboard. if you are going to connect the components directly, you will need only the female / female wires:

 

Soldering of components

Several vendors provide 20 × 4 LCDs pre-seolered to an IIC / I2C interface as shown in the following figure:

If you have a non-soldered LCD display at an I2C interface, you must first solder them together as shown in the figure above.

Likewise for the Logic Level Shifter, you must solder its pins to obtain the following result:

 

Hardware wiring

The following figure clearly describes the required connections between the different hardware devices:

 

Cloning the LCD Controller to Raspberry Pi

To clone the controller to your Raspberry Pi, just open a terminal, then execute the following command:


 

This command will clone the entire project inside a new folder “Control20x4LcdDisplayUsingNamedPipes”

You can rename this folder to “lcdController” using the following command:


 

How to use this controller?

In order to use this controller, you have to navigate to the folder containing “lcd_controller.py” file and add the execution permission to this file using the following command


 

Then, execute this file using the following command (in the same folder):


 

Note: You can add the following line to the crontab using the (crontab -e) command to launch the controller at the starting of the Raspberry Pi


 

how to write text on the LCD Display using this controller?

This controller is designed so that it receives the text content and formatting through named pipes.
In other words, each time we need to write text from any external script or program, we have to write a special command inside a text file which is located, by default, in the following path:


For instance, if we would like to write the expression “Hello world” in the third line of our LCD display and align it to the center, we have to write the following text to the named pipe: “3|2|Hello world

To do so, from a terminal, just execute the following command:


The general format of the command to write on the pipi file is as following:

NumberOfTheLineToWriteTo|TextAlignCode|TextToWriteInLCD

Note:

1- NumberOfTheLineToWriteTo is the number of the line of the LCD in which we intend to write to

2- TextAlignCode: here just write :

  • 1 to align the text to the lef
  • 2 to align the text to the center
  • 3 to align the text to the right

3- TextToWriteInLCD: is the text to write in the chosen line. Do not exceed 20 character per line

4- By default, the controller uses the vertical line character to separate the various fields : “|”. So, the text to write to the LCD has to not contain this special character. If so, you can edit the separator variable used in our controller and change it to another character.

Note that with this controller, you can also control the LCD back-light, you can also clear and refresh the LCD content using specific commands to be sent to the LCD named pipe:


 

If you found this tutorial useful, share it to your friends 🙂