USB Communication between Arduino and computer using Java

By dave | January 1, 2017

control board assembled

In this article I present a simple method for communicating between an Arduino board and a PC using USB serial. USB serial is available on nearly all Arduino based boards. Further, there are Java libraries available that make communication relatively simple. In this case we use the Neuron Robotics serial library that is available via maven and hosted on git hub.

Consider this project as a starting point that builds out the basics needed to get communication working between the two devices. Once this is working it should be quite easy to extend to your needs.

This will require that you have the following components to hand:

  • Arduino device with breadboard and jumper wire
  • 20x4 LCD display
  • some microswitches & 4k7 resistors
  • USB Connection to a computer
  • Java version 8 on the computer
  • The data-comms project sourcecode
  • A suitable Java IDE, I recommend IntelliJ IDEA

Once you have all of the above, firstly you’ll want to wire a 20x4 display to the Arduino as per the link. Take note of the ports used for the various connectors as they will be needed to set up the sketch later.

switch circuit diagram Now wire the switches in as well, each switch should be wired to the board as per the circuit diagram on the left (never use less that a 4k7 resistor for this or the current drain will increase). At 4k7 the current drain for each switch when on is about 1mA; which is not particurly high even with a few switches normally on.

At this point you should have the 20x4 display connected and a few switches connected according the circuit above. Now is the time to load the code onto the Arduino device. Within the source code you’ll see a directory called arduino/commandServerRS232, this is the Arduino code, copy it into your Arduino folder, normally under the home area or Documents on Windows. In the script there are some changes that need to be made, in order to tell the script where to expect the LCD display and also the buttons.

Setting up the arduino sketch to run

At this point we need the pins used for the display and switches noted earlier. Change the constants at the top of the sketch as shown in the example below (using your own settings). In my example I had the buttons connected on bins 24-26.

#define BUTTON1_PIN  24
#define BUTTON2_PIN  25
#define BUTTON3_PIN  26

For my case, I set the comms to run at 9600, over USB it can reliably run a lot faster.

// this is the speed at which to initiate the serial over USB connection 
#define PORT_SPEED 9600

Lastly, indicate which ports you wired up your LCD display to.

// this is the standard configuration of the LCD display. Change the pin assignments
#define RS_PIN 28
#define ENABLE_PIN 29
#define D0_PIN 30
#define D1_PIN 31
#define D2_PIN 32
#define D3_PIN 33
LiquidCrystal lcd(RS_PIN, ENABLE_PIN, D0_PIN, D1_PIN, D2_PIN, D3_PIN);

As long as all is wired up correctly you should be able to execute the script, the display should initialise and look like the example below. If not, switch off the Arduino and check your wiring and code carefully.

example of arduino display

The Java part of the project

Assuming you’ve followed the above steps, at this point you’ll need to build the Java source, and I recommend an IDE for this (although you could use Maven on the command line). If you are using IntelliJ or eclipse, then you should be able to import the maven (pom.xml) as a new project. In that case no further steps will be needed and all dependencies will be obtained automatically as per the maven POM.

There are several examples contained within the code, you want to look in the rs232commands package. In order to run the project, run the class:


comms initiate screen main comms screen

If all is working at this point you should see a dialog requesting the port and speed (above left), choose the Arduino port and press connect. Once done you should see the main screen (above right), Connection status is shown in the lower left corner, and the status of the last command sent on the lower right.

To send a text message to the server, type in the text box and press send, the text will be transmitted to the server and displayed on the Arduino display. Likewise if you press any of the switches on the Arduino, they will show on the Java UI at the next status interval (set to approx 1 second on the sketch). If you tick any of the combo boxes on the Java application, you’ll see the L1 - 5 change on the Arduino display.

More about the Java application

The Java application is split into two parts, User interface and data communication components, the communication components are in the protocol package, while the user interface parts are in the ui package. SimpleRS232ArduinoController is where the communication to / from the Arduino take place. It implements the interface ArduinoInterfaceControl of which there is another implementation that simulates the Arduino, for running the application without a board.

There are two Windows in the application, both are Java FX based and use the FXML format. They can be loaded into Scene Builder and designed graphically. There is a resources directory that contains both of the FXML design files and a logo.

I hope this has given you insight into communicating with an Arduino device, this article only scratches the surface but should help you understand the process.

comments powered by Disqus
We use cookies to analyse traffic and to personalise content and adverts. Our social buttons may also use cookies.