Communicating with Arduino using Java

When I got a new 16×2 LCD, one of the projects that came to my mind was sending info to it by serial communication. In this project I will be using java because of the ease of making a GUI (Graphical User Interface).  I set out to get a suitable library for serial communication in java for windows. My search was fruitful as I came across for various choices, from which I eventually selected “jssc (Java Simple Serial Connector)” by Alexey Sokolov. The steps I followed and the results I got are:

JAVA PART

  1. Download the jssc library from https://code.google.com/archive/p/java-simple-serial-connector/downloads. I used the jSSC-2.7.0-Release.
  2. Unzip your download. You should get the files shown below.

3. Fire up your Netbeans IDE. Start a new Project, I named mine “NetBeans Serial Test”. Right click on project and select properties. A ‘Project Properties’ video pops up, click “Add JAR/Folder” and select jssc.jar you downloaded.

4. After this insert the code below in your main method, compile.

// TODO code application logic here

String lcd_text ="A simple serial App";

SerialPort serialPort = new SerialPort("COM4"); // Input port for Arduino

try {

serialPort.openPort(); //Open port

//set parameters below

serialPort.setParams(SerialPort.BAUDRATE_9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE,false,false);

//code to indicate maximum characters that screen can take

if(lcd_text.length()>32){ lcd_text ="String must be  less than 32"; }

//writing to arduino

serialPort.writeBytes(lcd_text.getBytes());

//close of port

serialPort.closePort();

//

System.out.println("Done");

}

catch (SerialPortException ex){

System.out.println(ex);

}

5. Note if your Arduino is not plug it will throw an error. Also nothing will happen until you do the next part. Which is programming the Arduino.

ARDUINO PART

  1. Run the Arduino IDE, make sure your Arduino is connected.
  2. Follow the connection below. Adjust potentiometer to change contrast.

3. Copy the code below and upload.

/*
Edited by Lawani Oluwasheun ; 06/Jan/19
LiquidCrystal Library - Serial Input
Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch displays text sent over the serial port
(e.g. from the Serial Monitor) on an attached LCD.

The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplay

*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin

// with the arduino pin number it is connected to

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

//character counter

int char_count=0;

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// initialize the serial communications:

Serial.begin(9600);

lcd.write("Ready");

}

void loop() {

// when characters arrive over the serial port...

if (Serial.available()) {

// wait a bit for the entire message to arrive

delay(100);

// clear the screen

lcd.clear();

// read all the available characters

while (Serial.available() > 0) {

// display each character to the LCD

lcd.write(Serial.read());

//increase character count

char_count=char_count+1;

if(char_count>=16){

lcd.setCursor(0,1); //move to next line

//reset character counter

char_count=0;

}

}

//reset character counter

char_count=0;

// lcd.print(char_count); **debug

}

}

FINAL PART

  1. After completing the Arduino part run the Java program. The result is as seen below.

Leave a comment

Your email address will not be published. Required fields are marked *

Categories