A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interface and Command Arduino in Few Hours. - Magesh Jayakumar

June 27, 2018 | Author: Ahmad Reza Atef | Category: Arduino, Matlab, Graphical User Interfaces, Function (Mathematics), Rgb Color Model
Share Embed Donate


Short Description

start guide to MATLAB GUI...

Description

Contents Arduino (From Arduino Website) MATLAB Getting started with Arduino and MATLAB Checking communication between Arduino and MATLAB: Creating a GUI function to Control an LED RGB LED Color Mixing Control a Servo Motor DC Motor Control Plotting Graph using Temperature data Contact and app inventor files.



Arduino (From Arduino Website) Arduino is an open-source prototyping platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board. To do so you use the Arduino programming language (based onWiring), and the Arduino Software (IDE),based on Processing. Over the years Arduino has been the brain of thousands of projects, from everyday objects to complex scientific instruments. A worldwide community of makers - students, hobbyists, artists, programmers, and professionals - has gathered around this open-source platform, their contributions have added up to an incredible amount of accessible knowledge that can be of great help to novices and experts alike. Arduino was born at the IvreaInteraction Design Institute as an easy tool for fast prototyping, aimed at students without a background in electronics and programming. As soon as it reached a wider community, the Arduino board started changing to adapt to new needs and challenges, differentiating its offer from simple 8-bit boards to products for IoT applications, wearable, 3D printing, and embedded environments. All Arduino boards are completely open-source, empowering users to build them independently and eventually adapt them to their particular needs. The software, too, is open-source, and it is growing through the contributions of users worldwide.

MATLAB MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include: Math and computation Algorithm development Modeling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including Graphical User Interface building MATLAB supports low cost hardware system, one of the system is Arduino where you can connect an Arduino with MATLAB and start to work with on it, one of the advantage of Arduino is, and it is a low cost hardware which can be used with MATLAB to acquire data and process data. Throughout this book you will know how to create a GUI in MATLAB to control Arduino.

Getting started with Arduino and MATLAB To use Arduino with MATLAB, we need a support package that needs to be installed on the Arduino side to make communication with Arduino board. This is a one-time installation and you also need MATLAB registered email id and password to complete the installation process. If you have not registered yet, register in order to download the support software. You need to have your matlab installed on your system in order to continue the below steps, make sure you have installed MATLAB. 1) Setup MATLAB and Simulink support package For Arduino. Start your MATLAB, you can do this by double clicking MATLAB short cut on your desktop or search for MATLAB on your Programs. 2) Open MATLAB and click the Add-Ons drop down menu on the top right of the MATLAB window and select Get Hardware Support Packages in the drop down menu to start the installation process.

3) Select install from internet as the source for installing the support package

4) Select Arduino from a list of support packages

Click next and log into your Mathworks account. If you don’t have a MathWorks account, you can create one during the installation process.

Continue and Complete the Installation Accept the license agreement on the next screen and click Next through the following screens to finish the installation for both MATLAB and Simulink Support Package for Arduino.



If everything goes well you will see the screenshots that included above in this page, if you are not seeing the screen like above check your internet connectivity and still the problem persist you can disable your firewall and your antivirus till the installation

process get complete, sometime firewall may block the MATLAB to connecting to the server for downloading the required files.

You should see the above progress bar to get confirmation that the installation process is going smooth and downloaded has completed and installation is taking place. It will take some time to complete the installation process and it is completely depend on your Internet speed, It took me 1 hour to complete the whole process, so be patience during the installation process don’t click cancel while downloading, sit and relax still the process gets over. Once the process completes restart your MATLAB and get ready for your first program. Check the video tutorial in the link on how to setup the required files to make it work

Checking communication between Arduino and MATLAB: In order to check communication between Arduino and MATLAB you need to connect your Arduino device to MATLAB, you can use any of the Arduino device, for this example I am using the popular device from Arduino family, Arduino Uno. If you own an original Arduino you can type a = arduino in the matlab command window.

If you installed the software for Arduino in the MATLAB and typed the above command you will be returned with the arduino properties of your Arduino hardware. If you are using Arduino clone, sometime MATLAB finds difficult to find the properties of your Arduino hardware, if it’s not returned with any properties type the following command,

a = arduino(‘COM3’,‘Uno’) If you are connecting your Arduino on a Mac Connect to an Arduino Uno on port \ dev\tty.usbmodem1421.

a = arduino(‘/dev/tty.usbmodem1421’,‘Uno’) If all the above steps failed make sure you are not using your Arduino with any other IDE or reconnect your Arduino hardware and also upload blink example sketch from Arduino IDE examples and make sure it is connecting to Arduino IDE before coming to a conclusion that it is not working with MATLAB.

Make sure to delete the variable in the workspace, this may sometime prevent us to not use arduino hardware with other programs.

Creating a GUI function to Control an LED In this lesson you will learn how to create GUI (Graphical User Interface) in MATLAB to control an LED connected to Arduino Uno. Things you needed to make this tutorial. 1. Arduino Uno or any other type of Arduino hardware 2. An LED 3. A 220 ohm resistor. If you want to try this tutorial with the inbuilt LED connected to pin number 13 you don’t need LED or resistor to test this tutorial.

Connect LED to the 3rd pin of Arduino in between LED and Arduino Pin connect a 220 ohm resistor as shown in above, connect the other end of LED to Ground pin of Arduino. On the Top left side of MATLAB Home tab click on the + sign.

Select Graphical User Interface from the + sign.

You also open the Graphical User Interface creator by typing “guide “in the command window of MATLAB.

Select Blank GUI from GUIDE templates and click ok.

Drag and drop the push button available at the left side of the guide window, and resize to the desired length you want. Do the same for another button. One button is for turn on the LED and another push button is for Turn off the LED.

Right click on one of the button and select property inspector

In the property inspector window change the background color to any of your desired color it doesn’t mean you should change button color, but changing the color of the button will differentiate it from other button.



Scroll down the property inspector and change the string name , as this button is going to be used for turning on the LED I renamed the string to ON, you can type any of your desired name to it and also take a note of the Tag name that particular button associated with. Follow the same procedure for the second button and change the color to RED and change the string text to OFF, click on the green play button at the top of GUI file that you have created it will prompt you to save your file and save your file, type the name you wanted to call your first project and save it in safe place that you will not accidentally delete it in the future.

GUI generates two files,

You can see that files on the current folder on the left side of the matlab window. The file ending with fig contains the layout information and the file ending with .m contains the implementation code. When you save your GUI file the .m file will be automatically open in your MATLAB window like the one below. This file contains automatically generated code and space for us to add custom code to run when controls are selected, we have to add custom code for when turning on the button what should the function do and when we

select the turn off button what it should do? We have to specify such things on our custom code. The custom code is also known as the call back function.

Navigate to the function varargout to the end of this function add clear all; and declare a variable for Arduino hardware. Before adding custom code

After adding custom code

Clear all; command help us to clear all function that is currently accessing Arduino from MATLAB. Declaring a global variable “a” as this variable will using in the functions inside other code, assigning global variable to the Arduino hardware. After adding the custom function like the one attached in the above screen navigate to the button function, now we going to add custom function to the first button, when we push the first button what function MATLAB has to do with Arduino hardware has to define in the pushbutton1 function. Before adding custom command;

After adding custom command:

In the above custom command, calling the global variable which holds the Arduino hardware and the function writeDigitalPin helps to turn on or turn off the LED based on the value we applying to the function (a, ‘D3’, 1) “a” is the Arduino hardware and ‘D3’ is the pin we have connected the LED and ‘1’ will make the pin to go high. Now we have to add custom function to the second button, we have to add the same command as what we have added to the first button, instead of the value ‘1’ for the second button value should be changed to ‘0’. Before adding custom command to function pushbutton2

After adding custom command to function pushbutton2

If you planning to control the inbuilt LED change the digital pin from ‘D3’ to ‘D13’. Save the file and hit the run, you can find the run in the Editor tab.

Make sure you have connected your Arduino to your Laptop or PC before hit the run button. When you hit the run , it will take few seconds to establish communication with Arduino, you can check at the button of the screen there it will be display as busy wait till the busy

status to fade away, if you give any control during the busy status you may experience some errors.

Check out the Video tutorial of the same in this link here

RGB LED Color Mixing In this chapter you will learn how to create slider functions in MATLAB GUI and control an RGB LED connected to the Arduino Uno, RGB LED has connected to PWM pins of Arduino, which can be controlled from MATLAB using PWM write function using the slider.

Parts needed for this tutorial: 1) Arduino Uno. 2) Common cathode RGB LED (can use common anode too) 3) 3x 220 ohm Resistor and connecting wires. Connect Green pin of RGB LED to 9th pin of Arduino, Blue pin of RGB LED to 10th pin of Arduino, Red pin of RGB LED to 11th pin of Arduino, and connect the common ground pin to GND of Arduino, in case of common anode connect pin to 5V. Make sure to connect a resistor between LED pin and Arduino to protect the LED. Start MATLAB and type ‘guide’ in command window, and select blank GUI. Click on the Slider from the pallet on the GUI window.

Pick and drop the slider into the GUI window, drag and drop another two slider into the GUI in total of 3 sliders. Resize the sliders to make it fit into the window.

Right Click on the first Slider and select Property Inspector, change the background color to red and change the Max value to 5, leave the minimum value as same as 0.

Right click on the Second slider and change the color to green and also change the maximum value to 5, do the same for the third slider change the color to blue and also change the maximum value to 5.

Click on the run button from the GUI window which will ask to save the file, rename the file and save it. Now move to the .M file which has opened now in MATLAB window to add call back functions to the sliders. Find the function varargout in the .m file and replace the code with clear all; and declare a global variable and assign Arduino hardware to the global variable. Before adding custom command.

After adding custom command

Now we will add command to the first slider, when we move the first slider, it should change the PWM level in the pin 11, as we assigned the first slider red color and connected Red pin of RGB LED to 11th pin, we going to control the PWM of 11th pin. Before adding custom command for the first slider.

After adding custom command for the first slider.

Get (hObject, ‘Value’) command will return the current value of the slider, the value extracted using this command is stored in the variable b; writePWMVoltage command used for writing PWM function in the Arduino hardware from MATLAB. When the slider is changed, slider current value will be stored in the variable ‘b’ the value changes from 0 to 5, that is the accepted PWM change in MATLAB, in arduino this change from 0 to 255, as the RED LED has connected to 11 digital pin and also the PWM pin whenever the slider changes, current PWM voltage will be applied to the 11th pin. Same steps have to carried for the other two sliders, change the variables and copy paste the code which will make the work simpler. Before adding custom command for the second slider.

After adding custom command for the second slider.

Here the Digital pin 9 used for second Slider as the Green pin of RGB LED has connected to the 9th pin, it has changed to ‘D9’ in the custom command. Here a new variable has introduced to hold the value when the slider changes. Before adding custom command for the Third slider.

Before adding custom command for the Third slider.

Make all the changes to the .m file and add the necessary call back function to make things work when the slider position changes in the MATLAB GUI window. This tutorial can be followed as a video tutorial here Make sure you are connecting the correct color pin and selecting the corresponding pin when changing the custom command.

Control a Servo Motor In this chapter you will learn how to control a Servo motor using MATLAB GUI function, as we already learnt how to make a slider in the previous chapter, the same concept we going to implement in this chapter, we going to use the slider and move the servo motor position when we move the slider.

Parts needed for this tutorial 1) Arduino Uno 2) Many servo ( for this tutorial I am using only 2) 3) External power jack Make sure you are connecting external power supply for this tutorial when you are using servo motor with arduino , Servo consume more power which cannot be supplied from the USB , it is must to connect to external power for running smooth. Start MATLAB, type guide in the command window, and select blank GUI, Pick and place slider into the GUI window as like the same way did in the last chapter No need to change any values for the Slider function when using to control Servo motor.

Save the file and move to add custom command on .m file on the MATLAB window. I have connected Servo motors to 3rd and 6th pin of Arduino. When I change the first slider servo connected to the 3rd pin should respond when I move the second slider servo connected to the 6th pin should move for the slider position. Find the function varargout in the .m file and replace the code with clear all; and declare a global variable and assign Arduino hardware to the global variable. Before adding custom command.

After adding custom command.

When the Slider 1 changes we have to write a command to move the servo position accordingly to the Slider change. Before adding custom command to slider 1 function

After adding custom command to slider 1 function

The above custom command will get the value of the current slider position and stores into the variable b; and we are introducing another variable which assign the function that servo has attached to the Digital pin 3. Whenever Slider position changes from 0 to 1, Servo motor connected to the pin number 3 will move accordingly, continue the same for other servo attached to the pin number 6. Before adding custom command to slider 2 function

After adding custom command to slider 2 function

Save the file and run the function, make sure you properly connected your Arduino board to your PC or laptop before executing the program. You can also find the Video tutorial for this chapter here

DC Motor Control In this chapter you will learn about how to control a DC motor from MATLAB, For DC Motor control we going to use a Motor driver called L293D which is very simple H-bridge used for controlling motor in both direction, these can control 2 motors for this tutorial we going to concentrate on controlling a single motor.

Parts required for this tutorial 1) Arduino hardware any Arduino device can be used for this. 2) Toy DC Motor, you can also use any other type, the reason I am using a toy DC

motor is it consumes less power compare to the other Gear Motors 3) L293D Motor driver or any other Motor driver 4) Connecting wires. L293D pin out Enable pin of L293d connected to 9th pin of arduino which is the pwm pin, by applying PWM voltage to the Enable pin speed of the Motor can be controlled, Input 1 is connected to the 7th pin of Arduino and Input 2 is connected to 6th pin of Arduino, Output1 and Output2 pins are connected to the Motor. Logic behind L293D Motor Driver 7th pin (High)

6th pin (Low)

Anticlockwise Direction 7th pin (Low)

6th pin (High)

STOP

7th pin (Low)

6th pin (Low)

STOP

7th pin (High)

7th pin (High)

Clockwise Direction

In the MATLAB GUI we create 3 push buttons, one for rotating the motor to clockwise another button to rotate in anticlockwise and the last one is to stop the motor, and also a slider to control the speed of the motor, and last a text edit to display the current speed of the motor. When we press push button 1, on the custom command function we have write High to pin 7 and low to pin 6. When we press push button 2, on the custom command function we have to write Low to pin 7 and High to pin 6. When we press push button 3, on the custom command function we have to write Low to pin 7 and Low to pin 6.

When we move the Slider the current position of the slider has to apply to the pin number 9, as the PWM voltage varies from 0 to 5v, we need to set the Slider Max value to 5 in this case, and when we move the slider we can display the slider value in the edit text, as the slider moves from 0 to 5, we can multiply these value with 20 when displaying in the Edit text. Create a GUI function like the one shown in the Screenshot below, combine push button and Slider as we have seen how to create this in the previous chapters.

Right click on the slider and select property inspector and change the maximum value to 5 instead of 1, you also need to pick and place edit text from the pallet to display speed of the motor, and also right click on the push buttons and change the string and color of the buttons as you wish. Save the file and move to .m file at the MATLAB window. Add commands like clear all; declare global variable and assign Arduino hardware

to the global variable, in the function varargout.

On the call back function for the push button 1 add the following code , call global a; which holds the Arduino hardware, and Make the digital pin 6 to Low and 7th pin to High to rotate in Clockwise direction.

On the call back function for the push button 2 add the following code , call global a; which holds the Arduino hardware, and Make the digital pin 6 to High and 7th pin to Low to rotate in Anti-Clockwise direction.

On the call back function for the push button 3 add the following code , call global a; which holds the Arduino hardware, and Make the digital pin 6 to Low and 7th pin to Low to rotate in Anti-Clockwise direction.

On the slider function call back we need to edit more codes, firstly we have to store the slider value to a variable, on the function screenshot below, the variable slider is used for storing the data return from the slider movement, and the slider variable is multiplied by 20, when the slider move the maximum position its value will be 5 and if its multiplied by 20 the output will be 100, the final multiplied value will be displayed In the edit text. Function set(handles.edit1,‘String’, num2str(slider1)) The above function will display the slider1 data into edit text, it can only display string value so it should be converted from number to string before displaying the data. Edit the call back function in the slider as given in the screenshot below

Save the function and run the program, if there is no movement in the motor, check the connection between motor and motor driver. If still there is no movement connect an external power which will solve the problem if the arduino not supplying much power required by the motor this conditions may occur. The same example can be viewed as video tutorial here

Plotting Graph using Temperature data In this chapter you will learn about plotting graph using Temperature sensor, LM35 temperature sensor is used for this tutorial, LM35 sensor connected to analog pin of Arduino, we acquire analog data from Arduino and doing some calculation with the data return current temperature value, which can be plotted against number of count in the loop.

Parts required for this tutorial 1) Arduino Uno or Mega or any other type of Arduino 2) LM35 temperature sensor or any other type ( if you are using other type of sensor make sure you are also editing the value with the analog value) 3) Connecting wires. Connect the output of LM35 sensor to A0 pin of Arduino, the function is very simple acquiring analog data and doing some calculation with it (based on the LM35 datasheet) We going to create 3 functions here one is for plotting the data another one is for displaying the current temperature value and a push button for starting the program, when you press the push button it will acquire 100 samples from Arduino, if you want more sample you can edit by changing the loop.

Open MATLAB and type ‘guide’ in the command window, select blank GUI and axes from the pallet and resize to the required size. Drag and drop a push button from the pallet and also edit text and arrange in it in the GUI window. Save the file and move on to .m file the MATLAB window. Add commands like clear all; declare global variable and assign Arduino hardware to the global variable, in the function varargout.

When you click the pushbutton MATLAB should acquire 100 sample data and process it and plot graph and display the temperature data into edit text. All the function is on the pushbutton side so we need to enter custom command on the pushbutton call back function.

Update the custom command in the pushbutton 1 callback function and save the program. You can also check the video tutorial of this here

Contact and app inventor files. For suggestions and queries you can always mail me at [email protected] For all the code and necessary documents for this book please click here

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF