loading
For my first instructable I will show you how to use Qt to make graphic interfaces for your arduino (or any rs232 device)

So if you want to create your own graphic interfaces, you want to control stuff just by clicking buttons from your screen, this instructable is for you.

To do this we will use Qt (say : "cute") from nokia.
Of course Qt is not the only program that can make graphical interface.
But these are the advantages of Qt :
- It is free and open source software (distributed under the terms of the GNU Lesser General Public License)
- It is C/C++, so widely used.
- You can compile your program on a lot of platform (Windows, Linux, ...)
- The possibilities are infinite (you probably have many applications coded on Qt : Skype, Google Earth, VLC, ...

I only provide basic things to start.
Here we just see how to install Qt, the serial port library and write your first program.
The program will control the brightness of a led.

I admit that you already know know C/C++. If it is not the case, there are plenty of  course on the web. If your are a french speaker (like me), I suggest the course from www.siteduzero.fr which is very good to start from zero.



Step 1: Intallation of Qt


The first step of this instructable is to get and install Qt.

You can get Qt sdk from this wedsite : http://qt.nokia.com/products/

Once downloaded, follow the instruction to install it.

The first part is over. Fairly easy, isn't it?
The next part is a bit more tricky.


Step 2: Installation of the Library


Even if Qt is very huge and provides a lot of tools, there is one thing which is missing in our case :
the serial port functions.

So we have to install a library to be able to communicate with the port com.

To do this I use the qextserialport library (version 1.2alpha).

First you need to download the library on sourceforge :
http://sourceforge.net/projects/qextserialport/files/
or the last version here
http://code.google.com/p/qextserialport/

Then you extract the files.

Here comes the tricky part. The files you downloaded are not compiled.
So you must compiled them. To do so you must start the Qt prompt console.
You can find it on Windows in :
start menu,
Qt SDK,
Desktop,
Qt 4.7.3 for Desktop (MinGW)

Then send the command that are on picture 2.

Make sure that a new folder "build" was created during the compilation and the files
qextserialportd.dll and libqextserialportd.a are present.

Congratulation, you perfectly compiled the library!

Step 3: Let's Program

Now everything is installed, we can start to program.

Open Qt Creator and create a new project.

I join an example of the program, there are a lot of comments which will help to understand.

The main things to know are :

- you have to include the library in your .pro file (see the example)

- to connect a port, you need to create a variable, to open the port and then to configure it (in this order).
(Don't forget to put the right number of your port)

- to send a message through, use the function write(char*).

- to receive data use the function read :
int numBytes =portcom.bytesAvailable();
char buff[256];
portcom.read(buff, numBytes);
(you can also use the signal readyRead() to launch function when a data arrives)


- if when compiling you have the error n°1073741515 (or maybe another) that mean that you need to put the qextserialportd.dll into your .exe folder.


You can find other examples in the library example folder. That will teach you how to scan the ports or receive data.

Step 4: Program Your Arduino

The last part is very easy.

Plug a led with its resistance to the 9 pin.

Then you just have to load the program present in Examples => Communication => Drimmer.

Here is the relevant part of the program :

const int ledPin = 9; // the pin that the LED is attached to

void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}

void loop() {
byte brightness;

// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
brightness = Serial.read();
// set the brightness of the LED:
analogWrite(ledPin, brightness);
}
}


Start your Qt program and control the pin 9 voltage.

Step 5: Conclusion

In this instructable, we have see how to use Qt to control your arduino.

I know that this example is very minimalist, but I'm sure that you will be able to start bigger project.

Next time I will show you how to plot graph on Qt, so you could also monitor temperature, pressure...

PS : As you probably notice it, I am not a native English speaker, I would be please if you can correct me.
<p>Can you please help me with my thesis project?</p><p>I am making a gui that will read my duino's eeprom memory and if I can modify the way I present it. </p>
<p>HI,</p><p>First of all thanks a lot for this very nice instructable.</p><p>I'm very new on QT and never handled this kind of software.</p><p>Can you please be so kind to give me more instruction on how I have to handle you file portsender.rar ( I have already decompressed it )..using qt Creator already installed?</p><p>Please forgive me..but I'm very new on this.</p><p>I hope to receive your kind reply.</p><p>My OS is Linux Mint and everything has been properly installed and working.</p><p>Thanks again.</p><p>Arnaldo Armida</p>
<p>There is a .pro file in the archive. You open this usint Qt, and build it. Should not be more than that. Providing of course, that you have installed the serial library. openSuSE actually have this as installable so you don't have to compile the serial library your self. It is also part of Qt5</p>
<p>I just modified it to (which makes it more &quot;adjustable&quot;):</p><p>win32{<br> INCLUDEPATH +=<br> CONFIG(debug, debug|release) {<br> LIBS +=<br> } else {<br> LIBS +=<br> }<br>}<br><br>unix:!mac:!linux*{<br> INCLUDEPATH +=<br> LIBS +=<br>} else:mac {<br> INCLUDEPATH +=<br> LIBS +=<br>} else {<br> INCLUDEPATH += /usr/include/qextserialport<br> LIBS += /usr/lib64/libqextserialport.so<br>}</p>
<p>&quot;we have seen ho..&quot; not, &quot;we have see ho..&quot;</p><p>I don't find your english bad :-)</p><p>Arnaldo, have you received any assistance? What exactly is the challenge?</p>
<p>Great Instructable.</p>
<p>Does it work exactly the same on macs? Or do I have to write other code to make an app for OS X?</p>
<p>Are there any Qt IDEs (commercial or open source) that have a m$ visual studio feel to it?</p><p>I was thinking of doing a simple GUI for an embedded application using Tcl/Tk but seems like there's not many resources available (ActiveState dropped Komodo GUI builder in 2007!)</p><p>Also any thoughts on GTK+, OpenBox, openFramework, wxWidgets, or any of the other popular GUI libraries http://en.wikipedia.org/wiki/List_of_platform-independent_GUI_libraries</p>
OK, I just sad that I can't use your tutorial, it's so well explained.. <br>I saw at code.google.com/p/qextserialport that there is support for Qt5 so I thought I could follow your tutorial.. <br>Do you still use Qt 4.7.3? Is there any chance you got the qt SDK installer?
Hello. I tried to follow the steps but it didn't work on qt5.1. Do you know how do I do that? I even tried to download and install qt 4.7.3, but with no success.. Hope you can help me. So what I could manage to have on my PC is Qt 5.1.0 with Mingw. If you can get in touch with me by email I'll be so grateful too.. My email is jloibman@gmail.com
The library does not seem to work on qt5.X. Instead you might use this new Qt add-on : http://qt-project.org/wiki/QtSerialPort , I have not tried but I think it has the same features that the qtserialport library.
Hi, <br> <br>I've been working on your examples theses days. Unfortunately, every time last build step, Qt showed up: <br> enumwidget.obj:-1: error: LNK2019: unresolved external symbol &quot;public: __thiscall QextSerialPort::QextSerialPort(class QString const &amp;,enum QextSerialBase::QueryMode)&quot; ( ??0QextSerialPort@@QAE@ABVQString@@W4QueryMode@QextSerialBase@@@Z) referenced in function &quot;public: void __thiscall Widget::init_port(void)&quot; (?init_port@Widget@@QAEXXZ). <br> <br>Any suggestions? <br> <br>
Hey mate, <br> <br>I went to my Qt SDK desktop. I had 4.7.4 &amp; 4.8.1. <br>I followed both your instruction for both 4.7.4 &amp; 4.8.1 just like you've showed them in the picture. <br>And it seemed to work exactly how its displayed on your cmd window. <br> <br>Also instead I downloaded the beta file. <br>QextSerialPort_1_2_Beta2. But I believe the procedure is very similar. <br> <br>Then the next, I did was open the project file from your portcomsender. After this I went inside the portcomsender.pro and changed the <br> <br>INCLUDEPATH += C:/Qt/qextserialport-1.2win-alpha/ <br>LIBS += C:\Qt\qextserialport-1.2win-alpha\build\libqextserialportd.a <br> <br>to <br> <br>INCLUDEPATH += C:/qt/qextserialport-1.2beta/ <br>LIBS += C:\Qt\qextserialport-1.2beta\buildlib\buildlib.pro <br> <br>I did this because I could not libqextserialportd.a or any .dll files. <br>Im really not sure what to do here. Should I just copy the ibqextserialportd.a &amp; .dll files from the aplha to the buildlib of the beta folder? <br> <br> <br> <br> <br> <br> <br>
Hi varuninnz<br>I am not sure that you should export the alpha files to the beta folder, this might be a bad idea.<br>I have not tried the beta yet so I don't know what you have to do. You should probably check what the developers expect you to do to run this new library. Usually you can easily find instructions in the downloaded files.<br><br>Tell me when you find a solution to your problem.<br><br>Cheers
When i typed in this &quot;F:\QtSDK\qextserialport-1.2win-alpha&gt;f:\QtSDK\mingw\bin\mingw32-make.exe &quot; <br /> <br />following error comes, <br />sbs -c winscw_udeb.mwccinc <br />sbs: error: tool 'MWLD' from config 'none' did not return version 'Version 3.2.* <br />' as required. <br />Command 'mwldsym2.exe -version' returned: <br />/bin/bash: mwldsym2.exe: command not found <br /> <br />Check your environment and configuration. <br /> <br />What went wrong? <br /> <br />
Here is the latest qextserialport project. It is no longer at sourceforge.<br>http://code.google.com/p/qextserialport/ <br>
Thanks,<br>I change the link.
Hi,<br>There is a mistake in the code:<br><br>char *buf; //creation of a buffer<br>*buf = value;<br><br>buf point to random memory and changing the value may randomly crash.<br><br>char buf = (char)value;<br>port-&gt;write(&amp;buf, 1); //send the buffer<br><br>or better<br><br>port-&gt;write(QByteArray(1, (char)value)); //send the buffer
It is correct,<br>Another solution is to allocate the pointer with a &quot;new&quot; :<br><br>char *buf;<br>buf = new char;<br><br>and then delete the pointer to avoid a memory overflow :<br><br>delete buf;<br><br>This is a mistake I often do because these problems are sometime solved during the compilation.<br><br>Thank you for your comment and fill free to correct me again.<br>
Some helpful hints...<br> <br> 1. In the QtCreator I had to change the 'build configuration' under &quot;Projects&quot; (on the left side) from Debug to Release<br> <br> 2. The two lines char *buf; *buf = value; caused a crash of my programm. The compiler did its job, but when I ran the prog und called the method &quot;transmitCmd(int value)&quot;, it crashed. I thing the conversion from int to char isn't possible this way... I did it this way: char buf; buf=(int)value; port-&gt;write(&amp;buf);<br> <br> 3. All included DLL-files have to be copied in the C:/WIndows folder

About This Instructable

231,967views

90favorites

License:

More by intructable:Thumbprint wedding tree. arduino module for time-lapse photography Make graphs on Qt and plot your arduino measurements 
Add instructable to: