Introduction: Control Arduino With a Portable App

About: I build projects using arduino nano / esp32 / arduino micro and also arduino mkr1010

We are going to learn how to make an application to control arduino.

We will see how to:

  • Easily make a windows application with python.
  • Convert it as an executable to use it anywhere.
  • Manage Serial ports (detection/reconnection) automatically with 1 line of code

We can even reuse the code to manage as many arduino as we want.

But before i tell you more about it, let's try utest first!
We will use it to control the internal led on an Arduino nano.

As always, all the documentation/code are available on github in english/french
http://github.com/pigetArduino/utest

Step 1: Upload Arduino Code

Frist we need to upload the code on the arduino.

On the Arduino software ( http://arduino.madnerd.org/ )

  • Copy utest folder into your sketch folder
  • Upload utest.ino

(Tools: Arduino Nano / Processor : Atmega328)

You will need an Arduino nano clone (ch340g) as the application will only detected it.

You can use the serial monitor, to test your arduino:
No Line Ending / 115200

UTest : return OK
ON : Turn on internal led (pin13)
OFF: Turn off internal led (pin13)

Step 2: Control the Arduino With Utest

utest is a portable application, you don't need to install anything to make it works.

utest will automatically find the arduino
You can try to unplugged it/plug it on another usb port, and it will reconnect.

utest might not work on Windows 7 due to missing .dll
This shouldn't happen if your computer is up to date
Source: http://stackoverflow.com/questions/34218333/pyins...

Step 3: Create Your Own Application

Let's see how to reuse this application, to make your own application.

First we need to install python 3 to modify it.

Then we need to install pySerial, to communicate with our arduino.

  • Open a command prompt (Windows key + cmd)
  • Type:
pip install serial

Finally, test the application, it is available in the source code ( http://utest.madnerd.org ) at apps/utest/

  • Open a command prompt
  • Go to the source code folder (apps/utest/)
  • Type:
python utest.py

Step 4: Create the Interface

We have everything we need to modify our application.
Check out the source code : utest.py (github)

Let's see

  • how to manage our arduino
  • how to build a graphical interface with tkinter

In order to manage the arduino the easiest way possible, everything is handle by the module lib/usb.py

USB

As for now, this module has only two commands

usb = USB.Device(...)
  • Connect to every serial ports which as CH340 in his name
  • Send UTest to the serial port
  • If it received "OK", it will connected to it
from lib import USB

device_name = "CH340" #Device name in Windows
device_type = "UTest" #Device type (in the arduino sketch)
device_return_string = "OK" #Answer when a response is correct
device_baudrate = 115200 #Baudrate
usb = USB.Device(device_name,device_type,device_return_string,device_baudrate,status)

Everything is inside a separate thread to avoid blocking the application.

usb.write(string)
  • Send a string to the arduino
  • If the serial port is not available, it will try to reconnect

GUI (TKinter)

utest use tkinter to manage the GUI (graphical interface)
You can find more information on tkinter here : http://www.tkdocs.com/tutorial/index.html

  • To create a window:
from tkinter import *
root = Tk()
  • To create a button ON
Button(text="on",command=on).pack()
  • Create an action for the button ON
def on():
	print("on")
  • Create a label
status = Label(text="Searching...")
status.pack() 

If you want to modify a widget , we need to save it into a variable, and use .pack() on a different line
We send the label to the USB Moduleto display the current state of the connection

usb = USB.Device(...,status)

Finally, we generate the GUI loop.

root.mainloop()

Step 5: Add Commands to the Arduino

Our interface is ready,
but we need to teach our arduino to understand the commands we will send.
Check out the source code : utest.ino (Github)

Serial Functions

We use two functions to manage the serial communication.

    serialManager()

    Check the serial port and convert any messages to a string (inside readString)

      serialCheck()

      If UTest is received , answer OK

      Device name

      You can change the name of the usb device in the first line

      const String usb_name = "UTest";

      Add commands

      We manage our commands inside void loop()

      void loop() {
        serialManager();
        //If string received
           if (readString.length() > 0) {
            serialCheck();
           
            if (readString == "ON"){
              digitalWrite(13,1);
            }
            if (readString == "OFF"){
              digitalWrite(13,0);
            }
            
          }
        //We clean the serial buffer
        readString = "";
      }

      For each commands create a condition, for example to turn on the internal led when "ON" is sent

      if (readString == "ON"){
         digitalWrite(13,1);
      }

      Step 6: Convert Your App Into a Portable Executable

      The arduino / Application is ready to be bundle into an .exe

      • Install pyinstaller, we will use it to convert our application into an single executable file

      pip install pyinstaller

      • You can compile your application using the script compile.bat

      pyinstaller --onefile --noconsole utest.py 

      • If you want to display debug messages, use this command instead:

      pyinstaller --onefile utest.py
      

      You should now have a /dist/utest.exe file

      You will probably have warning about api-ms...dll file.
      This shouldn't be a problem as these DLL (Universal C Runtime) are preinstalled on Windows 10 and previous windows should also have them if they are up to date.

      Step 7: ... to Be Continued

      I hope this was useful, and that will inspired you to create Arduino application !
      With some modification, this application should works on MacOS / Linux.

      Follow me on instructables/youtube, if you are interested by this topic.
      Subscribe

      Next time, we will learn how to improve our application and use it to control a led strip!