Thursday, 26 January 2017

Java Program on Arduino over Serial Communication

Pre-requisite -

  • Java installed ( any version , must be greater than 1.6 JDK )
  • Arduino IDE on Raspberry PI
  • USB connection from R Pi to Arduino

Steps -

  • Setup Lib RX TX on Linux
  • Setup ArduLink on Raspberry pi ( linux basically )
  • Symlink for ttyACM
  • Transfer Program to Arduino for Serial listening
  • Compile and Run Java Program 

Step - 1. Setup Lib RX TX on Linux

  • Run this command with root user - sudo apt-get install librxtx-java
           Check if this file is created at location - /usr/lib/jni/
  • Create Symlink in location -  /usr/lib/jvm/<your JDK name here>/jre/lib/arm/
                   location  eg. /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/lib/arm/

         go to the above location first and then create symlink there with this
         command -  ln -s /usr/lib/jni/

Step -2. SETUP ArduLink on R Pi

  • Download Ardulink from this page - ArduLink Site
  • unzip anywhere and note the location , we will use this location in classpath while running java program.
Step -3. SymLink for TTYACM
  • run this command - ln -s /dev/ttyACM0 /dev/ttyS80
Step -4. Transfer Program to Arduino for Serial listening
  • Go to location - ardulink-V2.0.1\ardulink\sketches\ArdulinkProtocol\
  • Using Arduino IDE , upload this program - ArdulinkProtocol.ino ( this is available in above location )
Step -5. Compile and Run Java Program 

import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.ardulink.core.Link;
import org.ardulink.core.Pin;
import org.ardulink.core.Pin.DigitalPin;
import org.ardulink.core.convenience.Links;
public class TestArduLink {
public static void main(String[] args) throws InterruptedException, IOException {
       Link link = Links.getDefault();
       DigitalPin pin = Pin.digitalPin(10);
       boolean power = true;
       while (true) {
          String data = getInput();
          if(data.equalsIgnoreCase("on")) {
 power = true;
          link.switchDigitalPin(pin, power);
          }else if(data.equalsIgnoreCase("off")){
          power = false;
                           link.switchDigitalPin(pin, power);
private static String getInput() {
Scanner scanner = new Scanner(;
String data =;
// scanner.close();
return data;
  • Copy above program and paste in file named - 
  • Compile using this command - 
                           sudo  javac  -cp /<ardulink location here>/lib/*:.

                     eg. sudo  javac  -cp /home/pi/Downloads/ardulink/lib/*:.
  • run using this command - 
                          java  -cp /<ardulink location here>/lib/*:. TestArduLink

                   eg.  java  -cp /home/pi/Downloads/ardulink/lib/*:. TestArduLink

Explanation - Program takes command line input , type on to to turn on the LED and off to turn it off.

Ground - GND 
Positive - Pin 10
Resistance - 220 ohm 

Please look on internet for basic LED connection on arduino .

1 comment:

  1. serial communication through GUI

    hope it will be helpful