I needed to be able to control the Arduino from within Java for my Java course Term Project. Great tutorials existed for using the Arduino in Processing and using Processing within Eclipse (Java), but I could not find anything about controlling the Arduino within Eclipse (I am basically putting the two tutorials together – it is not at all difficult).
ARDUINO WITHIN ECLIPSE
This tutorial is shown for a Mac (I am using Mac OS 10.5.7) and assumes you already have Java, Processing, and Arduino installed. I have copied and pasted most of the steps from the Arduino in Processing and the Processing within Eclipse (Java) and have included some of my own annotations. If you want to dig deeper or really understand why some things are done as they are, check out the tutorials => I am simply giving a step-by-step how-to without discussing the reasons behind things.
Step 1. Upload Firmata onto the Arduino board
Run Arduino, open the Examples > Library-Firmata > StandardFirmata sketch, and upload it to the Arduino board. Make sure you quit the Arduino program when you are done (having multiple programs open trying to use the USB serial port with the Arduino does not usually work).
Step 2. Download and install Eclipse
Grab “Eclipse IDE for Java Developers” from http://www.eclipse.org/downloads/. When you first run Eclipse, it will ask you for a workspace directory. This is the directory where you will store your project files so pick something reasonable that you will remember (I put it in my Documents folder).
Step 3. Create a new project
If you see a Welcome screen, go ahead and close it. Then go to FILE –> NEW PROJECT and select “Java Project.” Click next. Enter your project name (for example, “TestArduino”) and select “Finish.”
Step 4. Import libraries
You need to COPY (not just point to) the libraries necessary to run Processing and the Arduino within Eclipse. The Processing in Eclipse tutorial does not mention importing the serial.jar file discussed here because it was not concerned with the Arduino.
Right click on the “TestArduino” folder under the Package Explorer column on the left side of Eclipse, then click:
IMPORT –> GENERAL –> FILE SYSTEM
Click next. The idea on a Mac is to click browse and find the Processing application because it is in the applications “package contents.” This is best done by typing the following in the Browse bar then clicking on “browse . . . ” => /Applications/Processing.app/Contents/Resources Then, highlight the “Java” folder and click “Open.” Now click once on the Java folder to highlight it and put a check next the “core.jar” file on the right column:
Then navigate to the “serial.jar” file (/Applications/Processing.app/Contents/Resources/Java/libraries/serial/library) and put a check next to it.
Click “Finish.” You should now see “core.jar” and “serial.jar” listed under your project in the package explorer when you use the drop down arrows to navigate:
Right-click on the “serial.jar” file and select –>�BUILD PATH –> ADD TO BUILD PATH.
Right-click on the “core.jar” file and select –> BUILD PATH –> ADD TO BUILD PATH.
The “core.jar” and “serial.jar” files should now appear with a jar icon under “Referenced Libraries.” �(Note this right-click option will only show up in the “Package Explorer.” If you are in the “Project Explorer” or some other view, switch by going to WINDOW –> SHOW VIEW –> PACKAGE EXPLORER.):
Step 5: Add a class file
Right-click on the “src” folder below “TestArduino” and type “testarduino” as the package and “TestArduino” as the name (then click “Finish”):
Step 6: Add the Arduino.java file
Download the Arduino.java.txt file from the Arduino in Processing tutorial. Remove the “.txt” from the name (I drug the file to my desktop, renamed it Arduino.java and the OS asked me if I wanted to “Use .txt” or “Use .java” and I clicked on “Use.java”).
Then, right click on the Arduino.java file and click “Copy “Arduino.java”"
In Eclipse, Right click on the package “testarduino” and select “Paste.”
There are a few errors due to file names, so . . . (go to the next step).
Step 7: Rename references
Double click on the Arduino.java file and it will open into the middle column (editor window) of Eclipse.
Change the error line from “package cc.arduino;” to read the name of your package “package testarduino;” Then press command+s to save the file and the errors should be gone:
Step 8: Add some code
Double click on the “TestArduino.java” file under the “testarduino” package or click on the “TestArduino.java” tab in the center editor of Eclipse. Replace whatever is in the file with the following code and FILE–>SAVE (or command+s):
package testarduino; import processing.core.PApplet; import testarduino.Arduino; public class TestArduino extends PApplet { //this is the main of the java program (where the program begins) //this simply tells java to run the PApplet (the Processing applet) //so really Processing is just running inside java (make sure to choose run as Java Application) public static void main(String args[]) { //package.javafilename PApplet.main(new String[] { "testarduino.TestArduino" }); // if you put the following, it will put it in full screen: // PApplet.main(new String[] { "--present", "arduinotest.ArduinoTest" }); } Arduino arduino; //create an arduino object int pin13 = 13; //create a variable for a pin on the arduino public void setup() //setup the arduino and anything else for your program { //to set the baud rate rather than go off the default (default is = 115200): //arduino = new Arduino(this, Arduino.list()[0], 57600); //otherwise,this is how to set up using default: arduino = new Arduino(this, Arduino.list()[0]); //setup if the pins are input or output arduino.pinMode(pin13, Arduino.OUTPUT); } public void draw() //this "draw" function; just loops until you shut off the program { arduino.digitalWrite(pin13, Arduino.HIGH); delay(1000); arduino.digitalWrite(pin13, Arduino.LOW); delay(1000); } } //end of TestArduino.java class
Step 9: Hook up the Arduino
The Arduino should already have the Standard Firmata sketch uploaded onto it’s chip (Step 1 above).
Put an LED in pin 13 of the Arduino (put the long leg of the LED in pin 13 and the short leg in the GND right next door to pin 13).
Plug the Arduino in the USB of the computer.
Step 10: Run the program.
From the top of the screen click RUN–> RUN
You will be asked if you want to run it as a “Java Applet” or a “Java Application” . . . make sure you choose “Java Application.”
A small applet should show up on the screen like this:
. . . and the light on your Arduino should blink on and off every second.
Close the small applet window to stop the program – if you close the window when the LED is on, that action will be frozen in the Arduino and the LED will remain on as long as the Arduino is plugged into the computer or some new message is sent to it.








