Display characters on I2C 1602 LCD with Raspberry Pi 3 & Java

Introduction

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.

Graph the sensor information of Raspberry Pi and prepare an environment that can be checked with a Web browser

Preparation

The module used this time is a combination of 1602 LCD and PCF8574 (likely?) As an I2C module.

surface back
1602 surface 1602backside

(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.

1602.png

LCD control class

Controlling an LCD with an I2C interface uses the I2CLcdDisplay class in the com.pi4j.component.lcd.impl package.

Documentation of I2CLcdDisplay class

Create an instance

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:~ $

Method

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()
Clear the screen and move the cursor to the upper left of the screen.
void clear(int row)
Erases the specified line by overwriting the space character.
void clear(int row, int column, int length)
Erases by overwriting the space character of the specified length from the specified position, and moves the cursor behind it.
void setCursorHome()
Move the cursor to the upper left of the screen
void setCursorPosition(int row)
Moves the cursor to the beginning of the specified line.
void setCursorPosition(int row, int column)
Moves the cursor to the specified position.
void write(byte data)
void write(byte[] data)
void write(char data)
void write(char[] data)
void write(String data)
Overwrites a character or string at the cursor position and moves the cursor behind it.
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)
Overwrites a character or string from the beginning of the specified line and moves the cursor after it.
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)
Overwrites a character or string from the specified position and moves the cursor behind it.
void write(int row, String data, LCDTextAlignment alignment)
Overwrites the string left-justified, centered, or right-justified on the specified line and moves the cursor behind it. The following values can be specified for alignment.
  LCDTextAlignment.ALIGN_CENTER
  LCDTextAlignment.ALIGN_LEFT
  LCDTextAlignment.ALIGN_RIGHT
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)
Overwrites the cursor or the string at the specified position and moves the cursor behind it. For data, specify the format string, and for arguments, specify the list of objects corresponding to the format string (String)..format​(String data, Object... arguments)The string returned by is displayed).
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)
Replaces the specified line with a character string (the part other than the character string is erased by overwriting the space character).
int getColumnCount()
Returns the number of digits displayed on the screen (16).
int getRowCount()
Returns the number of lines displayed on the screen (2).
void setBacklight(boolean backlight)
Specifies whether the backlight is on (true) or off (false). The settings are not reflected immediately, but are reflected when the screen is operated (writing characters, moving the cursor, etc.).
boolean isBacklight()
Returns the current backlight on / off specified state.

Demonstration using LCD control class

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:~ $

1602Demo.JPG

Display user-defined characters

The 1602 LCD also has the ability to register character patterns (user-defined characters) programmatically and display them.

lcd.png

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

Display characters on I2C 1602 LCD with Raspberry Pi 3 & Java
Display user-defined characters on the I2C 1602 LCD with Raspberry Pi 3 & Java
Launched Redmine with Docker on Raspberry Pi 3
How to run javafx with Raspberry Pi Posted on 2020/07/12
Read temperature / humidity with Java from Raspberry Pi 3 & DHT11
Install Docker on Raspberry Pi
Put Ubuntu 20.04.1 on Raspberry Pi 4
Minecraft server on Raspberry Pi 4
Seasonal display with Java switch
Install MariaDB on Raspberry Pi OS
Install Java with zip on Windows
Try putting CentOS 8 on Raspberry Pi 3
Note: setting javaMail on Raspberry Pi
Process the motion detected by the motion sensor HC-SR501 with Raspberry Pi 3 & Java
Read barometric pressure and temperature with Java from Raspberry Pi 3 & BMP180
Headless install of Ubuntu 20.10 on Raspberry Pi
Raspberry Pi Zero with Ubuntu-style Yocto-gcc with libusb
Using JupyterLab + Java with WSL on Windows 10
I installed Docker on my Raspberry Pi 3
Java version control with jenv on OSX
Find Raspberry Pi from Android with mDNS
Install Java8 with Yum on Amazon Linux
Install CentOS 7 on Raspberry pi 4 Model B
Build OpenCV with Java Wrapper on Ubuntu 18.04
Serially connect to Ubuntu on Raspberry Pi
Challenge to deal with garbled characters with Java AudioSystem.getMixerInfo ()
Install Docker on Raspberry Pi 4 and Raspberry Pi OS 64bit
Easily switch Java versions with alias on macOS
Radiko recording server on Raspberry Pi 4 (Docker unused)
I couldn't install docker with raspberry pi2 b +.
Install Java 14 (OpenJDK: AdoptOpenJDK) on macOS with Homebrew
Display Mat type image on GUI with Swing
Memo: [Java] Get Raspberry Pi data by SFTP