Skip to main content

LCD Interface Raspberry PI3

LCD with your Raspberry Pi, there’s a good chance you’ll need to program it in Python at some point. Python is probably the most popular programming language for coding on the Raspberry Pi, and many of the projects and examples you’ll find are written in Python.
In this article, I’ll show you how to program your LCD in Python, using the RPLCD library. I’ll start with showing you how to connect it in 8 bit mode and 4 bit mode. Then I’ll explain how to install the library, and provide example programs for printing and positioning text, clearing the screen, and controlling the cursor. I’ll also give you examples for scrolling text, creating custom characters, printing data from a sensor, and displaying the date, time, and IP address of your Pi.

Connecting the LCD

There are two ways to connect the LCD – 4 bit mode and 8 bit mode. 4 bit mode uses 6 GPIO pins, while 8 bit mode uses 10. Since it uses up less pins, 4 bit mode is the most common method, but I’ll explain how to set up and program the LCD both ways.
Each character and command is sent to the LCD as a byte (8 bits) of data. In 8 bit mode, the byte is sent all at once through 8 data wires. In 4 bit mode, the byte is split into two sets of 4 bits – the upper bits and lower bits, which are sent one after the other over 4 data wires.
Theoretically, 8 bit mode transfers data about twice as fast as 4 bit mode, since the entire byte is sent all at once. However, the LCD driver takes a relatively long time to process the data, so no matter which mode is being used, we don’t really notice a difference in data transfer speed between 8 bit and 4 bit modes.

Wiring the LCD in 8 Bit Mode


To connect your LCD in 8 bit mode set it up like this:
The backlight and contrast potentiometers are 10K Ohms, but they can be substituted with 1K to 3K Ohm resistors if you prefer.

Wiring the LCD in 4 Bit Mode

To connect the LCD to your Raspberry Pi in 4 bit mode, set it up like this:
The potentiometers here can also be substituted with 1K or 3K Ohm resistors.

Programming the LCD With Python

If this is your first time writing and running a Python program, you might want to read How to Write and Run a Python Program on the Raspberry Pi, which will explain everything you need to know to run the examples below.
We will be using a Python library that provides a lot of useful functions. Its called the RLPCD library, and was written by Danilo Bargen.

Installing the RPLCD Library

The RPLCD library can be installed from the Python Package Index (PyPi), or PIP. PIP might already be installed on your Pi, but if not, enter this at the command prompt to install it:
sudo apt-get install python-pip
After you get PIP installed, install the RPLCD library by entering:
sudo pip install RPLCD
The example programs below use the Raspberry Pi physical GPIO numbering system (BOARD). I’m assuming you have your LCD connected the way it is in the diagrams above, but you can easily change the pin connections.

Write to Display in 8 Bit Mode

This simple program will display “Hello world!” on the LCD. If you have a different sized LCD than the 16×2 I’m using (like a 20×4), change the number of columns and rows in line 2 of the code. cols= sets the number of columns, and rows= sets the number of rows. You can also change the pins used for the RS, E, and data pins. The data pins are set as pins_data=[D0, D1, D2, D3, D4, D5, D6, D7].

Text strings are written to the display using the lcd.write_string() function:
from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[40, 38, 36, 32, 33, 31, 29, 23])
lcd.write_string(u'Hello world!')

Write to Display in 4 Bit Mode

In 4 bit mode, only LCD pins D4, D5, D6, and D7 are used for data. These are set in pins_data=[D4, D5, D6, D7] on line 2 below:
from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
lcd.write_string(u'Hello world!')

Position the Text


The text can be positioned anywhere on the screen using lcd.cursor_pos = (ROW, COLUMN). The rows are numbered starting from 0, so the top row is row 0, and the bottom row is row one. Similarly, the columns are numbered starting at zero, so for a 16×2 LCD the columns are numbered 0 to 15. For example, the code below places “Hello world!” starting at the bottom row, fourth column:

from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
lcd.cursor_pos = (1, 3) 
lcd.write_string(u'Hello world!')



Clear the Screen

The function lcd.clear() will clear the screen. The following code will print “Hello world!” to the screen for two seconds before clearing it:




import time
from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])

lcd.write_string(u'Hello world!')
time.sleep(2)
lcd.clear()

Blinking Text

Combining lcd.clear() and time.sleep() in a while loop will produce a blinking text effect:

import time
from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])

while True:
    lcd.write_string(u"Hello world!")
    time.sleep(1)
    lcd.clear()
    time.sleep(1)

Press Ctrl-C to exit the program.

Cursor On/Off

The RPLCD library provides several functions for controlling the cursor. You can have a block cursor, an underline cursor, or a blinking cursor. Use the following functions to set the cursor:
  • Blinking block cursor: lcd.cursor_mode = CursorMode.blink
  • Line cursor: lcd.cursor_mode = CursorMode.line
  • Cursor off: lcd.cursor_mode = CursorMode.hide
The code below places a blinking cursor after the last character of text

from RPLCD import CharLCD
from RPLCD import CursorMode
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
lcd.write_string(u'Hello world!')
lcd.cursor_mode = CursorMode.blink
#lcd.cursor_mode = CursorMode.line
#lcd.cursor_mode = CursorMode.hide

Line Breaks

Text will automatically wrap to the next line if the length of your string is greater than the column length of your LCD. You can also control where a text string breaks to the next line by inserting \n\r where you want the break to occur. The code below will print “Hello” to the top row, and “world!” to the bottom row.
from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
lcd.write_string(u'Hello\n\rworld!')

Print the Date and Time

This program will display the date and time on the LCD:
from RPLCD import CharLCD
import time
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
while True:
    lcd.write_string("Time: %s" %time.strftime("%H:%M:%S"))
        lcd.cursor_pos = (1, 0)
    lcd.write_string("Date: %s" %time.strftime("%m/%d/%Y"))
    

Print Your IP Address

This program will print the IP address of your ethernet connection to the LCD. To print the IP of your WiFi connection, just change eth0 in line 19 to wlan0:
from RPLCD import CharLCD
import socket
import fcntl
import struct
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23])
def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915, 
        struct.pack('256s', ifname[:15])
    )[20:24])
lcd.write_string("IP Address:") 
lcd.cursor_pos = (1, 0)
lcd.write_string(get_ip_address('eth0'))

Custom Characters

Each character on the LCD is an array of 5×8 of pixels. You can create any pattern or character you can think of, and display it on the screen as a custom character. Check out this website for an interactive tool that creates the bit array used to define custom characters.
First we define the character in lines 4 to 12 of the code below. Then we use the function lcd.create_char(0-7, NAME) to store the character in the CGRAM memory of the LCD. Up to 8 (0-7) characters can be stored at a time. To print the custom character, we use lcd.write_string(unichr(0)), where the number in unichr() is the memory location (0-7) defined in lcd.create_char().

Comments

Popular posts from this blog

IDENTITY-BASED PROXY-ORIENTED DATA UPLOADING AND REMOTE DATA INTEGRITY CHECKING IN PUBLIC CLOUD report

IDENTITY-BASED PROXY-ORIENTED DATA UPLOADING AND REMOTE DATA INTEGRITY CHECKING IN PUBLIC CLOUD ABSTRACT More and more clients would like to store their data to PCS (public cloud servers) along with the rapid development of cloud computing. New security problems have to be solved in order to help more clients process their data in public cloud. When the client is restricted to access PCS, he will delegate its proxy to process his data and upload them. On the other hand, remote data integrity checking is also an important security problem in public cloud storage. It makes the clients check whether their outsourced data is kept intact without downloading the whole data. From the security problems, we propose a novel proxy-oriented data uploading and remote data integrity checking model in identity-based public key cryptography: IDPUIC (identity-based proxy-oriented data uploading and remote data integrity checking in public cloud). We give the formal definition, system model and se

A LOCALITY SENSITIVE LOW-RANK MODEL FOR IMAGE TAG COMPLETION

A LOCALITY SENSITIVE LOW-RANK MODEL FOR IMAGE TAG COMPLETION ABSTRACT Many visual applications have benefited from the outburst of web images, yet the imprecise and incomplete tags arbitrarily provided by users, as the thorn of the rose, may hamper the performance of retrieval or indexing systems relying on such data. In this paper, we propose a novel locality sensitive low-rank model for image tag completion, which approximates the global nonlinear model with a collection of local linear models. To effectively infuse the idea of locality sensitivity, a simple and effective pre-processing module is designed to learn suitable representation for data partition, and a global consensus regularizer is introduced to mitigate the risk of overfitting. Meanwhile, low-rank matrix factorization is employed as local models, where the local geometry structures are preserved for the low-dimensional representation of both tags and samples. Extensive empirical evaluations conducted on three

LIFI

LIFI Prof . Harald Haas is a technology of high brightness light emitting diodes(LED).It is bidirectional ,high speed and fully networked wireless communication.    LiFi is designed to use LED light bulbs similar to those currently in use in many energy-conscious homes and offices. However, LiFi bulbs are outfitted with a   chip   that modulates the light imperceptibly for optical data transmission. LiFi data is transmitted by the LED bulbs and received by photoreceptors. LiFi's early developmental models were capable of 150 megabits-per-second ( Mbps ). Some commercial kits enabling that speed have been released. In the lab, with stronger LEDs and different technology, researchers have enabled 10   gigabits -per-second (Gbps), which is faster than   802.11ad .  Benefits of LiFi: ·         Higher speeds than  Wi-Fi . ·         10000 times the frequency  spectrum  of radio. ·         More secure because data cannot be intercepted without a clear line of si