-1

I am an old java programmer, translating code from Desktop to Raspberry Pi, with the aim of embedding software in a hardware interface.

I wired a 16*2 character LCD display, which worked with Python code, however when I use pi4j libraries to access the GPIO via Java, the screen is blank.

Am I missing a some binary on/off switch?

I am running pi4j 1.2, on an A+ Pi, got over the 1.1 processor bug that affected wiring Pi.

Thanks for reading, any suggestions are appreciated.

import com.pi4j.component.lcd.LCDTextAlignment;
import com.pi4j.component.lcd.impl.GpioLcdDisplay;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.system.NetworkInfo;

public class LCD {

    public static void main(String args[]) {
        System.out.println("SYSTEM PRINT TEST");
        GpioController gpio = GpioFactory.getInstance();
        GpioLcdDisplay lcd = new GpioLcdDisplay(2,16,
            RaspiPin.GPIO_26,
            RaspiPin.GPIO_31,
            RaspiPin.GPIO_15,
            RaspiPin.GPIO_16,
            RaspiPin.GPIO_01,
            RaspiPin.GPIO_04);

        lcd.clear();
        Thread.sleep(1000);

        lcd.write(0, "LINE 1 TEST");
        lcd.write(1, "LINE 2 TEST");

        Thread.sleep(2000);
        gpio.shutdown();
    }
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
tietaja
  • 21
  • 3

2 Answers2

2

After spending several hours banging my head on the desk, I decided on this Google search

google.com >> raspberry pi java gpio not working python works

That led me to this question, which for some reason was down voted 3 times, but it was exactly what I was experiencing.

The second result of my search was this question:

Raspberry Pi4 with Pi4j Java

That clued me in on the fact that I needed to add this line of code because pi4j uses a different PIN layout by default.

GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));

That wasn't enough, but that question led me to this question/answer:

Raspberry pi 4 controle GPIO with java

Which explained I needed to update my gpio binary to v2.52


This sample Python code was working without issue:

https://maker.pro/raspberry-pi/projects/controlling-a-dc-motor-with-raspberry-pi4-1

import RPi.GPIO as GPIO
from time import sleep

# Pins for Motor Driver Inputs 
Motor1A = 20
Motor1B = 16
Motor1E = 21
 
def setup():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)              # GPIO Numbering
    GPIO.setup(Motor1A,GPIO.OUT)  # All pins as Outputs
    GPIO.setup(Motor1B,GPIO.OUT)
    GPIO.setup(Motor1E,GPIO.OUT)
 
def loop():
    # Going forwards
    GPIO.output(Motor1A,GPIO.HIGH)
    GPIO.output(Motor1B,GPIO.LOW)
    GPIO.output(Motor1E,GPIO.HIGH)
    print("Going forwards")
 
    sleep(5)
    # Going backwards
    GPIO.output(Motor1A,GPIO.LOW)
    GPIO.output(Motor1B,GPIO.HIGH)
    GPIO.output(Motor1E,GPIO.HIGH)
    print("Going backwards")
 
    sleep(5)
    # Stop
    GPIO.output(Motor1E,GPIO.LOW)
    GPIO.output(Motor1B,GPIO.LOW)
    print("Stop")

def destroy():  
    GPIO.cleanup()

if __name__ == '__main__':     # Program start from here
    setup()
    try:
            loop()
    except KeyboardInterrupt:
        destroy()

But my attempts at using the Pi4J Java library were failing miserably.

This is my working Java code:

//This line was initially not here, it is part of the solution
GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));

gpio = GpioFactory.getInstance();
    
in2 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_16, "IN2", PinState.LOW);
in1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_20, "IN1", PinState.LOW);
en = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_21, "EN", PinState.LOW);
    
//Going forwards
in1.high();
in2.low();
en.high();
   
Thread.sleep(5000);
       
//Going backwards
in1.low();
in2.high();
en.high();
    
Thread.sleep(5000);
    
// stop           
en.low();
in2.low();
    
gpio.shutdown();
jawsware
  • 924
  • 6
  • 13
0

The discrepancy between the pin numbering on this jargon code, and the original underlying wiringPi numbering was the cause of this frustration. Here is the revised code, where gpio 25 corresponds to wiringPi 6, not 26! Remember to update wiringPi and pi4j to the latest versions.

import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.Lcd;

public class LCD {

    public final static int LCD_ROWS = 2;
    public final static int LCD_COLUMNS = 16;
    public final static int LCD_BITS = 4;

    public static  void main(String args[]) {

        System.out.println("SYSTEM PRINT TEST");

        if (Gpio.wiringPiSetup() == -1) {
            System.out.println("GPIO SETUP ERROR");
            return;
        }

        int lcdHandle= Lcd.lcdInit(LCD_ROWS,
                                   LCD_COLUMNS,
                                   LCD_BITS,
                                   6,
                                   5,
                                   15,
                                   16,
                                   1,
                                   4,
                                   0,
                                   0,
                                   0,
                                   0);

        if (lcdHandle == -1) {
            System.out.println("LCD INIT ERROR");
            return;
        }

        Lcd.lcdDisplay(lcdHandle,1);
        Lcd.lcdClear(lcdHandle);

        Lcd.lcdPuts (lcdHandle, "LCD TEST LINE 1") ;

        Lcd.lcdPosition (lcdHandle, 0, 1) ; 
        Lcd.lcdPuts (lcdHandle, "LCD TEST LINE 2") ;

try {
     Thread.sleep(10000);
    } catch (Exception e) {}
     Lcd.lcdDisplay(lcdHandle,0);  

    }
}
tietaja
  • 21
  • 3