How to Write an Ardulink GUI, to Control Arduino

46K5714

Intro: How to Write an Ardulink GUI, to Control Arduino

In this tutorial you can read how to write, just in 10 steps, an Ardulink GUI like the one shown here. You need just five minutes to follow a video tutorial and just five minutes to read this tutorial.

Ardulink is a java open source library and/or a ready swing application able to control Arduino. Ardulink is a project of mine.

You can find more details about Ardulink here: www.ardulink.org

STEP 1: Is You a Developer or an User?

Ardulink has a ready application called Ardulink Console with all swing components. So if your aren't interested in develop your own interface then you can use Ardulink Console as is.

STEP 2: Ok, You Are a Developer

You can see the full five minutes video tutorial on youtube by following this link: http://youtu.be/2bwge-MrxLg
Now you can download the eclipse (kepler) workspace with the tutorial code following this link (the original tutorial):
http://www.ardulink.org/how-to-write-an-ardulink-gui-five-minutes-tutorial/

I used two Ardulink components and a simple SWING button.
  • org.zu.ardulink.gui.ConnectionPanel is able to search for serial ports connected to the Arduino and select one.
  • org.zu.ardulink.gui.PWMController is able to manage the power with modulation Arduino's pins.
  • The button handles the event for the connection.
To have a GUI like the one in this tutorial you only need to:
  • Use a JFrame (steps: 1,2 and 3)
  • Add inside a org.zu.ardulink.gui.ConnectionPanel (step 4)
  • Add a JButton (step 5)
  • Add inside a org.zu.ardulink.gui.PWMController (step 6)
  • Use a JButton to invoke the org.zu.ardulink.Link class (steps: 7, 8, 9 and 10)
The most important instruction of this tutorial is in fact:
boolean connected = Link.getDefaultInstance().connect(connectionPort, baudRate);

Using the default connection method Link class initializes a dialogue with Arduino board connected to a specific serial port with a given baud rate.
Here the source code:

package org.zu.ardulink.tutorial;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import org.zu.ardulink.Link;
import org.zu.ardulink.gui.ConnectionPanel;
import org.zu.ardulink.gui.PWMController;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;

public class Tutorial1 extends JFrame {

   private static final long serialVersionUID = -5884548646729927244L;

   // 2. Define the contentPane and an ardulink connection panel
   private JPanel contentPane;
   private ConnectionPanel connectionPanel;

   /**
    * Launch the application.
    */
   public static void main(String[] args) {
     EventQueue.invokeLater(new Runnable() {
       public void run() {
         try {
           // 1. Change Look and Feel
           UIManager.setLookAndFeel(NimbusLookAndFeel.class.getCanonicalName());

           Tutorial1 frame = new Tutorial1();
           frame.setVisible(true);
         } catch (Exception e) {
           e.printStackTrace();
         }
       }
     });
   }

   /**
    * Create the frame.
    */
   public Tutorial1() {
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setBounds(100, 100, 475, 375);
     // 3. Setup the contentPane
     contentPane = new JPanel();
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
     setContentPane(contentPane);
     contentPane.setLayout(new BorderLayout(0, 0));

     // 4. Insert the defined connection Panel
     connectionPanel = new ConnectionPanel();
     connectionPanel.setLayout(null);
     contentPane.add(connectionPanel, BorderLayout.WEST);

     // 5. Insert a connection button
     JButton btnConnect = new JButton("Connect");
     // 7. Add an action listener when the connection button is pressed
     btnConnect.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         // 8. Take connection parameters
         String connectionPort = connectionPanel.getConnectionPort();
         int baudRate = Integer.parseInt(connectionPanel.getBaudRate());

         // 9. Let's go. Link it to Arduino.
         boolean connected = Link.getDefaultInstance().connect(connectionPort, baudRate);

         // 10. Just an information message
         if(connected) {
           JOptionPane.showMessageDialog(connectionPanel, "Arduino connected", "Connection Status", JOptionPane.INFORMATION_MESSAGE);
         } else {
           JOptionPane.showMessageDialog(connectionPanel, "Arduino NOT connected", "Connection Status", JOptionPane.ERROR_MESSAGE);
         }

       }
     });
     contentPane.add(btnConnect, BorderLayout.SOUTH);

     // 6. Insert an ardulink power with modulation pin controller
     PWMController powerPinPanel = new PWMController();
     powerPinPanel.setPin(11);
     contentPane.add(powerPinPanel, BorderLayout.EAST);

   }

}

14 Comments

first of all you didn't mentioned how to import the libraries and resource file and directly jumped to code. i am having a trouble with the resource file where i can find them. not a good tutorial

Maybe. This is a tutorial I wrote some years ago. I think that I should write other tutorials since Ardulink users need for more information about it. Actually Ardulink is a maven project and its artifacts are available on maven central (

https://search.maven.org/#search%7Cga%7C1%7CArduli... so you should be able to understand which artifacts are needed.

A good point to start to study how write your own project is Ardulink Console you can find the code here:

https://github.com/Ardulink/Ardulink-2/tree/master...

Yes, of course, you can with Ardulink. Here a GUI example with a textbox:

http://www.ardulink.org/automatic-lipid-dispensing...

It uses this GUI widget:

http://arduinopclink.altervista.org/javadoc/arduli...

but there is also this one:

http://arduinopclink.altervista.org/javadoc/arduli...

These components have a textfield and a button. When you press the button a custom message with text value from the textfield is sent to Arduino.

I have used it in the SimpleSmartCarDriver example that you can find in the Ardulink distribution.

hi.am not a programmer but using your instructions i have successfully made a single tabbed real time chart ,plotting single analog value,but i want add another tab in to give a digital input and analog input to the arduino. please guide me .thanks for the help

Hello, is it possible to build a GUI to control 3 servo motors independently?

Hi I would like to ask, is it possible to make GUI for arduino with input textbox? I often see GUI with slider only.. My project is all about monitoring the weight of a ship as load is being loaded, it also comes with sensor, and height of the ship is one of the varibales in the formula. so I need to create a GUI in order to tell an arduino what is the height of the ship, and at the same time outputs also in the PC the weight of the ship, in continous time. Thanks..

Can anyone please advise? I receive the following error when I am running the code.

java.lang.UnsatisfiedLinkError: Can't load IA 32-bit .dll on a AMD 64-bit platform thrown while loading gnu.io.RXTXCommDriver

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Serial.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform

Hi,
Ardulink 1 uses mainly RXTX library to connect PC with Arduino. This library isn't a java pure library and needs for a shared object (dll for windows users) installed on the PC. Of course this shared object has to be compatible with the system architecture. So you have to replace the DLL 32 bit with the 64 bit one. You can find both library version into Ardulink distribution. You can put the right one into windows folder or into the working directory of your application.

on arduino wich .ino file i have to upload

I used to program in java and had my own application for microcontrollers ( PIC was the go to one before the arduino ). What you have done here is so much more ambitious, I am very impressed. I could never really get the Java GUI components under control. Now I have moved to Python and would recommend it, not necessarily as a replacement for Java but as a different type of language with its own strengths and differences, unlike C++ and Java, it is really different from Java. There are also environments that make them play together nicely ( in theory, I have not tried it ).

You have also figured out how to put code really nicely in an instructable, why not write a short one on that.

Finally there is no justice on instructables, this is among say the best 10% of all technical instructables published, yet its views languish. In some cases this is because it is a good instructable, TLDR. Please keep publishing, maybe dumb it down a little. If you take a look at my instructables you will see, I think, that views are not well correlated with intrinsic quality or effort. I publish frequently even when I know the views will be low.

Thank you, I'm very happy when someone likes my work. I agree about languages. Each language has its strengths and its weaknesses. They are just tools. I mean, it is very difficult to cut down a tree with a scalpel as it is very difficult to operate with a saw. So we should ever use the right tool for a given work.

Actually I don't know Python but I know it's a powerful language.

I'll try to add some instructable when I'll have the time for. Maybe that one of them will be famous!

I'll try to improve Ardulink. Java libraries can be extended a lot with a lot of Technologies. Maybe that Ardulink will have also a web interface in the future.

really neet!!, i´ll give it a try...
Thank you. Let me know if you'll find some issues.