Python port of the LiquidCrystal_I2C C++ library for Hitachi HD44780
Project description
LiquidCrystal_I2C-linux
This repository is a python port of the Arduino LiquidCrystal_I2C library.
With this library you can connect your I2C lcd to your linux box (using available GPIOs, but also via the I2C pins of your VGA port) and control it like you did with the C++ library.
Table of Contents
Supported devices
This library by default will handle most common type of character lcds based on the Hitachi HD44780 with PCF8574 I2C backpack:
40x2, 20x4, 20x2, 16x2, 16x1 (type 2), 16x4
displays not supported by this:
- 16x1 (type 1), This uses a discontigous memory for the single line (It's not particularly difficult to make it work, but you have to use it as an 8x2 display)
- 40x4 is dual 40x2 displays using dual E signals which is not supported
See here for further explanation of lcd memory addressing: http://web.alfredstate.edu/weimandn/lcd/lcd_addressing/lcd_addressing_index.html
Installation
-
Use
raspi-config
to enable the I2C interface (only for Raspberry Pi and similar boards) -
Enable i2c kernel module
sudo modprobe i2c-dev
- Add your user to i2c group
sudo usermod -a -G i2c $(whoami)
To apply group change, depending on the case, you may need to:
- Close and reopen your terminal window (if you are in a desktop environment)
- Log-out and log-in again in your tty session (if you use a computer without a desktop environment)
- Restart your ssh session (if you are connected to a remote device, maybe a Raspberry)
- List all available I2C buses
ls /dev/i2c-*
- Scan for devices on the first bus
i2cdetect -y 0
-
Connect your device
-
Scan again the same bus and look for new devices. If you see a new device you're done, otherwise repeat from step 4, scanning the other available buses until you find your device. (each bus corresponds to a physical connection so, if you change the device, you don't need to scan all the buses)
If you still can't find it try checking the cables and trying again. -
Installing the library
python3 -m pip install liquidcrystal_i2c-linux
Implementation
Getting Started
Basic commands:
-
Initialization
from liquidcrystal_i2c import LCD
Initialize the lcd using bus and address you found before.
If cols and rows are not specified, library assumes 16x2.lcd = LCD(bus=1, addr=0x3e, cols=16, rows=2)
At this point the lcd is already cleared and the cursor set to home, so we don't need to call lcd.clear() and lcd.home() again.
However when you need to clear all and place the cursor at 0,0 use:lcd.clear() lcd.home()
-
Printing
Place the cursor where you want to print (remember numbers start from zero so 0 will be the first column/row).lcd.setCursor(3,1) lcd.print("Hello, world!")
-
Printing special characters
Thanks to The Raspberry Pi guy's library we have two methods to print special characters:- classic LiquidCrystal_I2C way
lcd.setCursor(2,1) lcd.write(0xF7) #prints π symbol
- new The Raspberry Pi guy's way
lcd.setCursor(2,1) lcd.printExt("Hello {0xF7}")
Warning: if you use this method with string
format
you need to escape the placeholder using double curly brackets.lcd.setCursor(2,1) lcd.printExt("Hello {{0xF7}} = {0}".format(3.14))
-
Printing custom characters
The HD44780 allows to define 8 custom characters that you can load in CGRAM and call like any other special character. The 8 character slots are numbered from 0 to 7, their placeholders are obviously{0x00},{0x01},{0x02},{0x03},{0x04},{0x05},{0x06},{0x07}
.# define custom character bitmap # you can design the character using https://maxpromer.github.io/LCD-Character-Creator/ but keep in mind that byte definitions are different between C++ and Python custom_char_cpu = [ 0b01010, 0b11111, 0b10001, 0b10101, 0b10001, 0b11111, 0b01010, 0b00000 ] # now it's time to load the character # we will use slot 3 lcd.createChar(3, custom_char_cpu) # print the character as all special characters lcd.setCursor(0,0) lcd.printExt("This is a CPU: {0x03}")
Other commands:
- Turn backlight on/off
lcd.backlight() lcd.noBacklight()
- Show/hide cursor
lcd.cursor() lcd.noCursor()
- Turn cursor blinking on/off (needs cursor to be enabled)
lcd.blink() lcd.noBlink()
- Show hide all text
lcd.display() lcd.noDisplay()
- Scroll the entire display by one place left/right without resending strings
lcd.scrollDisplayLeft() lcd.scrollDisplayRight()
- Set autoscroll on/off
lcd.autoscroll() lcd.noAutoscroll()
- Set writing direction
lcd.leftToRight() lcd.rightToLeft()
Systemd
Use the following procedure to run any LCD Python script as a (systemd) service:
-
Create a new unit file in
/lib/systemd/system/
calledi2c-lcd.service
:sudo nano /lib/systemd/system/i2c-lcd.service
-
Copy and paste the following in the new unit file:
[Unit] Description=Python script for an hd44780 LCD [Service] Type=simple ## Edit the following according to the script permissions User=<YOUR-USERNAME> #Group=users ## Edit the following with the full path to your script ExecStart=/usr/bin/python3 /path/to/script.py Restart=always RestartSec=5 KillMode=process KillSignal=SIGINT [Install] WantedBy=multi-user.target
-
Enable the service and start it:
sudo systemctl enable i2c-lcd.service sudo systemctl start i2c-lcd.service
-
Check that the LCD is displaying the correct information; otherwise, check the service status:
systemctl status i2c-lcd.service
Thanks
I'd like to thank the creators of the C++ library for their awesome work, and The Raspberry Pi guy for the printExt
function, derived from his lcd_display_extended_string
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file liquidcrystal_i2c-linux-1.0.0.tar.gz
.
File metadata
- Download URL: liquidcrystal_i2c-linux-1.0.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bfa59b6c8c55971cd68ec8e1fe31832fb364cf9d9ce8e64cbc399def6983e5c |
|
MD5 | b5d3ee34ad4ab04ce5153104e506b0c8 |
|
BLAKE2b-256 | a0e84eae21025b3b3dfdf61b48868f8e0a9807dada0eade7dfb312e0dd8557f7 |
File details
Details for the file liquidcrystal_i2c_linux-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: liquidcrystal_i2c_linux-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7c6aceb1bb144747fad9ce1b04ed963a35b527e356bd7feccea166e2991b16c |
|
MD5 | 068cd97c5005c52663908bb3060b83ac |
|
BLAKE2b-256 | fbab27c9daf23fad447c6d75ecbfa14ba3df143d0af4290271e4bdd85734d921 |