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.
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-toolsand
python-smbus`.
sudo apt-get install i2c-tools python-smbus
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: -- -- -- -- -- -- -- --
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.
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.
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.
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.
sudo ./i2cdisplay.py <bus> <addr> clear
#Show cursor
sudo ./i2cdisplay.py <bus> <addr> cursor True
#You want to hide
sudo ./i2cdisplay.py <bus> <addr> cursor False
#Strength is 0-Choose from 255 (128 in the example below)
sudo ./i2cdisplay.py <bus> <addr> backlight 128
#Initialization
d = MPTHDisplay(<bus>, <addr>)
#Character output
d.write("abc")
#new line
d.newline()
#clear
d.clear()
#Multi-line output
d.writelines("abc", "def")
-I tried using "I2C LCD Breakout" on Raspberry Pi! | Switch Science Magazine
Recommended Posts