Display characters on the 1602 LCD connected to the Raspberry Pi. The programming language uses Java.
We use Pi4J to control the GPIO of the Raspberry Pi from Java, which includes a generic library for controlling the LCD from the beginning. This time we will use this library.
For the Pi4J environment, refer to "Installing Pi4J" in the following article.
The module used this time is a combination of 1602 LCD and PCF8574 (likely?) As an I2C module.
surface | back |
---|---|
(Reference) AMC1602A-I2C data sheet
Since this module communicates via I2C, the I2C communication function of the Raspberry Pi is enabled (it is disabled by default).
To enable it from the Raspbian GUI, open the settings panel from "Settings"-"Raspberry Pi Settings" from the "Menu" button on the taskbar, and select the "Interface" tab to enable "I2C". To enable it from the command line, enter the sudo raspi-config command, select Interfacing Options> I2C, and finally select Yes.
After setting, reboot just in case.
The Raspberry Pi and 1602 LCD were connected as follows.
Controlling an LCD with an I2C interface uses the I2CLcdDisplay class in the com.pi4j.component.lcd.impl package.
Documentation of I2CLcdDisplay class
To control the LCD programmatically, first create an instance of the I2CLcdDisplay class. The constructor has the following format.
python
I2CLcdDisplay(int rows, int columns, int i2cBus, int i2cAddress, int backlightBit, int rsBit, int rwBit, int eBit, int d7, int d6, int d5, int d4)
In addition to the size of the LCD display area, the I2C bus number to be used, and the module address, the constructor must specify the bit configuration for interacting with the module via I2C. Specify the 3rd parameter (i2cBus) and 4th parameter (i2cAddress) according to your environment. In the module used this time, specify the following values for other parameters to create an instance.
python
I2CLcdDisplay lcd = new I2CLcdDisplay(2, 16, 1, 0x27, 3, 0, 1, 2, 7, 6, 5, 4);
Use the following command to check the module address specified in the 4th parameter. It seems that some of the I2C modules used are recognized as 3F.
pi@raspberrypi:~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
pi@raspberrypi:~ $
The following methods are defined in the I2CLcdDisplay class. Those used in internal processing and those inherited from higher classes that are considered unnecessary are omitted.
When specifying the row and column positions as parameters, specify the values counted from 0 for each.
Methods and features |
---|
void clear() |
|
void clear(int row) |
|
void clear(int row, int column, int length) |
|
void setCursorHome() |
|
void setCursorPosition(int row) |
|
void setCursorPosition(int row, int column) |
|
void write(byte data) void write(byte[] data) void write(char data) void write(char[] data) void write(String data) |
|
void write(int row, byte data) void write(int row, byte[] data) void write(int row, char data) void write(int row, char[] data) void write(int row, String data) |
|
void write(int row, int column, byte data) void write(int row, int column, byte[] data) void write(int row, int column, char data) void write(int row, int column, char[] data) void write(int row, int column, String data) |
|
void write(int row, String data, LCDTextAlignment alignment) |
|
void write(String data, Object... arguments) void write(int row, String data, Object... arguments) void write(int row, String data, LCDTextAlignment alignment, Object... arguments) void write(int row, int column, String data, Object... arguments) |
|
void writeln(int row, String data) void writeln(int row, String data, LCDTextAlignment alignment) void writeln(int row, String data, LCDTextAlignment alignment, Object... arguments) void writeln(int row, String data, Object... arguments) |
|
int getColumnCount() |
|
int getRowCount() |
|
void setBacklight(boolean backlight) |
|
boolean isBacklight() |
|
I created a demo program using the classes and methods introduced above. If you want to actually run it, modify the bold I2C bus number and module address below according to your environment as mentioned in the "Create Instance" section. ..
I2CLcdDisplay lcd = new I2CLcdDisplay(2, 16, 1, 0x27, 3, 0, 1, 2, 7, 6, 5, 4);
I2C1602LCDDemo.java
import java.text.SimpleDateFormat;
import java.util.Date;
import com.pi4j.component.lcd.LCDTextAlignment;
import com.pi4j.component.lcd.impl.I2CLcdDisplay;
public class I2C1602LCDDemo {
public static final int LCD_ROW_1 = 0;
public static final int LCD_ROW_2 = 1;
public static final String DEMO_TITLE = "[I2C 1602 DEMO]";
public static void main(String[] args) throws Exception {
// for 1602 LCD + PCF8574 I2C module
I2CLcdDisplay lcd = new I2CLcdDisplay(2, 16, 1, 0x27, 3, 0, 1, 2, 7, 6, 5, 4);
lcd.write(LCD_ROW_1, DEMO_TITLE);
Thread.sleep(3000);
lcd.clear();
Thread.sleep(1000);
// show all characters
for (int ch = 0x10; ch <= 0xFF; ch += 4) {
lcd.write(LCD_ROW_1, "%02XH %02XH %02XH %02XH", ch, ch + 1, ch + 2, ch + 3);
lcd.write(LCD_ROW_2, " %c %c %c %c", ch, ch + 1, ch + 2, ch + 3);
Thread.sleep(800);
}
// show characters at specified positions
lcd.setCursorHome();
for (int column = 0; column < lcd.getColumnCount(); column++) {
lcd.write((byte)0xFF);
Thread.sleep(100);
}
for (int column = lcd.getColumnCount() - 1; column >= 0; column--) {
lcd.write(LCD_ROW_2, column, (byte)0xFF);
Thread.sleep(100);
}
lcd.setCursorHome();
for (int column = 0; column < lcd.getColumnCount(); column++) {
lcd.write(DEMO_TITLE.charAt(column));
Thread.sleep(100);
}
for (int column = lcd.getColumnCount() - 1; column >= 0; column--) {
lcd.write(LCD_ROW_2, column, ' ');
Thread.sleep(100);
}
// show text alignment
lcd.writeln(LCD_ROW_2, "< LEFT", LCDTextAlignment.ALIGN_LEFT);
Thread.sleep(2000);
lcd.writeln(LCD_ROW_2, "RIGHT >", LCDTextAlignment.ALIGN_RIGHT);
Thread.sleep(2000);
lcd.writeln(LCD_ROW_2, "< CENTER >", LCDTextAlignment.ALIGN_CENTER);
Thread.sleep(2000);
// show clock
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
while (true) {
lcd.writeln(LCD_ROW_2, formatter.format(new Date()), LCDTextAlignment.ALIGN_CENTER);
Thread.sleep(1000);
}
}
}
To run this program, put the above file in a directory of your choice and compile it.
pi@raspberrypi:~ $ pi4j -c I2C1602LCDDemo.java
--------------------------------------------
Pi4J - Compiling: I2C1602LCDDemo.java
--------------------------------------------
+ javac -classpath '.:classes:*:classes:/opt/pi4j/lib/*' -d . I2C1602LCDDemo.java
pi@raspberrypi:~ $
After compiling, execute it with root privileges. If the characters are not displayed, try adjusting the contrast by turning the + knob on the back of the module with a screwdriver.
When the demo is over, the clock will be displayed, so press Ctrl + C to exit.
pi@raspberrypi:~ $ sudo pi4j I2C1602LCDDemo
+ java -classpath '.:classes:*:classes:/opt/pi4j/lib/*' I2C1602LCDDemo
^Cpi@raspberrypi:~ $
The 1602 LCD also has the ability to register character patterns (user-defined characters) programmatically and display them.
Please also refer to the article below that extends the class of the general-purpose library introduced this time to enable this function.
Display user-defined characters on I2C 1602 LCD with Raspberry Pi 3 & Java
Recommended Posts