Arduino controlled by a GTK interface

For my little Arduino and relay project I started with the usage of the Serial monitor to switch the relay and the LED.

What’s about a nice GUI for doing this? Using Python to grab data from Arduino is easy. The only missing part is the GUI and some code to send information to the Arduino.

As the existing part was written in Python, I’m going on with GTK3. I just want a simple windows with a toggle button. That’s all. If you are not familiar with the successor of pygtk, reading this tutorial is a good starting point.

This blog post will be some kind of a tutorial/example for developing an user interface for Arduino platform. For simplicity only a LED can be controlled but the UI is easy expandable if you want. So the tools is called “Arduino LED commander” ;-).

The hardware setup is simple. You need an Arduino board, a resistor, and a LED. Connect the LED to the digital pin 13 and GRD. Check out the Blink tutorial for the layout.

On the software side the following tools are needed: Python (2.7.x is just fine), pySerial, Glade 3, and their dependencies.

The design of the UI in Glade is very simple. Drag-and-drop the component you want to the window. In Glade my UI looks like this:

Arduino LED commander Glade

If you want to skip this step, just download my Glade file.

The code can be divided in several parts. I will not go to much into details because everything is more or less standard stuff which was glued together. In the import section you see “serial”, this is the lib from pySerial.

I declared to constants for the serial connection. The port aka interface and the communication speed.

The largest part is needed for setting up the UI. The glade file is loaded, objects are created, and signals are connected.

The toggle button is the headliner. This button knows two states, pressed and released. Pressed is On, released is Off. When the button is clicked something needs to be sent to the Arduino. The self.send_command() is responsible for that.

The send_command() is invoked by the toggle button. Depending on the value of state, a O or a 1 is sent through the serial connection to the Arduino.

To lauch the Arduino LED commander, open a terminal and use a command like the one below depending on your naming schema.

If everything is ok, the UI shows up.

Arduino LED commander

Check-out the video to see the UI in action.


This entry was posted in Arduino. Bookmark the permalink.

One Response to Arduino controlled by a GTK interface

  1. ashkan says:

    hi, thanks for this good post . i am trying to do so but what i am facing it python is :

    Traceback (most recent call last):
    File “D:/Pyton/”, line 6, in
    from gi.repository import Gtk
    ImportError: No module named gi.repository

    I’ve searched for gi.repository but could not find it anywhere.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.