Skip to content

Tag: Dave Brink

Arduino and Java

Arduino

Arduino is a free popular platform for embedded programming based on a simple I/O board easily programmable. Interfacing it with Java allow us to create sophisticated interfaces and take advantages from the several API available in the Java ecosystem.

I’m following the original Arduino and Java interfacing tutorial by Dave Brink but in a more practical approach and with more details.

Step 1) Install the Arduino IDE

This is not a completely mandatory step but it will easy a lot our work. Our program will borrow some Arduino IDE libraries and configurations  like which serial port it is using and at which boud rate. At the moment I wrote this tutorial the version of Arduino IDE was 0013.

Step 2) Prepare your Arduino

Connect your Arduino to the serial port in your computer. Here I’m connecting my Arduino with my laptop throught a USB.

Arduino

Make sure your Arduino IDE is configured and communicating well if your Arduino. Let put on it a little program that sends to us a mensage:

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println("Is there anybody out there?");
  delay(1000);
}

Step 3) Install RXTX Library

We will use some libraries to acess the serial port, some of them relies on binary implementations on our system. Our first step is to install the RXTX library (Java CommAPI) in your system. In a Debian like Linux you can do that by:

sudo apt-get install librxtx-java

Or using a graphical package tool like Synaptic:

installing rxtx

For others systems like Windows see the RXTX installation docs.

Step 4) Start a new NetBeans project

Again, this is not a mandatory step but will easy a lot our work. NetBeans is a free and open source Java IDE that will help us to develop our little application. Create a new project at File → New Project and choose at Java at Categories and Java Application at Projects.

netbeans new project

Chose a name for your project. I called mine SerialTalker.

name your project

At the moment I wrote this tutorial I was using Netbeans version 6.5 and Java 6 update 10 but should work as well on newer and some older versions

Step 5) Adding Libraries and a Working Directory

On NetBeans the Projects tab, right-click your project and choose Properties.

libraries

On the Project Properties window select the Libraries on the Categories panel.

Netbeans project libraries

Click the Add JAR/Folder button.

arduino directory

Find where you placed your Arduino IDE installation. Inside this directory there’s a lib directory will some JAR files. Select all them and click Ok.

jars libraries

As we want to borrow the Arduino IDE configuration the program needs to know where is they configuration files.  There’s a simple way to do that.

Still in the Project Properties window select Run at Categories panel. At Working Directory click in the Browse button and select the directory of your Arduino IDE. Mine is at /home/silveira/arduino-0013.

Working directory

You can close now the Project Properties window. At this moment in autocomplete for these libraries are enable in your code.

netbeans autocomplete

Step 6) Codding and running

Here is the code you can replace at Main.java in your project:

package serialtalk;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import processing.app.Preferences;

public class Main {
    static InputStream input;
    static OutputStream output;

    public static void main(String[] args) throws Exception{
        Preferences.init();
        System.out.println("Using port: " + Preferences.get("serial.port"));
        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(
                Preferences.get("serial.port"));

        SerialPort port = (SerialPort)portId.open("serial talk", 4000);
        input = port.getInputStream();
        output = port.getOutputStream();
        port.setSerialPortParams(Preferences.getInteger("serial.debug_rate"),
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        while(true){
            while(input.available()>0) {
                System.out.print((char)(input.read()));
            }
        }
    }
}

Now just compile and run (with your Arduino attached in your serial port and running the program of step 2).

voillá

There is. Now you can make your Java programs to talk with your Arduino using a IDE like NetBeans to create rich interfaces.