javaraspberry-pipi4j

Raspberry Pi Java Pi4j gpio isn't working for me, works with python though


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();
    }
}

Solution

  • 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);  
    
        }
    }