Fab Academy 2017
Learn how to make ALMOST anything!

Interface and Application Programming

For this week, it was required to write an application that interfaces with an input &/or output device that you made and compare as many tool as we can.

I tried for this 2 Softwares:


2. Processing.



Since I was using MATLAB Simulink with Arduino before in my masters, I wanted to try what's like to do a GUI and interface it with Microcontrollers. The Interface I chose to do was pretty simple; To Turn a LED ON and OFF.

First of all before you start using MATLAB with Microcontrollers, you have to download MATLAB Arduino Support Package

I started to look for some tutorials to learn about GUI in MATLAB, especially I had very small knowledge about coding with MATLAB. There was some useful ones. Here are some.


Tutorial 1: Matlab and Arduino with GUI Interface. Simple LED Controller.


Matlab & Simulink: GUI Development for Arduino


Now it's time to develop it!

Opening Matlab GUI guide, I started developing the interface.


After Adding the buttons, I had to Edit them.


That's how the GUI looked like.


** Now, Save the GUI. But Note that you need to adjust your path!

Saving the GUI, MATLAB generates you the pre-set m file for you to edit.


Editing the code:

One of the things I love about MATLAB is its Program Help. It's awesome, details and very helpful when you want to search about functions, syntax, anything related to using MATLAB. So that was pretty helpful in getting the syntax required.

1. Open connection between MATLAB and Arduino. We do that in the "Opening Function".


2. Edit the Function of "ON" button (pushbutton1), we write the following: .

                        global a;
                        writeDigitalPin(a, 'D13', 1);

This defines a global variable a, on pin 13 and "1" is the value you write to that pin


3. Edit the Function of "OFF" button (pushbutton2), we write the following:.

                        global a;
                        writeDigitalPin(a, 'D13', 0);

This defines a global variable a, on pin 13 and "0" is the value you write to that pin


And Here it goes!

Now working on doing that with the Input circuit.


II. Processing:

Processing looks a lot like Arduino IDE with few differences. This is was my first with it, so I've done some research about it. A great tutorial I found was Connecting Arduino to Processing. It shpwed mainly how to connect Arduino with Processing.

That is usually done by serial commnucation. So the Networking assignment was actually pretty useful in understanding the base of this kind of communication! :D


Processing Environment (image taken from Sparkfun tutorial)


GUI development:

Looking at Processing tutorials and examples, there was "Button" Example in GUI list.

What this example shows is One Square and One Cirle with 2 different colors; and by clicking on one of the colored squares in the center of the image to change the color of the background.


Another base example, "Simple Write" Example in Serial list.

What this example shows is communicating serially with Arduino to illuminate a LED and turn it OFF later.


What I did was learning those two examples, understanding them and combine them to form the GUI I need.

The main idea is having the same square and cirle; clicking the square would turn the

Modifying the square and circle functions.


This is How the GUI looked Like.


Developing Arduino Code:

Arduino Code was easy to develop, Here it is.

                        char val; // Data received from the serial port
                         int ledPin = 13; // Set the pin to digital I/O 13
                         void setup() {
                         pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
                         Serial.begin(9600); // Start serial communication at 9600 bps
                         void loop() {
                         while (Serial.available()) { // If data is available to read,
                         val = Serial.read(); // read it and store it in val
                         if (val == 'H') { // If H was received
                         digitalWrite(ledPin, HIGH); // turn the LED on
                         if (val == 'L') { // If H was received
                         digitalWrite(ledPin, HIGH); // turn the LED on
                         delay(100); // Wait 100 milliseconds for next reading

And Here is the code in action.



1. LED Control GUI and Code filea.

2. Processing and Arduino Code files.