In my EECS 1021 class, in which we connect Java programs on a Mac or Windows machine with programs running on an Arduino-compatible board, we generally use Firmata. When combined with a library like Firmata4j (Java) or pyFirmata (Python), or Arduino-Octave (Matlab), Firmata provides an easy-to-use pathway for this to happen. It is, however, a bit limited in scope.
For many Arduino projects, you'll discover that Firmata (or the computer-specific library you're using like Firmata4j) doesn't include a driver for the sensor, display or actuator that you want to use. You're faced with a choice:
- Extend the computer-specific Firmata library (e.g. Firmata4j). For instance, you extend the i2c code used for the OLED controllers for other i2c devices.
- Modify the Firmata examples found in the Arduino IDE (File->Example->Firmata)
- Write your own Arduino program and use serial libraries like JSSC or jSerialComm
Here, we'll look at the third option: serial libraries in Java and a custom Arduino program. The big advantage with this approach is that you can leverage existing Arduino libraries for the device that you wish to use. Libraries like JSSC or jSerialComm for Java will provide you with plenty of flexibility on your Windows or Mac computer. JSSC is used by by the Firmata4j Java library. But we'll focus on jSerialComm because that is the library that we originally used in 2021 in EECS 1021 for linking Java and Arduino boards like the Grove Beginner Kit for Arduino from Seeed Studio.
To get started, you need to install a driver for the OLED on the Grove board: https://youtu.be/3-MUuKUelE0. Once the OLED driver is installed you'll be able to compile this Arduino program, Serial_SendA0.ino:
- Arduino program for sending a serial stream up the USB cable to your computer, Serial_SendA0.ino.
There are three other files that you need in Java:
- MainClass.java (the main class and main method)
- DataController.java (the class that deals with data received from the Arduino)
- SerialPortService.java (the class that listens to the serial port and engages DataController for processing)
You need to import two Maven libraries for the java files to compile:
- org.apache.commons:commons-lang3:3.3.2 (for StringUtils chop)
- com.fazecast:jSerialComm:2.9.1 for jSerialComm
James Andrew Smith is a Professional Engineer and Associate Professor in the Electrical Engineering and Computer Science Department of York University's Lassonde School, with degrees in Electrical and Mechanical Engineering from the University of Alberta and McGill University. Previously a program director in biomedical engineering, his research background spans robotics, locomotion, human birth and engineering education. While on sabbatical in 2018-19 with his wife and kids he lived in Strasbourg, France and he taught at the INSA Strasbourg and Hochschule Karlsruhe and wrote about his personal and professional perspectives. James is a proponent of using social media to advocate for justice, equity, diversity and inclusion as well as evidence-based applications of research in the public sphere. You can find him on Twitter. Originally from Québec City, he now lives in Toronto, Canada.