Operate an I2C-connected display from Python

Overview

Operate the LCD connected to RaspberryPi via I2C from Python. This time, we will use an LCD with the SpikenzieLabs MPTH attached to the HD44780.

Preparation

It is necessary to prepare to use I2C on the RaspberryPi side. First, add the following two lines to / etc / modules.

i2c-bcm2708
i2c-dev

Next, delete / comment out the following line from /etc/modprobe.d/raspi-blacklist.conf.

blacklist i2c-bcm2708

Reboot once, then install ʻi2c-toolsandpython-smbus`.

sudo apt-get install i2c-tools python-smbus

Obtaining I2C bus number and device address

First, get the bus number and address of the LCD to be used. Execute sudo i2cdetect 0 and sudo i2cdetect 1, The one with no error is the bus number (0 or 1), and the address is obtained from the output. For example, if the output is as follows, 0x20 is the address.

WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-1.
I will probe address range 0x03-0x77.
Continue? [Y/n] y
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- — -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Actual communication

The smbus library is used for I2C communication. Get the connection object with smbus.SMBus (bus_number) and send the data with write_i2c_block_data. This method takes ʻaddr, cmd, vals. ʻAddr is the device address. cmd and vals are different, putting the first byte of the transmitted data in cmd and the rest in vals. This is because it is necessary to send at least 1 byte (Reference) ..

import smbus

bus = smbus.SMBus(bus_number)
s = "abcde"

bus.write_i2c_block_data(addr, ord(s[0]), ord(s[1:]))

The HD44780 cannot read the control sequence for some reason, so use the command provided by MPTH to clear it.

Special commands are called after 0x80. The screen clear is 0x05 0x01, so

bus.write_i2c_block_data(addr, 0x80, [0x05, 0x01])

Will be.

Library

I created a library that summarizes these. You can call it from another program or try it from the command line. In both cases, it is necessary to execute with root privileges.

Use on the command line

The format is as follows.

sudo ./i2cdisplay.py <bus> <addr> {write|clear|cursor|backlight}

<bus> and <addr> are the bus number and device address, respectively. The subcommands are character output, screen clearing, cursor display switching, and backlight adjustment, respectively.

Character output

The output of characters is sudo ./i2cdisplay.py <bus> <addr> write <input> +. If multiple character strings are passed to <input> separated by spaces, the output will be divided into multiple lines. For example, if you want to display abc on the first line and def on the second line,

sudo ./i2cdisplay.py <bus> <addr> write "abc" "def"

It becomes.

Clear screen

sudo ./i2cdisplay.py <bus> <addr> clear

Cursor display switching

#Show cursor
sudo ./i2cdisplay.py <bus> <addr> cursor True 
#You want to hide
sudo ./i2cdisplay.py <bus> <addr> cursor False

Backlight adjustment

#Strength is 0-Choose from 255 (128 in the example below)
sudo ./i2cdisplay.py <bus> <addr> backlight 128

Use as a library

#Initialization
d = MPTHDisplay(<bus>, <addr>)

#Character output
d.write("abc")

#new line
d.newline()

#clear
d.clear()

#Multi-line output
d.writelines("abc", "def")

reference

-I tried using "I2C LCD Breakout" on Raspberry Pi! | Switch Science Magazine

Recommended Posts

Operate an I2C-connected display from Python
Operate Filemaker from Python
Operate neutron from Python!
Operate LXC from Python
Operate DynamoDB from Python like SQL.
Operate Maya from an external Python interpreter using the rpyc module
Scraping from an authenticated site with python
Operate Sakura's cloud object storage from Python
sql from python
[Python] [Excel] Operate an Excel sheet from Python using openpyxl (using a test sheet as an example)
MeCab from Python
Control Akizuki's I2C-connected OLED display from Raspberry Pi
[Python pandas] Create an empty DataFrame from an existing DataFrame
Generate an insert statement from CSV with Python.
Operate the schedule app using python from iphone
An easy way to call Java from Python
Use thingsspeak from python
Touch MySQL from Python 3
Operate Redmine using Python Redmine
Operate mongoDB from python in ubuntu environment ① Introduction of mongoDB
Use fluentd from python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Use MySQL from Python
[AWS] Operate SQS from SDK (send / receive) [Python] [Node.js]
Run python from excel
Install python from source
Operate Kinesis with Python
Execute command from Python
Use MySQL from Python
Operate Blender with Python
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Execute command from python
Operate Excel with Python (1)
[Python] Read From Stdin
Use mecab-ipadic-neologd from python
Operate Excel with Python (2)
[Python] Send an email from gmail with two-step verification set
I tried sending an email from Amazon SES with Python
Try to operate an Excel file using Python (Pandas / XlsxWriter) ①
Try to operate an Excel file using Python (Pandas / XlsxWriter) ②
Flatten using Python yield from
Call CPLEX from Python (DO cplex)
Deep Python learned from DEAP
Operate Excel with Python openpyxl
Post from Python to Slack
Quicksort an array in Python 3
Operate TwitterBot with Lambda, Python
Grammar features added from Python3.6
Cheating from PHP to Python
Make MeCab available from Python3
Display UTM-30LX data in Python
Information obtained from tweet_id (Python)
OCR from PDF in Python
Creating an egg with python
Run illustrator script from python
Use MySQL from Anaconda (python)
Python is an adult language