+ All Categories
Home > Documents > A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical...

A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical...

Date post: 05-Jul-2018
Category:
Upload: ahmad-reza-atef
View: 262 times
Download: 0 times
Share this document with a friend
44
Transcript

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 1/44

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 2/44

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.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 3/44

 

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 4/44

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 5/44

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 anLED, 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  Arduinoprogramming 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 objectsto 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 fastprototyping, aimed at students without a background in electronics and programming. Assoon as it reached a wider community, the Arduino board started changing to adapt to newneeds 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 andeventually adapt them to their particular needs. The software, too, is open-source, and it isgrowing through the contributions of users worldwide.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 6/44

MATLAB

MATLAB is a high-performance language for technical computing. It integratescomputation, visualization, and programming in an easy-to-use environment whereproblems and solutions are expressed in familiar mathematical notation. Typical usesinclude:

Math and computationAlgorithm 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 whereyou can connect an Arduino with MATLAB and start to work with on it, one of theadvantage of Arduino is, and it is a low cost hardware which can be used withMATLAB to acquire data and process data. Throughout this book you will knowhow to create a GUI in MATLAB to control Arduino.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 7/44

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 8/44

Getting started with Arduino and MATLAB

To use Arduino with MATLAB, we need a support package that needs to be installed onthe Arduino side to make communication with Arduino board. This is a one-timeinstallation and you also need MATLAB registered email id and password to complete theinstallation process. If you have not registered yet, register in order to download thesupport software.

You need to have your matlab installed on your system in order to continue the belowsteps, 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 onyour desktop or search for MATLAB on your Programs.

2)  Open MATLAB and click the Add-Ons drop down menu on the top right of theMATLAB 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

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 9/44

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.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 10/44

 

Continue and Complete the Installation

Accept the license agreement on the next screen and click Next through the followingscreens to finish the installation for both MATLAB and Simulink Support Package forArduino.

 

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

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 11/44

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

You should see the above progress bar to get confirmation that the installation process isgoing smooth and downloaded has completed and installation is taking place. It will takesome time to complete the installation process and it is completely depend on yourInternet speed, It took me 1 hour to complete the whole process, so be patience during theinstallation process don’t click cancel while downloading, sit and relax still the processgets 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

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 12/44

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 13/44

Checking communication between Arduino and MATLAB:

In order to check communication between Arduino and MATLAB you need to connectyour Arduino device to MATLAB, you can use any of the Arduino device, for thisexample 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 commandyou will be returned with the arduino properties of your Arduino hardware.

If you are using Arduino clone, sometime MATLAB finds difficult to find the propertiesof your Arduino hardware, if it’s not returned with any properties type the followingcommand,

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 ArduinoIDE examples and make sure it is connecting to Arduino IDE before coming to aconclusion that it is not working with MATLAB.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 14/44

  Make sure to delete the variablein the workspace, this may sometimeprevent us to not use arduino hardwarewith other programs.

 

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 15/44

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 16/44

Creating a GUI function to Control an LED

In this lesson you will learn how to create GUI (Graphical User Interface) in MATLABto control an LED connected to Arduino Uno.

Things you needed to make this tutorial.

1. Arduino Uno or any other type of Arduino hardware2. An LED3. A 220 ohm resistor.

 

If you want to try this tutorial with the inbuilt LED connected to pin number 13you 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.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 17/44

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

 

Select Blank GUI from GUIDE templates and click ok.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 18/44

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 theLED.

 

Right click on one of the button and select property inspector

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 19/44

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

 

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 20/44

Scroll down the property inspector andchange the string name , as this button isgoing to be used for turning on the LED Irenamed the string to ON, you can type anyof your desired name to it and also take anote of the Tag name that particular button

associated with. Follow the same procedurefor the second button and change the color toRED 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 tosave your file and save your file, type the name you wanted to call your first project andsave 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 fileending with fig contains the layout information and the file ending with .m contains theimplementation code. When you save your GUI file the .m file will be automatically openin your MATLAB window like the one below. This file contains automatically generatedcode and space for us to add custom code to run when controls are selected, we have toadd custom code for when turning on the button what should the function do and when we

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 21/44

select the turn off button what it should do? We have to specify such things on our customcode. 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 avariable 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 fromMATLAB. Declaring a global variable “a” as this variable will using in the functionsinside other code, assigning global variable to the Arduino hardware.

After adding the custom function like the one attached in the above screen navigate to thebutton function, now we going to add custom function to the first button, when we pushthe first button what function MATLAB has to do with Arduino hardware has to define inthe pushbutton1 function.

Before adding custom command;

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 22/44

Afteraddingcustomcommand:

In the above custom command, calling the global variable which holds the Arduinohardware and the function writeDigitalPin helps to turn on or turn off the LED based onthe value we applying to the function (a, ‘D3’, 1) “a” is the Arduino hardware and ‘D3’ isthe 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 samecommand as what we have added to the first button, instead of the value ‘1’ for the secondbutton 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’. Savethe 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

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 23/44

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

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

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 24/44

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 25/44

RGB LED Color Mixing

In this chapter you will learn how to create slider functions in MATLAB GUI andcontrol an RGB LED connected to the Arduino Uno, RGB LED has connected to PWMpins of Arduino, which can be controlled from MATLAB using PWM write function usingthe 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 to10th pin of Arduino, Red pin of RGB LED to 11th pin of Arduino, and connect thecommon ground pin to GND of Arduino, in case of common anode connect pin to5V. Make sure to connect a resistor between LED pin and Arduino to protect theLED.

Start MATLAB and type ‘guide’ in command window, and select blank GUI. Click on theSlider from the pallet on the GUI window.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 26/44

Pick and drop the slider into the GUI window, drag and drop another two slider into theGUI 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 colorto 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 alsochange the maximum value to 5.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 27/44

Click on the run button from the GUI window which will ask to save the file, rename thefile 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 aglobal variable and assign Arduino hardware to the global variable.

Before adding custom command.

After adding custom command

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 28/44

Now we will add command to the first slider, when we move the first slider, it shouldchange the PWM level in the pin 11, as we assigned the first slider red color andconnected 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 valueextracted using this command is stored in the variable b;

writePWMVoltage command used for writing PWM function in the Arduino hardwarefrom MATLAB.

When the slider is changed, slider current value will be stored in the variable ‘b’ the valuechanges from 0 to 5, that is the accepted PWM change in MATLAB, in arduino thischange from 0 to 255, as the RED LED has connected to 11 digital pin and also the PWMpin 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 pastethe code which will make the work simpler.

Before adding custom command for the second slider.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 29/44

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 connectedto the 9th pin, it has changed to ‘D9’ in the custom command. Here a new variable hasintroduced 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 makethings 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 pinwhen changing the custom command.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 30/44

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 31/44

Control a Servo Motor

In this chapter you will learn how to control a Servo motor using MATLAB GUIfunction, as we already learnt how to make a slider in the previous chapter, the sameconcept we going to implement in this chapter, we going to use the slider and move theservo motor position when we move the slider.

Parts needed for this tutorial

1)  Arduino Uno2)  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 areusing servo motor with arduino , Servo consume more power which cannot besupplied from the USB , it is must to connect to external power for runningsmooth.

Start MATLAB, type guide in the command window, and select blank GUI, Pick and placeslider into the GUI window as like the same way did in the last chapter No need to changeany values for the Slider function when using to control Servo motor.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 32/44

Save the file and move to add custom command on .m file on the MATLAB window. Ihave connected Servo motors to 3rd and 6th pin of Arduino. When I change the first sliderservo connected to the 3rd  pin should respond when I move the second slider servoconnected 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 aglobal 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 positionaccordingly to the Slider change.

Before adding custom command to slider 1 function

 

After adding custom command to slider 1 function

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 33/44

The above custom command will get the value of the current slider position and stores intothe variable b; and we are introducing another variable which assign the function thatservo 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 forother 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 boardto your PC or laptop before executing the program.

You can also find the Video tutorial for this chapter here

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 34/44

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 35/44

DC Motor Control

In this chapter you will learn about how to control a DC motor from MATLAB, For DCMotor control we going to use a Motor driver called L293D which is very simple H-bridgeused for controlling motor in both direction, these can control 2 motors for this tutorial wegoing 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

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 36/44

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 pinof arduino which is the pwm pin, byapplying PWM voltage to the Enable pinspeed of the Motor can be controlled,Input 1 is connected to the 7th  pin ofArduino and Input 2 is connected to 6th

pin of Arduino, Output1 and Output2 pinsare connected to the Motor.

 

Logic behind L293D Motor Driver

Clockwise Direction 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)

 

In the MATLAB GUI we create 3 push buttons, one for rotating the motor toclockwise another button to rotate in anticlockwise and the last one is to stop themotor, and also a slider to control the speed of the motor, and last a text edit todisplay the current speed of the motor.

 

When we press push button 1, on the custom command function we have writeHigh to pin 7 and low to pin 6.

 

When we press push button 2, on the custom command function we have to writeLow to pin 7 and High to pin 6.

 When we press push button 3, on the custom command function we have to writeLow to pin 7 and Low to pin 6.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 37/44

 

When we move the Slider the current position of the slider has to apply to the pinnumber 9, as the PWM voltage varies from 0 to 5v, we need to set the Slider Maxvalue to 5 in this case, and when we move the slider we can display the slider valuein the edit text, as the slider moves from 0 to 5, we can multiply these value with20 when displaying in the Edit text.

 

Create a GUI function like the one shown in the Screenshot below, combine pushbutton 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 maximumvalue to 5 instead of 1, you also need to pick and place edit text from the pallet todisplay speed of the motor, and also right click on the push buttons and change thestring 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

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 38/44

to the global variable, in the function varargout.

 

On the call back function for the push button 1 add the following code , call globala; 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 globala; which holds the Arduino hardware, and Make the digital pin 6 to High and 7 th

pin to Low to rotate in Anti-Clockwise direction.

 

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

 

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 39/44

 

On the slider function call back we need to edit more codes, firstly we have to storethe slider value to a variable, on the function screenshot below, the variable slideris used for storing the data return from the slider movement, and the slider variableis multiplied by 20, when the slider move the maximum position its value will be 5and if its multiplied by 20 the output will be 100, the final multiplied value will bedisplayed 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 displaystring value so it should be converted from number to string before displaying thedata.

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 theconnection between motor and motor driver. If still there is no movement connect anexternal power which will solve the problem if the arduino not supplying much powerrequired by the motor this conditions may occur.

The same example can be viewed as video tutorial here

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 40/44

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 41/44

Plotting Graph using Temperature data

In this chapter you will learn about plotting graph using Temperature sensor, LM35temperature sensor is used for this tutorial, LM35 sensor connected to analog pin ofArduino, we acquire analog data from Arduino and doing some calculation with the datareturn 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 ofsensor 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 simpleacquiring 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 fordisplaying 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 moresample you can edit by changing the loop.

 

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 42/44

Open MATLAB and type ‘guide’ in the command window, select blank GUI and axesfrom the pallet and resize to the required size. Drag and drop a push button from the palletand 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 hardwareto the global variable, in the function varargout.

 

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

function.

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 43/44

Update the custom command in the pushbutton 1 callback function and save the program.

You can also check the video tutorial of this here

 

8/16/2019 A Quicstart guide to MATLAB GUI k Start Guide to MATLAB GUI for Controlling Arduino Create Graphical User Interfa…

http://slidepdf.com/reader/full/a-quicstart-guide-to-matlab-gui-k-start-guide-to-matlab-gui-for-controlling 44/44

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

 


Recommended