B4J Tutorial [IoT]Control a LCD display with you RaspBerry

For this tutorial you will need the Pi4J library. Get it here (under Erel's video): https://www.b4x.com/android/forum/threads/iot-jpi4j-raspberry-pi-gpio-controller.37493/#content

I did not need to install any other sw on my Pi (Noobs Image).

Hardware:

- a RaspBerry
- a cheap HD44780 compatible 2x16 (up to 4x16) display for 5-10€
- a breadboard with cables (see Ebay for it)
- your app

In you app you'll need only these lines:

B4X:
Sub Process_Globals

   Private controller As GpioController

End Sub

Sub AppStart (Args() As String)
    controller.Initialize
    Private lcd As JavaObject
    lcd.InitializeStatic("com.pi4j.wiringpi.Lcd")

    'int lcdInit(int rows, int cols, int bits, int rs, int strb, int d0, int d1, int d2, int d3, int d4,
    '     int d5, int d6, int d7);
   'Private handle As Int = lcd.RunMethod("lcdInit", Array(2,16,4,2,3,4,14,15,17,0,0,0,0))
    Private handle As Int = lcd.RunMethod("lcdInit", Array(2,16,4,8,9,7,15,16,0,0,0,0,0))
 
    lcd.RunMethod("lcdPosition", Array(handle, 0,0))
    lcd.RunMethod("lcdPuts", Array(handle, "Test LCD on Rpi"))
    lcd.RunMethod("lcdPosition", Array(handle, 0,1))
    lcd.RunMethod("lcdPuts", Array(handle, "Pi4j with B4J"))
End Sub

The display

Get a cheap one (about 5-10€)

http://www.amazon.co.uk/Fivesix-HD44780-Module-displays-characters/dp/B00WPOUCOI/ref=sr_1_1?s=electronics&ie=UTF8&qid=1447335668&sr=1-1&keywords=lcd 2x16

Wiring:

Use Google & "2x16 lcd wiring" (it would be too much text here to display). Use the 4-Bit wiring scheme. Connect the cables to GPIO-Pins "as you like" (and only those - be careful not to get 5v - the layout of the different PI's is not the same!).

Mapping of the pins to your app:

Take a look to one of my questions and how the mapping works: https://www.b4x.com/android/forum/threads/solved-iot-raspberry-wiringpi-b4j-lcd-2x16.60252/

In the code you see:

B4X:
'int lcdInit(int rows, int cols, int bits, int rs, int strb, int d0, int d1, int d2, int d3, int d4,
    '     int d5, int d6, int d7)

It depends on the display and the wiring. Assuming you have a 2x16 display you wil have 2 lines of 16 characters: Fill rows with 2 and cols with 16. As we use it in 4 Bit mode (=4 data wires) the next integer is a 4. If you use a 4x16 display it would be 4, 16 and 4.

The next ones correspond to the pins on the lcd board and our used (mapped) GPIO pins:

f.e. rs (register select) -> Pin name (it is just an alias) -> WiringPi -> Physical pin -> cable -> rs connector on the lcd and so on.

Example: See the commented line "Private handle As Int = lcd.RunMethod("lcdInit", Array (2,16,4,2,3,4,14,15,17,0,0,0,0))"

This was my first try. I plugged the lcd's "rs" port to GPIO pin #2 on the RaspBerry assuming this was #2 in B4J. As I could see (see the other thread above) the WiringPi on the RB did a remap, so my #2 is #8. Very confusing but not really if you know that "trick".

Because I did not want to change my wiring I took a look which physical (real) pin was which GPIO "alias" in WiringPi (you can get the mapping by typing 'gpio readall") and took other values which worked then.

So the new line was:
B4X:
Private handle As Int = lcd.RunMethod("lcdInit", Array(2,16,4,8,9,7,15,16,0,0,0,0,0))

"B4J"-Pin 8 -> WiringPi -> 8 = 2 -> RaspBerryHardware -> 2 -> my cable -> LCD board -> "rs"

Documentation of other methods like "lcdClear": http://pi4j.com/apidocs/com/pi4j/wiringpi/Lcd.html
 
Last edited:

Mark Turney

Active Member
Licensed User
Longtime User
Over in Cocoa Beach for a job interview @ 4pm EST. But, now I can't wait to get home and try this out! BTW - any idea on the mapping of RPi to PiFace pinouts. I just haven't looked yet. Hoping I don't have to disconnect my PiFace to implement this, but no issue if so. Figured that you may already know but if I determine a way first, I'll post the mapping.

Thanks again!
 

Mark Turney

Active Member
Licensed User
Longtime User
Forgot about those threads. Thanks again!!:)
 

Mark Turney

Active Member
Licensed User
Longtime User
I'm gonna take another stab at this. Got tied tied up getting everything ready for my new "day job". But, I never did get the 2x16 LCD working off the PiFace Digital 2 add-on board, which was my goal.
 
Top