0% found this document useful (0 votes)
49 views

Interfacing Arduino With Matlab (First Book 1) A

Uploaded by

end31750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Interfacing Arduino With Matlab (First Book 1) A

Uploaded by

end31750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 98

I N T E R FACI N G AR DUI N O W

I T H M AT L AB
RAJESH DEY SUBHAJIT PAUL RAJKUMAR
MANDAL BIJOY PAL

Dedicated to RAMKRISHNA
MISSION & Dr. Achintya Das
Book Title Copyright © 2018 by Rajesh Dey & Co. All Rights Reserved.

All rights reserved. No part of this book may be reproduced in any form or by any electronic or mechanical
means including information storage and retrieval systems, without permission in writing from the author.
The only exception is by a reviewer,

who may quote short excerpts in a review.


Cover designed by Cover Designer Subhendu Ghosh
This book is a work of fiction. Names, characters, places, and incidents either are products of the author’s
imagination or are used fictitiously. Any resemblance to actual persons, living or dead, events, or locales is
entirely coincidental.

Limits of Liability/Disclaimer of Warranty: The publisher and the author make no representation or
warranties with respect to the accuracy or completeness of the contents of this work and specifically
disclaim all warranties, including without

limitation warranties of fitness for a particular purpose. No warranty may be created or extended by scales
and promotional materials . The advice and strategies contained here in may not be suitable for every
situation.

Author Name

Rajesh Dey
Subhajit Paul
Rajkumar Mandal
Bijoy Pal
Visit my website at www.electroinventors.com

Printed in the United States of America

First Printing: Feb 2018 Name of Company The Electro Inventor

ISBN-9781980284512
AUTHOR’S BIOGRAPHY
Raje sh Dey An Assistant Professor, Dept. of ECE, SDET-BGI, Kolkata, India,
under West Bengal University of Technology, India. He holds an M.Tech degree
in ECE from Kalyani Government Engineering College, Kalyani, Nadia, West
Bengal, India under West Bengal University of Technology, India. The Author
has 12 years of teaching experience along with 6 years of Industrial experience.
His research interests include control engineering, Adaptive signal processing,
sensor etc.

Sub hajitPaul(B.Tech.)
Dept. of ECE, SDET-BGI, Kolkata, India, under West Bengal University of
Technology, India. He has been the finalists of top National level competitions
like KSHITIJ (IIT Kharagpur) and SPARKLE (KPIT Technologies Ltd.). He is
presently working as an engineer in a reputed automation industry. His research
interests include Robotics, Arduino, Embedded System, Automation,Java, C,
C++, sensor etc.
Rajkumar Mandal M.Tech Dept. of ECE, SDET-BGI,

Kolkata, India, under West Bengal University of Technology, India.His research


interests include control engineering, Adaptive signal processing, sensor etc

B ijoy Pal B.Tech Student Dept. of CSE, SDET-BGI, Kolkata, India, under West
Bengal University of Technology, India. His research interests include IOT,
Embedded System, Algorithm, Python, Java, DBMS, C, C++, sensor etc.
CONTENTS
Author’s Biography.....................................................................................................2
Preface .........................................................................................................................5
chapter 1 ...................................................................................................................... 7
chapter 2....................................................................................................................29
chapter 3 .................................................................................................................... 61
chapter 4....................................................................................................................89
PREFACE
This book has been written to have a knowledge regarding
MATLAB and Arduino based some projects. MATLAB (matrix
laboratory or grid research facility) is a multiworldview
numerical processing condition. An exclusive programming
dialect created by MathWorks, MATLAB permits grid controls,
plotting of capacities and information, usage of calculations,
formation of UIs, and interfacing with programs written in
different dialects, including C, C++, C#, Java, Fortran and
Python. A microcontroller is a single-chip computer. Before the
invention of the microcontrollers most intelligent systems were
premeditated using microprocessors. In this era Arduino is very
popular microcontroller, for its easy to access and the open source
activities and also the programming language to boot the
microcontroller is very easy. So that any person can build their
project through Arduino. In this book has been written to
interface the Arduino module with MATLAB programming
language Chapter 1 provides a basic idea based on MATLAB
programming language. This chapter is containing History,
Syntax, some projects using MATLAB
Chapter 2 provides an idea based on Arduino and their feature,
types, different units which there by helps us to implement
different projects based on Arduino and also contains some
projects regarding Arduino.
Chapter 3 provides an idea interfacing Arduino with MATLAB
and containing some project regarding Arduino with MATLAB
programming.
Chapter 4 provides an idea on Data Acquisition System
CHAPTER 1 MATLAB BASICS
1.1 INTRODUCTION:

MATLAB (matrix laboratory or grid research facility) is a multiworldview


numerical processing condition. An exclusive programming dialect created by
MathWorks, MATLAB permits grid controls, plotting of capacities and
information, usage of calculations, formation of UIs, and interfacing with
programs written in different dialects, including C, C++, C#, Java, Fortran and
Python.

In spite of the fact that MATLAB is planned principally for numerical


processing, a discretionary tool stash utilizes the MuPAD emblematic motor,
enabling access to representative registering capacities. An extra bundle,
Simulink, includes graphical multi-space recreation and model-based outline for
dynamic and implanted frameworks.
Starting at 2017, MATLAB has more than 2 million clients crosswise over
industry and academia. MATLAB clients originate from different foundations of
designing, science, and financial aspects.

1.2 HISTORY:

Cleve Moler, the administrator of the software engineering office at the


University of New Mexico, began creating MATLAB in the late 1970s. He
composed it to give his understudies access to LINPACK and EISPACK without
them learning Fortran. It soon spread to different colleges and found a solid
group of onlookers inside the connected science group. Jack Little, a specialist,
was presented to it amid a visit Moler made to Stanford University in 1983.
Perceiving its business potential, he joined with Moler and Steve Bangert. They
changed MATLAB in C and established MathWorks in 1984 to proceed with its
advancement. These revised libraries were known as JACKPAC. In 2000,
MATLAB was reworked to utilize a more up to date set of libraries for grid
control, LAPACK.

MATLAB was first embraced by scientists and experts in control designing,


Little's forte, yet rapidly spread to numerous different areas. It is currently
likewise utilized as a part of instruction, specifically the educating of direct
polynomial math, numerical examination, and is prevalent among researchers
engaged with picture handling.

1.3 SYNTAX:

The MATLAB application is worked around the MATLAB scripting dialect.


Regular utilization of the MATLAB application includes utilizing the Command
Window as an intelligent numerical shell or executing content documents
containing MATLAB code.

1.3.1 Variables:

Factors are assignment characterized utilizing the task administrator or operator,


=. MATLAB is a feebly written

programming dialect since sorts are verifiably changed over. It is a construed


written dialect since factors can be relegated without proclaiming their sort,
aside from in the event that they are to be dealt with as representative items, and
that their sort can change. Qualities can originate from constants, from
calculation including estimations of different factors, or from the yield of a
capacity. For instance:

1.3.2 Vectors and Matrices:

A basic exhibit is characterized utilizing the colon punctuation:


init:increment:terminator. For example:
defines a variable named array (or assigns a new value to an existing variable
with the name array) which is an array consisting of the values 1, 3, 5, 7, and 9.
That is, the array starts at 1

(the init value), increments with each step from the previous value by 2 (the
increment value), and stops once it reaches (or to avoid exceeding) 9 (the
terminator value).

assigns to the variable named ari an array with the values 1, 2, 3, 4, and 5, since
the default value of 1 is used as the incrementer.

Indexing is one-based, which is the usual convention for matrices in


mathematics, although not for some programming languages such as C, C++,
and Java.

Matrices can be defined by separating the elements of a row with blank space or
comma and using a semicolon to terminate each row. The list of elements should
be surrounded by square brackets: []. Parentheses: () are used to access elements
and subarrays (they are also used to denote a function argument list).

A square identity matrix of size n can be generated using the function eye, and
matrices of any size with zeros or ones can be generated with the functions zeros
and ones, respectively.
Transposing a vector or a matrix is done either by the function transpose or by
adding prime to the matrix.

1.3.3 Structures:

MATLAB has structure information sorts. Since all factors in MATLAB are
exhibits, a more sufficient name is "structure cluster", where every component of
the exhibit has a similar field names. Furthermore, MATLAB underpins dynamic
field names (field look-ups by name, field controls, and so on.). Sadly,
MATLAB JIT does not bolster MATLAB structures, consequently only a basic
packaging of different factors into a structure will include some significant
downfalls.

1.3.4 Functions:

MATLAB bolsters components of lambda analytics by presenting capacity


handles, or capacity references, which are executed either in .m documents or
unknown/settled capacities.

1.3.5 Classes and Object-Oriented Programming:


MATLAB underpins question situated programming including classes, legacy,
virtual semantics, and cruise dispatch, bundles, cruise by-esteem

by-reference semantics. However, the linguistic structure and calling traditions


are essentially not quite the same as different dialects. MATLAB has esteem
classes and reference classes, contingent upon whether the class has handle as a
super-class (for reference classes) or not (for esteem classes).

Method call behavior is different between value and reference classes. For
example, a call to a method

can alter any member of object only if object is an instance of a reference class.
An example of a simple class is provided below.

When put into a file named , this can be executed with the following
commands:

1.4 USING MATLAB AS CALCULATOR:

For instance of a basic intelligent count, simply sort the articulation you need to
assess. We should begin at the absolute starting point. For instance, how about
we assume you need to ascertain the articulation, 1 + 2 * 3. You write it at the
provoke summon (>>) as takes after,

>> 1+2*3 a = 7

You will have seen that on the off chance that you don't indicate a yield variable,
MATLAB utilizes a default variable a, short for reply, to store the after effects of
the present computation. Note that the variable an is

made (or overwritten, in the event that it is now existed). To stay away from this,
you may allot an incentive to a variable or yield contention name. For instance,

>> x = 1+2*3
x=7

Will bring about x being given the esteem 1 + 2 * 3 = 7. This variable name can
simply be utilized to allude to the consequences of the past calculations. In this
manner, registering 4x will bring about

>> 4*x
a = 28.0000

Functions in
Symbol Operation Example + Addition 2+3
- Subtraction 2-3 * Multiplication 2*3 / Division 2/3

A portion of the capacities in Matlab are inbuilt and some are client
characterized.
Ex:
X = ones (3, 3);
111
111
111
For more insights with respect to capacities utilize help alternative in summon
window.
Ex: >>help ones
Matlab Matrices:

Matlab regards all factors as grids. For our motivations a network can be thought
of as a cluster, indeed, that is the way it is put away. Vectors are extraordinary
types of frameworks and contain just a single line OR one section.

Ex: push vector = [1 27 74];


x = [1, 2, 5];
Note: To isolate two components one can utilize space or comma. Scalars are
lattices with just a single
line AND one segment.
Ex: Colvector = [5; 45; 89]
A lattice can be made in Matlab as takes after: >>matrix = [1, 2, 3; 4, 5, 6; 7, 8,
9];
lattice = 1 2 3
456
589
Extracting a Sub-Matrix:

A bit of a network can be removed and put away in a littler lattice by


determining the names of the two frameworks and the lines and segments to
separate. The language structure is sub_matrix = network (r1: r2, c1: c2) ;
Where r1 and r2 indicate the start and completion lines and c1 and c2 determine
the start and consummation segments to be removed to make the new network
Plotting cases:

1)
>> x = [1 2 3 4 5 6];
>> y = [3 - 1 2 4 5 1];
>> plot(x,y)

Spare, Load,
Delete Workspace Variables:

The workspace isn't kept up crosswise over sessions of MATLAB. When you
quit MATLAB, the workspace clears. In any case, you can spare any or the
greater part of the factors in the present workspace to a MAT-record (.tangle).
You can stack MAT-records at a later time amid the current MATLAB session, or
amid another session, in the event that you need to reuse the workspace factors.
Linguistic structure for spare spare myfile VAR1 VAR2

or, then again


save(‗myfile','VAR1','var2')
Grammar for Load
stack filename
stack ('filename')
stack filename.ext
stack filename – ascii
stack filename – tangle
Plotting with Matlab:

Matlab has a considerable measure of capacity for plotting information. The


essential one will plot one vector versus another. The first will be dealt with as
the abscissa (or x) vector and the second as the ordinate (or y) vector. The
vectors must be a similar length.

>> plot (time, dist) % plotting versus time

Matlab will likewise plot a vector versus its own record. The file will be dealt
with as the abscissa vector. Given a vector ―timeǁ and a vector ―distǁ we could
state: >> plot (dist) % plotting versus file
Yield: is from a decent serial program.

At times, composing a decent serial code might be adequate for your transient
needs.In general, pre-allotment of clusters (as opposed to developing them
progressively) is likewise a critical piece of composing productive Matlab code.

The serial port session involves every one of the means you are probably going
to bring when speaking with a gadget associated with a serial port. These means
are: Create a serial port question — Create a serial port protest for a particular
serial port utilizing the serial creation work. Design properties amid protest
creation if important.

2) Plotting a sine wave: >> x = 0:pi/100:2*pi; >> y = sin(x);


>> plot(x,y)
Fig: Sine wave plot

1.5GRAPHICS AND GRAPHICAL USER INTERFACE


PROGRAMMING:

MATLAB bolsters creating applications with graphical UI (GUI) highlights.


MATLAB incorporates GUIDE (GUI improvement condition) for graphically
outlining GUIs. It likewise has firmly coordinated diagram plotting highlights.
For instance, the capacity plot can be utilized to deliver a diagram from two
vectors x and y. The code:

produces the following figure of the sine function


A MATLAB program can produce three-dimensional graphics using the
functions surf, plot3 or mesh.

This code produces a wireframe 3D plot of the two-dimensional unnormalized


sinc function:
This code produces a surface 3D plot of the two-dimensional unnormalized sinc
function:
1.6EXAMPLES OF ADVANCED PROGRAMMING USING
MATLAB: Example 1: RADIATION PATTERN OF A
UNIFORM LINEAR ARRAY (ULA)
PROGRAM CODE:
OUTPUT:

Enter the value of wavelength=10 Enter no. of antenna element=3 ans = 0

ans =9
ans =18
ans = 27
ans = 36
ans =45
ans =54
ans =63
ans =72
ans = 81
ans =90
ans =99.0000
ans =108
ans = 117
ans =126
ans = 135
ans = 144
ans =153
ans =162
ans = 171

ans = 180 RADIATION PATTERN OF A ULA


NAME:POULAMI SAHA
ROLL NO.:BSSE/UT/EC/13/008
DATE:24-03-15

90 3 ULA 120 60
2
150 30
1
180 0
210 330
240 300 270

Example 2: 3D AND 2D PLOT OF HALF WAVE DIPOLE


ANTENNA RADIATION PATTERN PROGRAM CODE:
OUTPUT:
3D RADIATION PATTERN OF A HALF WAVE DIPOLE ANTENNA NAME:POULAMI SAHA
ROLL NO.:BSSE/UT/EC/13/008
DATE:01-04-15

0.2
0
-0.2

0.8 0.6 0.4 0.8 0.2 0.6 0


0.4

0.2 -0.20
-0.4 -0.2

-0.6-0.4
-0.8
-0.6

-0.8
y-axis
2D RADIATION PATTERN OF A HALF WAVE DIPOLE ANTENNA

NAME:POULAMI SAHA

OUTPUT:
ROLL NO.:BSSE/UT/EC/13/008
DATE:01-04-15
90 AT PHI=90
0.4
120 60
0.3
150 0.2 30
0.1
180 0
210 330
240 300 270
CHAPTER 2
ARDUINO BASICS
2.1INTRODUCTION:

Arduino is an open-source platform used for building different electronics


projects. Arduino consists of both a physical programmable circuit board (often
referred to as a microcontroller) and a piece of software, or IDE (Integrated
Development Environment) that runs on your computer, used to write and
upload computer code to the physical board.

The Arduino platform has become quite popular with people just starting out
with electronics, and for good reason. Unlike most programmable circuit boards,
the Arduino does not need a separate piece of hardware (called a programmer) in
order to load new code onto the board – we can simply use a USB cable.
Additionally, the Arduino IDE uses a simplified version of C++, making it easier
to learn to program. Finally, Arduino provides a standard form factor that breaks
out the functions of the micro-controller into a more accessible package.
Arduino also makes simpler the working process of

microcontroller, but it gives some advantages over other

systems for teachers, students and beginners.


 Inexpensive
 Cross-platform
 Simple, clear programming environment
 Open source and extensible software

2.2 DIFFERENT TYPES OF ARDUINO BOARD:

The list of Arduino boards includes the following such as  Arduino Uno (R3)
 LilyPad Arduino
 Red Board
 Arduino Mega (R3)
 Arduino Leonardo
ARDUINO UNO (R3):

The Uno is an immense choice for your underlying Arduino. It comprises of 14-
advanced I/O pins, where 6-pins can be utilized as PWM(pulse width balance
yields), 6-simple information sources, a reset catch, a power jack, a USB
association and the sky is the limit from there. It incorporates everything
required to hold up the microcontroller; essentially append it to a PC with the
assistance of a USB link and give the supply to begin with an AC-to-DC
connector or battery.

Fig.1: Arduino UNO(R3).

2.2.2 LILYPAD ARDUINO BOARD:

The Lily Pad Arduino board is a wearable e-material innovation extended by


Leah " Buechley" and circumspectly outlined by "Leah and SparkFun". Each
board was creatively planned with gigantic interfacing cushions and a smooth
back to give them to be sewn into dress utilizing conductive a chance to string.
This Arduino additionally includes I/O, control, and furthermore sensor sheets
which are assembled particularly for e-materials. These are even washable!

Fig. 2: Arduino Lilypad.

2.2.3 REDBOARD ARDUINO BOARD:

The RedBoard Arduino board can be modified utilizing a Mini-B USB link
utilizing the Arduino IDE. It will take a shot at Windows 8 without modifying
your security settings. It is more consistent due to the USB or FTDI chip we
utilized and furthermore it is altogether level on the back. Making it is extremely
easy to use in the task outline. Simply plug the board, select the menu alternative
to pick an Arduino UNO and you are prepared to transfer the program. You can
control the RedBoard over USB link utilizing the barrel jack.
Fig.3: Redboard Arduino.

2.2.4 ARDUINO MEGA (R3) BOARD:

The Arduino Mega is like the UNO's huge sibling. It incorporates loads of
advanced I/O pins (from that, 14-pins can be utilized as PWM o/ps), 6-simple
sources of info, a reset catch, a power jack, a USB association and a reset catch.
It incorporates everything required to hold up the microcontroller; essentially
connect it to a PC with the assistance of a USB link and give the supply to begin
with an AC-to-DC connector or battery.The colossal number of pins make this
Arduino board extremely accommodating for planning the activities that need a
group of advanced I/ps or o/ps like parcels catches.
Fig.4: Arduino Mega (R3).

2.2.5 ARDUINO LEONARDO BOARD:

The principal improvement leading group of an Arduino is the Leonardo board.


This board utilizes one microcontroller alongside the USB. That implies, it can
be extremely straightforward and shoddy too. Since this board handles USB
specifically, program libraries are possible which let the Arduino board to take
after a console of the PC, mouse, and so on.
Fig.5: Arduino
Leonardo.

2.2.6 THE ARDUINO SHIELDS:

Also, Arduino shields are pre constructed circuit sheets used to associate with
various Arduino sheets. These shields fit on the highest point of the Arduino
perfect sheets to give an extra abilities like associating with the web, engine
controlling, giving remote correspondence, LCD screen controlling, etc..The
distinctive sorts of an Arduino shields are

 Wireless Shields
 The GSM Shield
 The Ethernet Shield
 The Proto Shields
Fig.6: Arduino Shields.

2.3 HARDWARE:

Now detailed analysis of pin configuration is as follows:
 Power (USB / Barrel Jack):

Every Arduino board requires a way to be connected to a power source. The


Arduino UNO can be powered from a USB cable coming from your computer or
a wall power supply that is terminated in a barrel jack. In the picture above the
USB connection is labelled (1) and the barrel jack is labelled (2).

The USB connection is also how you will load code onto your Arduino board.

NOTE: Do not use a power supply greater than 20 Volts as you will overpower
your Arduino so that it might be destroyed. The standard voltage for most
Arduino models is between 6 and 12 Volts.

Pins(5v,3.3v,GND,Analog,Digital,PMW,AREP)

The pins on your Arduino are the places where you connect wires to make a
circuit possibly in conjunction with a breadboard and some wire. They generally
have black plastic ‘headers’ that allow you to just plug a wire accurately into the
board. The Arduino has different kinds of pins, each of which is labelled on the
board and used for different utilities.

5V (4) & 3.3V (5): The 5V pin supplies 5 volts of power, and the 3.3V pin
supplies 3.3 volts of power. Most of the simple components used with the
Arduino run accurately in 5 or 3.3 volts.

GND (3): There are a number of GND pins on the Arduino, any of which can
be used to ground your circuit.
Analog (6): The pins under the ‘Analog In’ label (A0 through A5 on the
UNO) are Analog In pins. These pins can read the signal from an analog sensor
and convert it into a digital equivalent that we can read.
Digital (7): The digital pins are from 0 through 13 on the UNO. These pins
can be used for both digital input and digital output.
PWM (8): You may have observed the tilde (~) next to some of the digital
pins (3, 5, 6, 9, 10, and 11 on the UNO). These pins worked as normal digital
pins, but can also be used for a special purpose called Pulse-Width Modulation
(PWM).
AREF (9): It stands for Analog Reference. Most of the time you can leave this
pin unconnected. It is sometimes used to set an external reference voltage
between 0 and 5 Volts for the analog input pins.

 Reset Button:

The Arduino has a reset button (10). Pushing it will momentarily connect the
reset pin to ground and restart any code that is loaded on the Arduino. This can
be very worthwhile if your code doesn’t repeat, but you want to test it a number
of times.

 Power LED Indicator:

Just beneath and to the right of the word “UNO” on your circuit board, there’s a
small LED beside the word ‘ON’ (11). This LED should light up whenever your
Arduino is plugged into a power source. If this light doesn’t turn on, there’s a
definite chance that something is erroneous. Need to re-check your circuit once.

 TX RX LEDs:

TX is short for transmit, RX is short for receive. These markings appear quite a
bit in electronics to specify the pins responsible for serial communication i.e. the
bit by bit communication. In our case, there are two particular places on the
Arduino UNO where TX and RX appear – once by digital pins 0 and 1, and a
second time next to the TX and RX indicator LEDs (12). These LEDs will give
us some wonderful visual indications whenever our Arduino is receiving or
transmitting data.

 Main IC:

The black entity with all the metal legs is an IC, or Integrated Circuit (13).
Visualize it as the brains of our Arduino. The main IC on the Arduino is a little
different from board type to board type, but is typically from the ATmega line of
IC’s from the ATMEL company. This can be important, as you may need to
know the IC type along with your board type before loading up a new program
from the Arduino software. This information can generally be found in writing
on the top side of the IC. If you want to know more about the difference between
various IC’s, going through the datasheets is often a good idea.
 Voltage Regulator:

The voltage regulator (14) is not essentially something you can interact with on
the Arduino. But it is hypothetically useful to know that it is there and what it’s
for. The voltage regulator does exactly what it says – it controls the amount of
voltage to maintain a constant value that is let into the Arduino board.
Eventually, it has its limits, so don’t plug in your Arduino to anything greater
than 20 volts.

2.4 SOFTWARE AND PROGRAMMING TOOLS:

A program for Arduino might be composed in any programming dialect for a


compiler that produces parallel machine code for the objective processor. Atmel
gives an advancement situation to their microcontrollers, AVR Studio and the
more up to date Atmel Studio.

The Arduino venture gives the Arduino improvement condition (IDE), which is
a application written in the programming dialect Java. It began from the IDE for
the dialects Processing and Wiring. It incorporates a code manager with
highlights, for example, content reordering, looking and supplanting content,
programmed indenting, support coordinating, and sentence structure featuring,
and gives straightforward a single tick components to aggregate and transfer
projects to an Arduino board. It additionally contains a message zone, a content
reassure, a toolbar with catches for normal capacities and a progression of
operation menus.
coordinated

cross-stage

A program composed with the IDE for Arduino is known as an outline.


Representations are saved money on the advancement PC as content records
with the document augmentation .ino. Arduino Software (IDE) pre-1.0 spared
portrays with the expansion .pde.
The Arduino IDE bolsters the dialects C and C++ utilizing unique standards of
code organizing. The Arduino IDE supplies a product library from the Wiring
venture, which gives numerous regular info and yield systems. Client composed
code just requires two essential capacities, for beginning the draw and the
primary program circle, that are aggregated and connected with a program stub
fundamental() into an executable cyclic official program with the GNU
toolchain, additionally included with the IDE dissemination. The Arduino IDE
utilizes the program avrdude to change over the executable code into a content
document in hexadecimal encoding that is stacked into the Arduino board by a
loader program in the board's firmware.

Because of the organization's open source nature, there exist many free open
libraries for designers to use to increase their activities.

2.4.1 PROGRAM STRUCTURE:

A minimal Arduino C/C++ program consist of only two functions:  setup():


This function is called once when a sketch starts after power-up or reset. It is
used to initialize variables, input and output pin modes, and other libraries
needed in the sketch.

 loop(): After setup() has been called, function loop() is executed repeatedly in
the main program. It controls the board until the board is powered off or is reset.
Most Arduino boards contain a light-emitting diode (LED) and a load resistor
connected between pin 13 and ground, which is a convenient feature for many
tests and program functions. A typical program for a beginning Arduino
programmer blinks a LED repeatedly.

/* Blink Turns on an LED on for one second, then off for one second,
repeatedly.*/

#define LED_PIN 13 // Pin number attached to LED.


// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_PIN as an output.
pinMode(LED_PIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

This program uses the functions pinMode(), digitalWrite(), and delay(), which
are provided by the internal libraries included in the IDE environment.
Fig.7: Arduino Software tool.
Fig.8: The Blink .

2.5 PROJECTS ON ARDUINO:


2.5.1 Cell Phone Controlled AC using Arduino and Bluetooth

In the present current world, any place we go we have heaps of electronic


gadgets around us. In any case, out of all, there is just a single gadget that we by
and by have in our pockets constantly. Truly, it is our cell phones. Presently
Mobile telephones have turned out to be more than a gadget utilized for
correspondence, they are our cameras, they are our maps, they are our shopping
karts and so forth?

With this capacity in our grasp, it is extremely an exhausting plan to utilize


Remote controls to control any electronic applications in our home like TV, AC,
Home theatre and so on. It's continually disappointing to go after the AC's
Remote from the comfortable solace of our Bed or couch. Consequently in this
venture we will develop a little set utilizing which you can control the Air
conditioner through your Smart telephone utilizing Bluetooth and Arduino.
Sounds intriguing right! How about we constructed one.
Materials Required:

1. Arduino Mega 2560


2. TSOP (HS0038)
3. IR Led
4. Any Colour LED and 1K Resistor(optional) 5. HC-06
6. Breadboard
7. Connecting Wires

Working Methodology:

All the Remote Controls in our home that we use to control TV, Home Theatre,
AC etc work with the help of IR Blasters. An IR blaster is nothing but an IR
LED which could blaster a signal by repetitive pulsing; this signal will be read
by the receiver in the electronics appliance. For each different button on the
remote a unique signal will be blasted which after read by the receiver is used to
perform a particular pre-defined task. If we are able to read this signal coming
out from the Remote, we can then mimic the same signal using an IR LED when
ever required to perform that particular task. We have previously made a IR
Blaster circuit for Universal IR Remote and for Automatic AC Temperature
control.

A TSOP is an IR Receiver that could be used to decode the signal coming from
the Remotes. We will use this TSOP to decode all the information from our
Remote and store it on Arduino. Then using that information and an IR Led we
can re-create the IR signals from our Arduino whenever required.

Circuit Diagram and


Explanation:
The following table can also be used to verify your connections. S.No:
Component Pin Arduino Pin
1 TSOP – Vcc 5V
2 TSOP – Gnd Gnd
3 TSOP - Signal 8
4 IR Led – Cathode Gnd
5 IR Led – Anode 9
6 HC-05 – Vcc 5V
7 HC05 – Gnd Ground
8 HC05 – Tx 10
9 HC05 – Rx 11
Code:
/*
*Bluetooth AC Temperature control using Arduino and TSOP S.No:Component
Pin Arduino Pin 1TSOP – Vcc 5V 2TSOP – Gnd Gnd

3TSOP - Signal
4IRLed – Cathode Gnd
5IRLed – Anode 9
6HC-05 - Vcc 5V
7HC05 – Gnd Ground
8HC05 – Tx 10
9 HC05 – Rx 11
*/
#include <IRremote.h> //Lib for IT Blaster and TSOP #include
<SoftwareSerial.h>// import the serial library
SoftwareSerial BT_module(10, 11); // RX, TX IRsend irsend;
int khz = 38; // 38kHz carrier frequency for the NEC protocol char
BluetoothData; // the data read by Bluetooth Module int PevData;
//Decoded Remote Signals For my AC ##CHANGE IT FOR YOUR REMOTE
unsigned int ACoff[] = {2950,1750, 400,1100, 450,1050, 450,400, 400,400,
400,400, 450,1100, 400,400, 400,400, 450,1100, 400,1100, 450,350, 450,1100,
400,400, 400,400, 450,1100, 400,1100, 450,400, 400,1100, 400,1100, 450,400,
400,400, 400,1100, 450,350, 450,400, 400,1100, 450,400, 400,400, 400,400,
450,350, 450,350, 450,400, 400,400, 450,350, 450,400, 400,400, 400,400,
450,350, 450,400, 400,400, 400,400, 450,400, 400,400, 400,400, 450,350,
450,350, 450,1100, 400,400, 450,400, 400,1100, 450,1050, 450,400, 400,400,
400,400, 450,350, 450,400, 400,400, 450,350, 450,400, 400,400, 400,1100,
450,350, 450,400, 400,400, 400,400, 450,400, 400,1100, 450,350, 450,400,
400,400, 400,400, 400,1100, 450,400, 400,400, 450,350, 450,400, 400,400,
400,400, 450,350, 450,350, 450,400, 400,400, 450,350, 450,400, 400,400,
400,400, 450,350, 450,400, 400,400, 450,350, 450,400, 400,400, 400,400,
450,350, 450,350, 450,400, 450,350, 450,350, 450,400, 450,350, 450,350,
450,350, 450,400, 450,350, 450,350, 450,400, 400,1100, 450,350, 450,350,
450,400, 450,350, 450,350, 450,1100, 450}; unsigned int ACon[] = {2950,1700,
450,1100, 400,1100, 450,350, 450,350, 450,400, 450,1050, 450,350, 450,400,
450,1050, 450,1100, 400,400, 450,1050, 450,350, 450,400, 400,1100, 450,1100,
450,350, 450,1050, 450,1100, 450,350, 450,350, 450,1100, 450,350, 400,400,
450,1100, 450,350, 450,350, 450,400, 400,400, 450,350, 450,350, 450,400,
400,400, 450,350, 450,350, 450,400, 400,400, 450,350, 450,350, 450,400,
450,350, 450,350, 450,1100, 400,400, 450,350, 450,1100, 400,400, 450,350,
450,1100, 400,1100, 450,350, 450,400,
400,400, 450,350, 450,350, 450,400, 400,400, 450,350, 450,1100,
450,350, 400,400, 450,350, 450,400, 450,350, 400,400, 450,400,
450,350, 450,350, 450,350, 450,400, 400,400, 450,350, 450,400,
400,400, 400,400, 400,400, 450,350, 450,400, 450,350, 450,350,
450,400, 450,350, 450,350, 450,350, 450,400, 400,400, 400,400,
450,350, 450,400, 450,350, 400,400, 450,350, 450,400, 450,350,
450,350, 450,350, 450,400, 450,350, 450,1100, 400,400, 400,400,
450,350, 450,350, 450,1100, 400,400, 450}; unsigned int Temp23[] =
{3000,1650, 550,950, 550,1000, 500,300,
550,250, 550,250, 550,1000, 500,300, 550,300, 500,1000, 550,950,
550,300, 550,950, 550,250, 550,300, 500,1000, 500,1050, 500,300,
500,1000, 550,1000, 500,300, 500,300, 550,1000, 450,350, 500,300,
500,1050, 450,350, 450,350, 450,350, 450,400, 450,350, 450,350,
450,400, 400,400, 450,350, 450,350, 450,350, 450,400, 400,400,
400,400, 450,400, 400,400, 400,400, 450,1100, 400,400, 400,400,
450,1050, 450,400, 400,400, 450,1100, 400,1100, 400,400, 450,350,
450,400, 400,400, 400,400, 450,400, 400,400, 400,400, 450,350,
450,1100, 400,400, 400,400, 450,350, 450,400, 400,400, 450,1100,
400,400, 400,1100, 450,1100, 400,1100, 450,350, 450,400, 400,400,
450,350, 450,350, 450,400, 400,400, 400,400, 450,350, 450,400,
400,400, 450,350, 450,400, 400,400, 400,400, 450,350, 450,400,
400,400, 450,350, 450,350, 450,400, 450,350, 400,400, 450,350,
450,400, 450,350, 450,350, 450,400, 450,350, 450,350, 450,350,
450,400, 400,400, 400,400, 450,350, 450,1100, 400,1100, 450,1100,
400,1100, 450,1100, 400,1100, 400,400, 450}; unsigned int Temp24[] =
{3000,1650, 500,1050, 500,1000, 500,300,
500,300, 500,350, 500,1000, 500,300, 500,350, 500,1000, 500,1050,
500,300, 500,1000, 500,300, 500,350, 500,1000, 500,1050, 500,300,
500,1000, 500,1050, 500,300, 500,300, 500,1050, 500,300, 500,300,
500,1050, 500,300, 500,300, 500,350, 500,300, 500,300, 500,300,
500,350, 500,300, 500,300, 500,300, 500,350, 500,300, 500,300,
500,300, 500,350, 500,300, 500,300, 500,1050, 500,300, 500,300,
500,1050, 500,300, 500,300, 500,1050, 500,1000, 500,300, 500,350,
500,300, 500,300, 500,300, 500,350, 500,1000, 500,1050, 500,1000,
500,300, 500,350, 450,350, 500,300, 500,300, 500,350, 500,1000, 500,300,
500,300, 500,300, 500,300, 500,350, 500,300, 450,350, 500,350, 450,350,
450,350, 450,350, 450,400, 400,400, 400,400, 450,400, 400,400, 400,400,
400,400, 450,350, 450,400, 400,400, 450,350, 450,400, 450,350, 450,350,
450,350, 450,400, 450,350, 450,350, 450,350, 500,350, 450,1050, 500,300,
500,1050, 500,1000, 500,1050, 500,1000, 500,1000, 500,350, 550}; unsigned int
Temp25[] = {3050,1650, 500,1000, 550,950, 550,300, 500,300, 500,300,
550,1000, 500,300, 500,300, 550,1000, 550,950, 550,250, 550,1000, 500,300,
550,250, 550,1000, 500,1000, 550,300, 550,950, 550,950, 550,300, 500,300,
500,1000, 550,250, 550,300, 550,950, 550,300, 500,300, 500,300, 550,250,
550,300, 500,300, 550,250, 550,250, 600,250, 500,300, 550,250, 550,250,
550,300, 550,250, 500,300, 550,300, 500,300, 500,1000, 550,250, 550,300,
500,1000, 550,250, 550,300, 500,1000, 550,1000, 500,300, 500,300, 550,250,
550,300, 500,300, 500,300, 550,300, 500,1000, 550,950, 550,300, 500,300,
500,300, 550,250, 550,300, 500,300, 550,950, 550,300, 500,1000, 550,1000,
500,1000, 500,300, 550,300, 500,300, 500,300, 550,250, 550,300, 500,300,
500,300, 550,250, 550,300, 500,300, 550,250, 550,250, 550,300, 500,300,
500,300, 550,250, 550,300, 500,300, 550,250, 550,300, 500,300, 500,300,
550,250, 550,250, 550,300, 550,250, 550,250, 550,300, 500,300, 500,300,
550,250, 550,300, 500,300, 500,300, 500,350, 500,1000, 500,1000, 500,1050,
500,1000, 500,1050, 500,300, 550}; unsigned int Temp26[] = {3000,1650,
500,1000, 500,1050, 500,300, 500,300, 500,350, 500,1000, 500,300, 500,350,
500,1000, 500,1050, 450,350, 500,1000, 500,300, 500,350, 500,1000, 500,1050,
500,300, 500,1000, 500,1050, 500,300, 500,300, 500,1050, 500,300, 500,300,
500,1050, 500,300, 500,300, 500,300, 500,350, 500,300, 500,300, 500,350,
500,300, 500,300, 500,300, 500,350, 500,300, 500,300, 500,300, 500,350,
500,300, 500,300, 500,1050, 500,300, 500,300, 500,1050, 450,350, 500,300,
500,1050, 500,1000, 500,300, 500,350, 500,300, 500,300, 500,300, 500,350,
500,1000, 500,300, 500,1050, 500,300, 500,300, 500,300, 500,350, 500,300,
500,300, 500,1050, 500,300, 500,1050, 450,1050, 500,1000, 500,350, 500,300,
500,300, 500,350, 450,350, 500,300, 500,300, 500,300, 500,350, 500,300,
500,300, 500,350, 450,350, 500,300, 500,300, 500,300, 500,350,
500,300, 500,300, 500,350, 500,300, 500,300, 500,300, 500,350,
500,300, 500,300, 500,350, 450,1050, 500,1000, 500,350, 500,1000,
500,1000, 500,1050, 500,1000, 500,350, 500}; unsigned int Temp27[] =
{3050,1600, 550,1000, 500,1000, 550,300,
500,300, 550,250, 550,1000, 500,300, 550,300, 500,1000, 550,1000,
500,300, 550,1000, 550,250, 500,300, 550,1000, 500,1050, 500,300,
500,1000, 550,1000, 500,300, 550,250, 550,1000, 550,250, 550,300,
500,1000, 550,300, 500,300, 550,250, 550,300, 500,300, 500,300,
550,300, 500,300, 550,250, 550,300, 500,300, 500,300, 550,300,
500,300, 500,300, 550,300, 500,300, 500,1000, 550,300, 500,300,
550,1000, 500,300, 500,300, 550,1000, 550,1000, 500,300, 500,300,
550,250, 550,300, 500,300, 550,300, 500,300, 500,300, 550,1000,
500,300, 550,250, 550,300, 500,300, 500,300, 500,350, 500,300,
550,250, 550,1000, 500,1000, 550,1000, 500,300, 550,300, 500,300,
550,250, 550,300, 500,300, 500,300, 550,300, 500,300, 500,300,
550,300, 500,300, 550,250, 550,300, 500,300, 500,300, 500,300,
550,300, 550,250, 550,300, 500,300, 500,300, 550,300, 500,300,
500,300, 550,300, 500,300, 500,300, 550,300, 500,300, 500,300,
500,300, 500,350, 500,300, 500,350, 500,300, 500,300, 500,1050,
500,1000, 500,1050, 500,1000, 500,350, 500}; // PANASONIC C4D3:64800024
unsigned int Temp28[] = {3100,1600, 550,950, 550,1000, 550,250,
550,250, 550,250, 550,1000, 500,300, 500,300, 550,1000, 500,1000,
550,250, 550,1000, 500,300, 550,250, 550,1000, 550,950, 550,300,
500,1000, 550,950, 550,300, 550,250, 500,1000, 550,300, 500,300,
550,950, 550,300, 500,300, 500,300, 550,250, 550,300, 550,250,
500,300, 550,300, 500,300, 500,300, 550,250, 550,250, 600,250,
500,300, 500,300, 550,300, 500,300, 500,1000, 550,300, 500,300,
500,1000, 550,250, 550,300, 500,1000, 550,1000, 550,250, 550,250,
550,250, 550,300, 500,300, 550,250, 550,1000, 500,1000, 550,250,
550,300, 500,300, 500,300, 550,250, 550,300, 500,300, 550,1000,
500,300, 500,1000, 550,1000, 500,1000, 550,250, 550,300, 500,300,
550,250, 550,250, 550,300, 500,300, 500,300, 550,250, 550,300,
500,300, 550,250, 550,300, 550,250, 500,300, 550,250, 550,250, 550,300,
500,300, 550,250, 550,300, 500,300, 500,300, 550,250, 550,250, 550,300,
500,300, 550,1000, 500,300, 500,300, 550,950, 550,1000, 500,1000, 550,1000,
500,300, 550}; unsigned int Temp29[] = {3100,1550, 600,950, 500,1000,
550,300, 500,300, 500,300, 550,950, 550,300, 550,250, 550,1000, 500,1000,
550,250, 550,1000, 500,300, 550,250, 550,950, 600,950, 550,250, 550,1000,
500,1000, 550,250, 600,250, 550,950, 550,250, 550,300, 550,950, 550,250,
550,300, 550,250, 550,250, 550,250, 550,300, 550,250, 550,250, 550,300,
500,300, 550,250, 550,250, 550,300, 500,300, 550,250, 550,250, 600,250,
550,950, 550,250, 550,300, 500,1000, 550,250, 550,300, 550,950, 550,1000,
500,300, 500,300, 550,250, 550,250, 550,300, 500,300, 550,250, 550,1000,
500,300, 550,250, 550,300, 500,300, 550,250, 550,250, 550,300, 500,1000,
550,250, 550,1000, 500,1000, 550,1000, 500,300, 500,300, 550,300, 500,300,
500,300, 550,250, 550,250, 550,300, 500,300, 500,300, 550,300, 500,300,
500,300, 550,250, 550,300, 500,300, 500,300, 550,300, 500,300, 500,300,
550,250, 550,250, 550,300, 500,300, 500,300, 550,300, 500,300, 500,300,
550,250, 550,300, 500,300, 500,300, 550,250, 550,300, 500,300, 550,250,
550,250, 550,1000, 500,1000, 550,1000, 500,1000, 550,300, 500}; unsigned int
Temp30[] = {3000,1650, 500,1000, 550,1000, 500,300, 500,300, 550,250,
550,1000, 500,300, 500,300, 550,1000, 550,950, 550,250, 550,1000, 550,250,
550,250, 550,1000, 550,950, 550,300, 500,1000, 550,950, 550,300, 500,300,
550,950, 550,300, 550,250, 550,1000, 500,300, 500,300, 550,250, 550,250,
550,300, 500,300, 550,250, 550,300, 500,300, 500,300, 550,250, 550,300,
500,300, 500,300, 550,300, 500,300, 500,300, 550,950, 550,300, 500,300,
500,1000, 550,250, 550,300, 550,950, 550,1000, 500,300, 550,250, 550,250,
600,250, 500,300, 550,250, 550,1000, 500,300, 550,250, 550,300, 500,300,
500,300, 550,250, 550,300, 500,300, 550,950, 550,300, 500,1000, 550,950,
550,1000, 500,300, 550,300, 500,300, 500,300, 550,250, 550,300, 500,300,
500,300, 550,250, 550,300, 500,300, 500,300, 550,250, 550,300, 500,300,
500,300, 550,250, 550,300, 500,300, 550,250, 550,300, 500,300, 500,300,
550,250, 550,250, 550,300, 500,300, 550,250, 550,300, 500,300, 500,300,
500,1000, 500,1000, 500,1050, 500,300, 500}; //Change it for your remote

void setup() {
BT_module.begin(9600); // BT works on 9600 Serial.begin(57600); // Serial
Monitor works on 57600

Void loop() {
While(BT_module.read()) // If data is coming {
BluetoothData=BT_module.read(); // read it and save it
Serial.println(BluetoothData); // print it on serial for testing purpose
}
if (BluetoothData != PevData)
{
if ( BluetoothData==’0’ )
{
irsend.sendRaw(ACon, sizeof (ACon) / sizeof(ACon[0], khz); delay(2000);
//Send signal to Turn On the AC
}
if ( BluetoothData==’1’ )
{
irsend.sendRaw(ACoff, sizeof (ACoff) / sizeof(ACoff[0], khz); delay(2000);
//Send signal to Turn Off the AC }
if(BluetoothData=='2')
{
irsend.sendRaw(Temp23,sizeof(Temp23)/sizeof(Temp23[0]),khz) ;
delay(2000);//Send signal to set Temperatue 23C if(BluetoothData=='3') {
irsend.sendRaw(Temp24,sizeof(Temp24)/sizeof(Temp24[0]),khz
);
delay(2000);//Send signal to set Temperatue 24C }
if(BluetoothData=='4') {
irsend.sendRaw(Temp25,sizeof(Temp25)/sizeof(Temp25[0]),kh
z);
delay(2000);//Send signal to set Temperatue 25C }
if(BluetoothData=='5')
{
irsend.sendRaw(Temp26,sizeof(Temp23)/sizeof(Temp26[0]),kh
z);
delay(2000);//Send signal to set Temperatue 26C }
if(BluetoothData=='6') {
irsend.sendRaw(Temp27,sizeof(Temp27)/sizeof(Temp27[0]),kh
z);
delay(2000);//Send signal to set Temperatue 27C }
if(BluetoothData=='7') {
irsend.sendRaw(Temp28,sizeof(Temp28)/sizeof(Temp28[0]),kh
z);
delay(2000);//Send signal to set Temperatue 28C }
if(BluetoothData=='8') {
irsend.sendRaw(Temp29,sizeof(Temp29)/sizeof(Temp29[0]),kh
z);
delay(2000);//Send signal to set Temperatue 29C if(BluetoothData=='9') {
irsend.sendRaw(Temp30,sizeof(Temp30)/sizeof(Temp30[0]),k hz);
delay(2000);//Send signal to set Temperatue 30C
}
}
PevData = BluetoothData;
delay(100);//prepare for next data ...

2.5.2 The Traffic Light Controller


Here is a traffic light system based on Arduino that can be used in a 3-way ond
4-way intersections. It has an additional blinking orange led that is used to signal
when the pedestrians can cross the street.

The code is written based on 4 situations:

1. the first traffic light (TF1) has the red signal and the pedestrians ON; the
second one (TF2) has green ON.
2. TF1 has red and yellow ON; TF2 has yellow ON
3. TF1 is green now and TF2 is red and pedestrians ON
4. TF1 is yellow and TF2 is red and yellow

Components Required :  Red, yellow and green LEDs.  A breadboard.


 6 x 220k resistors.
 Connecting wires.
 1 x pushbutton switch.
 1 x 10k resistor.

I. A PEDESTRIAN CROSSING Circuit Diagram:


A switch either lets the current flow, or doesn’t. This seems simple enough, but
in a logic circuit, the current should be always flowing in either a high or low
state (remember– 1 or 0, high or low). You might assume that a pushbutton
switch that isn’t actually being pushed would be defined as being in a low state,
but in fact it’s said to be ‘floating’, because no current is being drawn at all.

In this floating state, it’s possible that a false reading will occur as it fluctuates
with electrical interference. In other words, a floating switch is giving neither a
reliable high, nor low state reading. A pull down resistor keeps a small amount
of current flowing when the switch is closed, thereby ensuring an accurate low
state reading. In other logic circuits, you may find a pull-up resistor instead –
this works on the same principle, but in reverse, making sure that particular logic
gate defaults to high.

Now, in the loop part of the code, instead of changing the lights every 15
seconds, we’re going to read the state of the pushbutton switch instead, and only
change the lights when it’s activated.

Code:
Start by adding a new variable to the start of the program:

int button = 12; // switch is on pin 12 pinMode(button, INPUT);


digitalWrite(green, HIGH);
void loop() {

if (digitalRead(button) == HIGH){
delay(15); // software debounce
if (digitalRead(button) == HIGH) {

// if the switch is HIGH, ie. pushed down - change the lights!


changeLights();
delay(15000); // wait for 15 seconds }
}
}
II. A JUNCTION
Circuit Diagram:

Code:

First, assign your new traffic light pins to variables, and configure them as
outputs, just like in the first example:
// light one
int red1 = 10;
int yellow1 = 9;
int green1 = 8;

// light two
int red2 = 13;
int yellow2 = 12;
int green2 = 11;

void setup(){
// light one
pinMode(red1, OUTPUT);
pinMode(yellow1, OUTPUT);
pinMode(green1, OUTPUT);

// light two
pinMode(red2, OUTPUT);
pinMode(yellow2, OUTPUT);
pinMode(green2, OUTPUT);
}
Now, update your loop to use the code from the first example (instead

of the pedestrian crossing):


void loop(){
changeLights();
delay(15000);

Once again, all the work is being carried out in the changeLights() function.
Rather than going red > red & yellow > green, this code will alternate the traffic
lights. When one is on green, the other will be on red. Here’s the code:

void changeLights(){
// turn both yellows on
digitalWrite(green1, LOW);
digitalWrite(yellow1, HIGH);
digitalWrite(yellow2, HIGH);
delay(5000);

// turn both yellows off, and opposite green and red


digitalWrite(yellow1, LOW);
digitalWrite(red1, HIGH);
digitalWrite(yellow2, LOW);
digitalWrite(red2, LOW);
digitalWrite(green2, HIGH);
delay(5000);

// both yellows on again digitalWrite(yellow1, HIGH); digitalWrite(yellow2,


HIGH); digitalWrite(green2, LOW); delay(3000);

// turn both yellows off, and opposite green and red


digitalWrite(green1, HIGH); digitalWrite(yellow1, LOW); digitalWrite(red1,
LOW); digitalWrite(yellow2, LOW); digitalWrite(red2, HIGH); delay(5000);

}
CHAPTER 3
INTERFACING ARDUINO WITH MATLAB
3.1 INTRODUCTION:

MATLAB and Simulink let you assemble Arduino ventures utilizing abnormal
state programming and square outlines.
MATLAB Support Package for Arduino gives you a chance to impart over USB
to your Arduino and associated gadgets, for example, Adafruit engine shield,
I2C, and SPI gadgets. Since MATLAB is an abnormal state deciphered dialect
you can get comes about because of I/O directions promptly without
aggregating. MATLAB incorporates worked in math, designing, and plotting
capacities that you can use to break down and imagine information from your
Arduino. The natural dialect and capacity to effortlessly plot sensor information
attract numerous Arduino clients to MATLAB.
Simulink Support Package for Arduino gives you a chance to create calculations
that run independent on your Arduino. For those of you not comfortable with
Simulink, it is a square outline condition for displaying dynamic frameworks and
creating calculations. The help bundle broadens Simulink with obstructs for
designing Arduino sensors and perusing and composing information from them.
Subsequent to making your Simulink show, you can reproduce it, tune
calculation parameters, and download the finished calculation for independent
execution on the gadget. One especially helpful (and remarkable) capacity
offered by Simulink is the capacity to tune parameters live from your PC while
the calculation keeps running on the equipment.

3.1.1 WHY MATLAB IS USEFUL FOR PROTOTYPING AI


PROJECTS ?

Large toolbox of numeric/image library functions. Very useful for


displaying, visualizing data.

High-level: focus on algorithm structure, not on lowlevel Details.


Allows quick prototype development of algorithms. Some other aspects of
Matlab
Mat lab is an interpreter -> not as fast as compiled code. Typically quite fast
for an interpreted language.

Often used early in development -> can then convert to C (e.g.,) for speed.
Can be linked to C/C++, JAVA, SQL, etc.
Many algorithms and toolboxes freely available.

3.1.2 MATLAB code example:

Instantiate an Arduino object from MATLAB using: >> a = arduino('COM5');


Connect to and control Arduino inputs and outputs: >> av = readVoltage(a,5);
>> writeDigitalPin(a,13,1);
>> delete(a)
Control the position of an Adafruit motor shield V2:
>>a=arduino('com5','Uno','libraries','Adafruit\MotorshieldV2'); >> shield =
addon(a,'Adafruit\MotorshieldV2')
>> s = servo(shield,1);
>> writePosition(s,0.5)
>> dcm = dcmotor(shield,1);
>> start(dcm)
>> pause(4)
>> stop(dcm)
>> sm = stepper(shield,1,200,'RPM',10)
>> move(sm,10)

3.2 SETTING UP ARDUINO DEVELOPMENT FOR MATLAB:

We're going to create simple MATLAB m-files to talk to our Arduino board. The
MATLAB Support Packages for Arduino Hardware lets you use MATLAB to
communicate with your board through USB cable. The support packages are
available for 32-bit and 64-bit Windows, 64-bit Mac OS and 64-bit Linux.

STEP 1: Run MATLAB application. Click Get Hardware Support Packages


on Add-Ons icon on toolbox.
STEP 2:
Then, you get a dialog. Select Install from Internet. If done, click Next> button

STEP 3: Select
Arduino on Support for. You should see two Arduino support packages. You can
select both. If done, click Next> button.
STEP 4: You will be asked to log on with your MATLAB account. You should
have MATLAB license. Click Log In button.

STEP
5: You will get the authentication dialog. Fill your account. After that, click Log
In button.
STEP 6: If successful, you should get a
software license agreement. Click on I accept and then click Next> button.

STEP 7: You will get


confirmation. Click Next> button
STEP
8: Click Install button to start installation.
STEP
9: After done, you will be asked to configure Arduino board. Select Arduino and
then, click Next> button.
STEP
10: Confirmation will be shown. Click Continue> button.

STEP 11: The program will check if your platform needs Arduino driver or not.
If you’re working on Linux and Mac, you don’t need a driver. You need to install
Arduino driver if you’re working on Windows platform. Click Next> button if
done.

STEP 12: Installation is over. Click Finish button to close installation.

3.3
CONNECTING ARDUINO BOARD TO COMPUTER:

Once the bundles are introduced, interface your Arduino board to your PC and
sort the accompanying charge in MATLAB order window,
>> a = arduino()
In the event that you have more than one Arduino associated with your PC, you
need to indicate the board sort you will communicate with:
>> a = arduino('com3', 'uno')
MATLAB will then endeavour to communicate with your board. On the off
chance that fruitful, MATLAB will show the properties of the Arduino board
associated with your PC as demonstrated as

This data shows the port on which your board is associated, the model of your
Arduino board, and accessible pins and libraries accessible for the board.
In your workspace, you will see a variable a, which is the MATLAB Arduino
question. Run the accompanying order to clear this protest. >> clear a
This order will expel the protest ‘a’ from your workspace.

3.4 BLINKING LED:

In this segment, we assemble a flickering LED program utilizing MATLAB.


Arduino Uno/Mega/Leonardo sheets gives installed LED which is associated on
stick 13.
How about we begin to compose our Blink program.

Right off the bat, you set working envelope on MATLAB. You can transform it
on MATLAB IDE, see a red bolt.
Then, click New Script icon to create a new script file.

After that, you can get a script


editor, shown in Figure below.

3.4.1 CODE:

board = arduino();
led = 'D13';
for k=1:10

disp ('turn on LED');


writeDigitalPin(board,led,1); pause(1);
disp('turn off LED');
writeDigitalPin(board,led,0); pause(1);

end
disp('close Arduino board'); clear board;
3.4.2 OUTPUT:

3.5 WORKING WITH DIGITAL I/O:

MATLAB bolster for Arduino board gives three capacities which we can use on
computerized I/O preparing. The accompanying is the capacities:

configurePin() is utilized to characterize stick mode either as info or yield.


writeDigitalPin() is utilized to compose computerized information into a
particular advanced stick.
readDigitalPin() is utilized to peruse advanced contribution on particular
computerized stick.
To show how to function with advanced I/O, we manufacture a basic program by
using LED and pushbutton.

3.5.1 CONNECTION DIAGRAM:


The following is hardware wiring:  LED is connected to
Arduino digital pin 9.

 Pushbutton is
connected to Arduino
digital pin 8.

3.5.2 CODE:

function [] = led_pushbutton()
pushbutton = 'D8';
led = 'D9';
board=arduino();
finishup = onCleanup(@() exitprogram(board));
configurePin(board,pushbutton,'DigitalInput');disp('press Ctr-C to exit');
while 1
state = readDigitalPin(board,pushbutton);
writeDigitalPin(board,led,state);
disp(state);
pause(0.5);
end
end
function exitprogram(b) clear b;
disp('program has exit'); end

We use onCleanup(), to catch CTRL+C for quiting the program. Save this
program into file, called led pushbutton.m.
Run the program and press puchbutton. Then, you should see lighting LED.

3.5.2 OUTPUT:

3.6 WORKING WITH PWM AND ANALOG INPUT:

MATLAB bolster for Arduino board gives five capacities which we can use on
simple I/O preparing. The accompanying is the capacities:

configurePin() is utilized to characterize stick mode either as info or yield.


writePWMVoltage() is utilized to compose PWM voltage on

advanced stick.
writePWMDutyCycle() is utilized to set PWM obligation cycle
on computerized stick.
readVoltage() to peruse simple contribution on Analog stick. In this section, we
endeavor to get to Arduino Analog I/O
utilizing MATLAB. There are three situations for our cases:  Controlling RGB
LED
 Controlling LED shine
 Perusing Analog information utilizing Potentiometer

 3.6.1 ANALOG OUTPUT (PWM) / RGB LED:

 In this situation we fabricate a program to control RGB LED shading


utilizing Arduino Analog yield (PWM). If you don't mind be watchful on the off
chance that you need to work with Arduino PWM. On the off chance that you
have Arduino Mega, you will see PWM name so you get PWM sticks effectively
however in the event that you have Arduino Uno, it composes DIGITAL (PWM
~). It implies your PWM pins can be found on DIGITAL pins which stick with

~, for example, ~3,~5,~6,~9, ~10, ~11.


 For Arduino Uno R3, you can see PWM sticks on picture underneath (see red
bolt).

3.6.1.1 CONNECTION DIAGRAM:

Firstly we implement RGB LED hardware. The following is a hardware schema.


Pin 1: Red
Pin 2: Common Pin
Pin 3: Green
Pin 4: Blue
1) RGB LED pin 1 (red) is connected to Arduino PWM pin 3 2) RGB LED pin 2
is connected to Arduino VCC 5V 3) RGB LED pin 3 (green) is connected to
Arduino PWM pin 5 4) RGB LED pin 4 (blue) is connected to Arduino PWM
pin 6
3.6.1.2 CODE:

function [] = led_rgb()
board = arduino();

finishup=onCleanup(@()exitprogram(board));
configurePin(board,'D3','PWM');
configurePin(board,'D5','PWM');
configurePin(board,'D6','PWM');
disp('press Ctr-C to exit');
while 1

disp ('red');
write_rgb(board,0,1,1); % red pause(1);
disp('green');
write_rgb(board,1,0,1); % green pause(1);
disp('blue');
write_rgb(board,1,1,0); % blue pause(1);
disp('yellow');
write_rgb(board,0,0,1); % yellow pause(1);
disp('purple');
write_rgb(board,0.3,1,0.3);%purple pause(1);
disp('aqua');
write_rgb(board,1,0,0); % aqua pause(1);

end
end

% testing for Arduino Uno


function write_rgb(board,r,g,b) writePWMDutyCycle(board,'D3',r);
writePWMDutyCycle(board,'D5',g); writePWMDutyCycle(board,'D6',b);

end

function exitprogram(b)
clear b;
disp('program has exit');
end

Save this program as led_rgb.m.This program will generate six colors: red,
green, blue, yellow, purple, and aqua.

3.6.1.3 OUTPUT:
3.6.2 ANALOG OUTPUT VOLTAGE / LED BRIGHTNESS:

In this demo, we attempt to control LED shine by controlling voltage on LED.


MATLAB for Arduino bolster gives writePWMVoltage() capacity to set voltage
on PWM pins. We can set a voltage esteem from 0 to 5 for Arduino Uno/Mega
and 0 - 3.3 for Arduino Due.

3.6.2.1 CONNECTION DIAGRAM:

We connect a LED on PWM pin on digital pin 3.

3.6.2.2 CODE:

function [] = led_brightness()
board = arduino();
finishup=onCleanup(@()exitprogram(board));
configurePin(board,'D3','PWM');
disp('press Ctr-C to exit'); while 1

for k = 0:5
writePWMVoltage(board,'D3',k);
pause(1);
end
for k = 5:-1:0
writePWMVoltage(board,'D3',k);
pause(1);
end
end
end
function exitprogram(b)
clear b;
disp('program has exit');
end

Save these scripts into a file, called led_brightness.m. This program will set
voltage value on PWM pin on digital pin 3 from 0 to 5 and then set a value from
5 to 0 too.

3.6.2.3 OUTPUT:

You can run the program by typing this command. >> led_brightness
On hardware side, you should see LED brightness changing gradually.
3.6.3 ANALOG INPUT / WORKING WITH
POTENTIOMETER:

In this area, we figure out how to peruse simple contribution on Arduino board.
For outline, I utilize Potentiometer as simple info source. Our situation is to
peruse simple incentive from

Potentiometer. Then, display it on console.

3.6.3.1 CONNECTION DIAGRAM:

To understand Potentiometer, you see its scheme in Figure below.

You can connect VCC to Arduino board on VCC +5V pin. Vout to Arduino
board Analog input A0. In addition, GND to Arduino board GND. The
following is hardware implementation. Use slide potentiometer.
3.6.3.2 CODE:

function [] = potentiometer()
board = arduino();
finishup=onCleanup(@()exitprogram(board));

disp ('press Ctr-C to exit');


while 1
analog = readVoltage(board,'A0');
disp(['analog= ',num2str(analog)]);
pause(1);
end
end
function exitprogram(b)
clear b;
disp('program has exit');
end

Save this code as potentiometer.m.

3.6.3.3 OUTPUT:

To run the program, you can type this command. >> potentiometer
You should see analog value on Command Window.

3.7 WORKING WITH SERVO MOTOR:

Servo engine gives a pole development 360 degree. We can control this
development in view of its degree. In this situation, you can utilize any DC
engine (servo) that will be associated with Arduino. I utilized a little servo from
Arduino Sidekick Basic unit. The accompanying is a photo of my smaller than
expected servo engine.

3.7.1 CONNECTION DIAGRAM:

To build hardware implementation, you can connect servo motor to Arduino by


following configuration.
1) Arduino UnRed cable is be connected to 5V
2) Black or brown cable is be connected to GND
3) The rest (yellow or orange cable) is be connected to Arduino PWM pin. I used
pin 10 for Arduino Uno R3
3.7.2 CODE:

function [] = servo_motor()
board = arduino();
finishup=onCleanup(@()exitprogram(board)); motor= servo(board,'D10');

disp ('press Ctr-C to exit');


while 1
for pos = 0:0.25:1
disp(['position: ',num2str(pos)]);
writePosition(motor,pos);
pause(1);
end
for pos = 1:0.25:0
disp(['position: ',num2str(pos)]);
writePosition(motor,pos);
pause(1);
end
end
end
function exitprogram(b)
clear b;
disp('program has exit');
end

Save these script into a file, servo_motor.m.

3.7.2 OUTPUT:

You should see servo motor is running from degree 0 to 180 and then back again
from degree 180 to 0.
CHAPTER 4
DATA ACQUISITION SYSTEM (DAS)
4.1 INTRODUCTION:

Data acquisition is the way toward inspecting signals that measure genuine
physical conditions and changing over the subsequent specimens into advanced
numeric esteems that can be controlled by a PC. Information securing
frameworks, truncated by the acronyms DAS or DAQ, commonly change over
simple waveforms into advanced esteems for preparing. The parts of information
obtaining frameworks include:

 Sensors, to change over physical parameters to electrical signs.

 Flag conditioned hardware, to change over sensor signals into a frame that
can be changed over to computerized values.
 Simple to-computerized converters, to change over conditioned sensor signs
to advanced esteems.

Information procurement applications are typically controlled by programming


programs created utilizing different universally useful programming dialects, for
example, Assembly, BASIC, C, C++, C#, Fortran, Java, LabVIEW, Lisp, Pascal,
and so forth. Remain solitary information procurement frameworks are
frequently called information lumberjacks.

There are likewise open-source programming bundles giving all the vital
instruments to procure information from various equipment hardware. These
instruments originate from mainstream researchers where complex analysis
requires quick, adaptable and versatile programming. Those bundles are
typically custom fit however more broad DAQ bundle like the Maximum
Integrated Data Acquisition System can be effortlessly custom-made and is
utilized as a part of a few material science tests around the world.

4.2 DIGITAL DATA ACQUISITION SYSTEM:


Data acquisition systems (abbreviated with the acronym DAS or DAQ) typically
convert analog waveforms into digital values for easy processing.
The components of data acquisition systems include:

 Sensors that convert physical parameters to electrical signals.

 Signal conditioning circuitry to convert sensor signals into a form that can be
converted to digital values.
 Analog-to-digital converters, which convert conditioned sensor signals to
digital values.

Fig. 1: Digital Data Acquisition System

In general analog DASs are used for measurement systems with


wide bandwidth. But the accuracy is less. So digital DASs which
have high accuracy, low per-channel cost and narrow bandwidth
(slowly varying signal) are designed. The function of the digital
data acquisition system include handling analog signals, making the
measurement, converting and handling digital data, internal
programming and control.
Here, the transducer translates physical parameters to electrical
signals acceptable by the acquisition system. The physical parameters include
temperature, pressure, acceleration, weight,
displacement, velocity etc.
Electrical quantities such as voltage, stance, and frequency may
be measured directly. The signal conditioner includes the
supporting circuitry for the transducer. This circuit may provide
excitation power, balancing circuits and calibration elements and
an example of this is a strain-gauge bridge lance and power supply
unit The scanner or multiplexer accepts multiple analog inputs and
sequentially connects them to one measuring instrument. The
signal converter translates the analog signal to a form acceptable
by the analog to digital converter like an amp1ifier used for
amplifying low-level voltages generated by thermocoup1es or
strain gauges.
The analog to digital converter (ADC) converts the analog voltage to
its equivalent digital form. The output of the ADC may displayed
visually and is also available as voltage outputs indiscrete steps for
further processing or recording on a digital recorder. The auxiliary
section contains instruments for system programming and digital data processing
such as linearizing and limit comparison. These functions
may be performed by individual instruments or by a digital computer.
The digital recorder records digital information on punched cards,
perforated paper tape, magnetic tape, typewritten pages or a
combination of these systems. Digital recorder may be preceded by a
coupling unit that translates the digital information to the proper form for entry
into particular digital recorder selected.

4.2.1 OBJECTIVE:

 DAS must acquire the necessary data, at correct speed and at correct time.
 It must monitor the complete plant operation to maintain on line and safe
operations.
 It must be able to collect, summarise and store data for diagnosis of operation
and record purpose.
 It must be flexible and capable of being expanded for future requirements.
 It must be able to compute unit performance indices using online, real time
data.
 It must be reliable, easy to operate and must be user friendly.
4.3 PHYSICAL SYSTEM / CONDITIONS:

Physical condition that can be used as input of DAS or which


can be represented in Digital form are as under
 Temperature
 Displacement
 Pressure Level
 Light
 Electric signals
 Force  ON/OFF switch

4.3.1 TRANSDUCERS:

A transducer converts temperature, pressure, level, length,


position, etc. into voltage, current, frequency, pulses or other
signals. A transducer thus converts the physical conditions in electrical
waveform for easy signal processing.

4.3.2 SIGNAL CONDITIONING:

Signal conditioning circuits improve the quality of signals generated


by transducers before they are converted into digital signals by the
PC's data-acquisition hardware. Most common signal conditioning
functions are amplification, linearization, cold-junction
compensation, filtering, attenuation, excitation, common-mode rejection, and so
on.

4.3.3 ANALOG DIGITAL (A/D) CONVERTER:

Analog to digital (A/D) conversion changes analog voltage or current


levels into digital information. The conversion is necessary to enable the
computer to process or store the signals.

4.4 METHODOLOGY:

DAS begins with the physical property to be measured. Examples of


this include temperature, light intensity, gas pressure, fluid flow, force
etc. A sensor, which is a type of transducer converts a physical property
into a corresponding electrical signal. Signal conditioning may be
necessary if the signal from the transducer is not suitable for the DAQ
hardware being used. After signal conditioning the analog wave output is
converted into digital form using A/D converter. Once digitized, the
signal can be encoded to reduce and correct transmission errors. This whole
process is called as DATA ACQUISITION SYSTEM.

4.5 DATA ACQUISITION HARDWARE:

DAQ hardware interfaces the signal and a PC. It could be in the form
of modules that can be connected to the computer's ports or cards
connected to slots in the motherboard. Following are some hardware's:
 CAMAC - Computer Automated Measurement and Control
Industrial Ethernet
 LAN extensions for Instrumentation NIM
 PowerLab
 VME bus  VXI

4.6 DATA ACQUISITION SOFTWARE:

DAQ software is needed in order for the DAQ hardware to work


with a PC. Involves the use of a programming language, such as:
 C++, visual C++  BASIC, Visual Basic + Add-on tools (such as Visual lab
with
VTX)
 Fortran Pascal
 Ladder logic  Lab view

4.7 MERITS AND DEMERITS OF DAS:

 MERITS/ADVANTAGES:
1. Reduced data redundancy.
2. Reduced updating errors and increased consistency.
3. Greater data integrity and independence from applications
programs.
4. Improved data access to users through use of host and query
languages.
5. Improved data security.
6. Reduced data entry, storage, and retrieval costs.
7. Facilitated development of new applications program.
 DEMERITS/DISADVANTAGES:
1. Database systems are complex, difficult, and time-consuming
to design.
2. Substantial hardware and software start-up costs Damage to
database affects virtually all applications programs.
3. Extensive conversion costs in moving form a file-based system
to a database system. 4. Initial training required for all programmers and users.

4.8 SERIAL / PARALLEL COMMUNICATION:

Properties amid object creation if vital. Interface with the gadget


— Connect the serial port question the gadget utilizing the fopen
work. After the protest is associated, adjust the essential gadget
settings by designing property estimations, read information, and
compose information. Design properties — To build up the coveted
serial port question conduct, appoint qualities to properties utilizing
the set capacity or speck documentation. Compose and read
information — Write information to the gadget utilizing the fprintf
or fwrite capacity, and read information from the gadget utilizing the fgetl, fgets,
fread, fscanf, or readasync work. Detach and tidy up —
When you never again require the serial port protest, disengage it from the
gadget.

4.9 COLOUR DETECTION VIA CAMERA TO


HARDWARE:

In the below given program camera which is connected to the PC will be


accessed and RED color will be detected. This colour information will be sent to
the hardware using a function senddata. Which is a user defined one.

4.9.1 SERIAL COMMUNICATION PROGRAM CODE:

Create serial port object :


disp('BEGIN') % displays BEGIN
ser=serial('COM4'); % Select the COM port
set (ser,'BaudRate',9600); % set baudrate
set(ser,'DataBits',8); % Set databits
set(ser,'Parity','none'); % set parity
set(ser,'StopBits',1); % set stop bits
set(ser,'FlowControl','none');% set flow control
set(ser,'Terminator','CR/LF'); % set Terminator
fopen(ser); % open serial port
fprintf(ser,'%d',aa); % data what to send via serial port fclose(ser); % close serial
port
delete(ser); % delete serial port
disp('STOP'); % display ‗STOP

4.9.2 E- MAIL SEND PROGRAM CODE:

Sending E-mail via MATLAB:


Sending mail through MATLAB without the help of a browser. Below given is
the program for sending the mail through Gmail.
myaddress = '[email protected]'; %From address sender mail ID mypassword = 'password'; % Sender
password
toaddress = '[email protected]'; % Receiver mail ID
setpref('Internet','E_mail',myaddress); % set preference setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','SMTP_Username',myaddress);
setpref('Internet','SMTP_Password',mypassword);
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class',

...'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','465'); sendmail(toaddress, 'subject’, ‘Message’); %
function to send mail

4.10 SIMULINK:

Simulink is a product bundle for displaying, reenacting, and


breaking down dynamical frameworks. It underpins direct and
nonlinear frameworks, displayed in consistent time, tested time, or
a crossover of the two. Frameworks can likewise be multi-rate, i.e.,
have distinctive parts that are inspected or refreshed at various
rates.For displaying, Simulink gives a graphical UI (GUI) for
building models as piece charts, utilizing snap and-drag mouse
operations. With this interface, you can draw the models similarly
as you would with pencil and paper (or as most course books
portray them). Simulink incorporates a complete square library of sinks, sources,
straight and nonlinear segments, and connectors. You can likewise
alter and make your own squares.

Fig. 2: Simulink Library Browser


Simulink Library Browser:
· Components required for building a circuit.
· Drag and drop the components from the browser window.

·Below is a small example of how to use block in


Simulink for a simple circuit construction.
Fig. 3: Simple Simulink
Fig. 4:
Components in Simulink Library
Now drag the Sine Wave block from the Sources window to your model window
.Copy the rest of the blocks in a similar manner from their respective libraries
into the model window. You can move a block from one place in the model
window to another by dragging the block. You can move a block a short distance
by selecting the block, then pressing the arrow keys. With all the blocks copied
into the model window, the model should look something like this.

Fig. 5: Taking individual components from Simulink Library


Hold down the mouse button and move the cursor to the top input port of the
Mux block.
Notice that the line is dashed while the mouse button is down and

that the cursor shape changes to double-lined cross hairs as it approaches the
Mux block. Drawing a branch line is slightly different from drawing the line you
just drew. To weld a connection to an existing line.Finish making block
connections

Fig. 6: The block connections

Now, open the Scope block to view the simulation output. Keeping the Scope
window open, set up Simulink to run the simulation for 10 seconds. First, set the
simulation parameters by choosing Simulation Parameters from the Simulation
menu. On the dialog box that appears, notice that the Stop time is set to 10.0 (its
default value).
Fig. 7: Output Waveform
The simulation stops when it reaches the stop time specified in the Simulation
Parameters dialog box or when you choose stop from the Simulation menu. To
save this model, choose Save from the File menu and enter a filename and
location. That file contains the

description of the model.

4.11 ARDUINO WITH SIMULINK:

Steps to install Arduino package in Matlab:


1. Home—>Add-on—>Get Hardware support Packages.
2. Choose the option as internet to download the package.

3. Tick the check box of which you want to download. Click on next for option.
4.Log into your Matlab account for downloading the package.
5. After login you can find the window as shown.
6.Next an install option will come click install to install the software
7. Give finish after you download the entire package.
8.Click on examples to get this window.
9.Now let’s see from scratch how to build Simulink model, just click Simulink
library then you will be appearing with many libraries there we can see our
Simulink arduino package library.
10. In
the left there will be option to open new model, open a new model

11.In this example we are going to use digital output block ,just drag and drop on
new model, just double click on this block you will appearing with in which pin
number you are going to show output, give it as 9 click ok.

12. Go to sources library drag and drop pulse generated block, double click on
that give sample time 0.1 and click ok, because it is going to generate the pulse
every one second. We can see that by using scope which is available in sinks
library.
13. Later connect the pulse generator and digital output.
14.Now we shall move on to code generation, save the model as tutorial1.

15.Click on tools and select run on target hardware and prepare to run, it opens
configuration parameters dialog there we have to select arduino uno as target
hardware and leave rest of parameters as default and click ok.

16. Now we have to download the code to hardware, just go to tools and select
run on target hardware and run.
17. In the bottom bar of model it is going to show status of download
18.After the download finishes, at that time LED has to blink for every second.
Fig. 8: Arduino Uno connected to LED via resistor.

You might also like