Lab10: Arduino and Java communication
Demo code is HERE.
Install Arduino
- Download and install the latest version of the Arduino Software from
Arduino
website
If you are a mac user, after installation, drag and drop the icon to your Application folder. - Connect the Arduino board with a USB cable to your computer.
- Double click the Arduino software icon to lunch the program. Make sure Arduino board is connected.
- Select Chip: You'll need to select the entry in the Tools > Board menu that corresponds to your Arduino (e.g. Diecimila). This information is written on the board.
-
Select Serial Port: From IDE menus, go to Tools >Serial Port > and
select the serial port. On a PC it will probably be something like
COM3 or COM4. On a Mac it will be something like
tty.usbserial-xxxxx.
If you do not see these options, you need to install the drivers first. The latest version of the drivers can be found on the FTDI website.
Open the Blink example
- Open the LED blink example sketch: File > Examples > 1.Basics > Blink.
-
Upload the program: Now, simply click the "Upload" button in the
environment. Wait a few seconds - you should see the RX and TX leds
on the board flashing. If the upload is successful, the message
"Done uploading." will appear in the status bar.
A few seconds after the upload finishes, you should see the pin 13 (L) LED on the board start to blink (in orange). If it does, congratulations! You've gotten Arduino up-and-running.
Java Serial Communication Library (RXTX Library) Set Up
- If you installed the Arduino IDE, you already have the RXTX library installed for the next steps, and you may be able to skip this section and start with the sample Java code. If it did not work come back to this section!
- You might need to state to Java where the RXTX
libraries are located. First from RXTX
Library Set Up , and find
what libraries you need to use. It is different in Mac and PC.
To find the libraries in your machine, go to Arduino folder and look inside its content. For example in Mac: Use Finder > Find Search Arduino. Right click on Arduino icon > Open Enclosing Folder. If it brings you to Application folder, right click > Show Package Contents>Content. Find "RXTXcomm.jar" and "librxtxSerial.jnilib" there.
There are two ways to add the libraries to your project. Choose the one that works for you.Add libraries to your Java Application
This link is helpful.- Copy the JARs you'll be using to your project.
- Create a new folder named lib in your project folder
- Copy the JARs you need to lib.
- Refresh your project by right clicking the project name and selecting Refresh. The lib folder will now be visible in Eclipse with the JARs inside.
- Right click the project name and navigate to Build Path.
- Select Configure Build Path... and the project properties window will appear showing your build path configurations.
- Select the Libraries tab.
- Click Add JARs.
- Locate and select the JARs you want and click OK. The JARs will now appear in the list of libraries in the build path.
- Click OK to close the properties window. The JARs will now be in Referenced Libraries instead of lib.
Set Java path
- For Mac users: You might need to set the -Djava.library.path to state where the JNI libraries are located. For that purpose, you can serach about path in this website.
- For PC users: Create a lib folder somewhere and put the files there. Go to system properties>Advanced>Enviroment Varialbes >System Variables > path Add the address of the folder at the end of the path variable with ; at the end.
- If your sample Java code still does not work, you need to manually set up the RXTX library from this website or the following steps:
- Download and install the RXTX library from RXTX wikipage. The libraries and installation is different for Mac and PC.
- Example installation on Intel Mac OS X: (be aware if your system is 32 or 64 bits) librxtxSerial-2.jnilib and RXTXcomm.jar both go in to: /Library/Java/Extensions
- Example installation on PC:
Depending on your machine (it could be 32 or 64 bits), download the
following files and put it in one folder.
librxtxSerial-2.jnilib and RXTXcomm.jar
Then go to system properties>Advanced>Enviroment Varialbes >System Variables > path Add the address of the folder at the end of the path variable with ; at the end.
Save it!
Sample Java and Arduino code for Serial Communication
This code reads analog information from A0 and puts the information into serial port. Java program then will read the information from serial port and sketch a graph out of them.Ardunio code
- Upload the Ardunio code into your Ardunio board.
- Connect a slider sensor to your Arduino board. The black wire goes to Ground, The middle wire goes to 5 V and the last wire goes to A0 in Analog side of Arduino.
Java code
Import the Project to Eclipse. Try to compile and run the Java project. If you get errors try the following hints. If you still get errors, go to previous section and set up the RXTX library.-
Hint 1: For error: "Access restriction on class due to restriction on required
library"
Go to Eclipse> Window->Preferences->Java->Compiler->Errors/Warnings, find and open the "Deprecated and Restricted API" bullet, and set Forbidden and Discouraged Reference to something other than Error. -
Hint 2:
If downloaded RXTX libraries from websites, be aware that Arduino
serial library interferes with RXTX library.
If you may get this error: "Serial Port ...Already in Use.",
you need to set up a lock folder. Go to terminal and type:$ sudo mkdir /var/lock $ sudo chmod 777 /var/lock
If you still could not run the program come to the open lab Wednesday Nov 14 together with your assignment partnet. I will help you set up the libraries in at least one of your machines.
If still could not get things work, use one Processing class inside your Eclipse project. A sample code is HERE.
Processing website has useful instructuions about Processing in Eclipse.
Here is some of my old notes for IAT 267.