5. 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.
6. 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.
7.
8. 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
10. 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
13. 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 devtty.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.
19. 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.
20. 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
22. 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
25. 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.
28. 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.
31. 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.
33. 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
36. 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
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 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.
41. 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.
42. 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.