Universal GPIO Board User Manual V1.0 With Matlab: Author: Thaufeek

Download as pdf or txt
Download as pdf or txt
You are on page 1of 82

2013

Universal GPIO Board User Manual V1.0 With


MATLAB

Author: Thaufeek
Version: 1.0

Table of contents
1. Universal GPIO Board Introduction
1.1. Overview
1.2. Board Details
2. Board Design
2.1 Hardware Schematic
2.2 Board Layout
3. Hardware Details
3.1 Light Emitting diodes
3.2 Push Buttons
3.3 Buzzer
3.4 Seven Segment Display
3.5 Potentiometer
3.6 Temperature sensor
3.7 TSOP
3.8 Light dependent Sensor
3.9 Power Switch
4. LAB Experiments with MATLAB

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

1. Universal GPIO Board Introduction


1.1 Overview
Ready to use Input and Output circuits are always important to experiment with any
microcontroller. The Universal GPIO Board is very useful for beginners, hobbyist and
students. It is suitable for carrying out quick experiments with any microcontroller and lets
you access numerous peripheral devices. It provides access to pins through male connectors
for wiring to the microcontroller development board.
1.2 Board Details
This board has below listed interface circuits to work with:
1. 8 LEDs
2. 4 Switches
3. 1 Potentiometer
4. 1 Light Sensor (using Light Dependent Resistor)
5. 1 Temperature Sensor
6. 1 Infrared Receiver
7. 1 Buzzer
8. 1 Seven Segment Display
The above mentioned interface circuits fall into analog/digital/Input/Output as depicted
below:
1. Digital Inputs to any microcontroller
Four Switches
Infrared receiver TSOP 1738
2. Analog Inputs to Any Microcontroller
Potentiometer
Temperature sensor
Light Sensor
3. Use as Digital/Analog Outputs for Any Microcontroller
LEDs
Buzzer
Seven Segment Display

2. Board Design
2.1 Hardware Schematic

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Fig. 1

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

2.2 Board Layout


8 x LEDs

Seven Segment
Display

4 x Switches

Power Switch

Temperature
Sensor

Potentiometer

IR Receiver (TSOP)

Light Sensor

Buzzer

Fig. 2

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

3. Hardware Details
This section will detail every hardware module, interface parameters and pin definitions of
Universal GPIO board.
3.1. Light Emitting Diodes
The Universal GPIO board has eight LEDs, connected to L1, L2L8 through a network resistor.
By driving the pins HIGH (5v), the LEDs can be switched ON.

Fig. 3

3.2. Push Buttons


This board provides four mini switch buttons, connected to SW1, SW2, SW3 and SW4. When the
pushbutton is open (un pressed) there is no connection between the two legs of the pushbutton,
so the pin is connected to GND (through the pull-up resistor) and we read a LOW. When the
button is closed (pressed), it makes a connection between its two legs, connecting the pin to
ground, so that we read a HIGH.

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Fig. 4

3.3. Buzzer
This through-hole buzzer is great for projects where you need something that sounds but dont
have room for a full-blown speaker. We can access the buzzer from the male connector which is
imprinted as Buzzer in the board.

Fig. 5

3.4. Seven Segment Display


The seven segment display is a pretty simple device. It is actually combination of 8 LEDs (the
decimal point is the 8th). It can be arranged so that different combinations can be used to make
numerical digits.
A seven segment is generally available in ten pin package. While eight pins correspond to the eight
LEDs, the remaining two pins (at middle) are common and internally shorted. These segments
come in two configurations, namely, Common cathode (CC) and Common anode (CA). In CC
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

configuration, the negative terminals of all LEDs are connected to the common pins. The
common is connected to ground and a particular LED glows when its corresponding pin is given
high. In CA arrangement, the common pin is given a high logic and the LED pins are given low to
display a number.
This board comes with common anode seven segment displays with the male connector. Use
resistor that wont destroy led. We have connected led pin 3 and 8 to 5v through 1k resistor
because it is common anode display.
Note: Please dont use Common Cathode Display in this board because common pin is connected
to 5V.

Fig. 6

3.5. Potentiometer
A potentiometer, informally a pot, is a three-terminal resistor with a sliding or rotating contact that
forms an adjustable voltage divider. If only two terminals are used, one end and the wiper, it acts
as a variable resistor.
A potentiometer measuring instrument is essentially a voltage divider used for measuring electric
potential (voltage); the component is an implementation of the same principle, hence its name.
Potentiometers are commonly used to control electrical devices such as volume controls on audio
equipment. Potentiometers operated by a mechanism can be used as position transducers, for
example, in a joystick. Potentiometers are rarely used to directly control significant power (more
than a watt), since the power dissipated in the potentiometer would be comparable to the power in
the controlled load.
This board uses a 10k potentiometer and signal pin is connected to male connector. We can access
the signal pin from the male connector which is imprinted as Pot in the board.
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Fig. 7

3.6. Temperature Sensor


The LM35 temperature sensor is a low voltage, precision centigrade temperature sensor. It
provides a voltage output that is linearly proportional to the Celsius temperature. It also doesnt
require any external calibration. The LM35 is rated to operate over a 55C to +150C
temperature range, while the LM35C is rated for a 40C to +110C range. We like it because its
so easy to use: Just give the device a ground and 2.7 to 5.5 VDC and read the voltage on the LM35
male connector pin. The output voltage can be converted to temperature easily using the scale
factor of 10 mV/C.

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Fig. 8

3.7 IR Receiver (TSOP)


TSOP is an IR receiver which will help you to receive IR signal from transmitting devices like the
TV remotes. The TSOP outputs a constant HIGH signal when idle and as it receives data, it tends
to invert the data. i.e when an IR LED is transmitting data onto the TSOP, every time the IR led
goes high, the TSOP will go LOW and vice versa. Remote control signals are often bytes of data
that is encoded and transmitted by pulsing(switching ON & OFF the IR LED at a specific
frequency) Most TV remote controls work at 32-40 Khz frequency and most receivers can receive
this range.
We can access the TSOP signal pin from the male connector which is imprinted as TSOP in the
board.
3.8. Light Sensor
Light-dependent resistor (LDR) or photocell is a light-controlled variable resistor. The resistance
of a photo resistor decreases with increasing incident light intensity; in other words, it exhibits
photoconductivity. A photo resistor can be applied in light-sensitive detector circuits, and lightand dark-activated switching circuits.

Interfacing Universal GPIO Board LEDs with Arduino

Lets start with a quick introduction to the Arduino GPIOs (General Purpose Input Output) and
number of LEDs available in Universal GPIO Board. There are 14 Digital I/O Pins (of which 6
provides PWM output) and 6 Analog Input Pins in Arduino, and There are totally 8 LEDs in
Universal GPIO Board. Arduino can set to HIGH (taking the value 1) by connecting it to a
voltage supply, or set to LOW (taking the value 0) by connecting it to the ground.

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

INTERFACING MATLAB WITH THE GPIO


BOARD
MATLAB is a high-level language and interactive environment for numerical computation,
visualization, and programming. Using MATLAB, you can analyze data, develop algorithms, and
create models and applications. You can use MATLAB for a range of applications, including
communications, plotting and interfacing with external devices. More than a million engineers and
scientists in industry and academia use MATLAB, the language of technical computing.
In this Document, we will see how MATLAB can be interfaced with simple external components
like a 7 Segment display, LEDs, Buzzer etc. in the GPIO board. We will use an Arduino Uno
board to interface Matlab with GPIO board.
MATLAB has a "serial" function that allows it to communicate through a serial port. It is used to
establish serial port connection with the Arduino and demonstrate communication between simple
electronic components and a MATLAB program. For demonstration purposes, Serial datas will be
sent from MATLAB to Arduino user on a GPIO board. But how does a Serial communication
work in Matlab with the Arduino???

Serial Port communication:

The concept of Serial communication is simple, in serial communication data is sent one bit at a
time. Although this is slower, it is simpler and can be used over longer distances.
The Arduino driver emulates a serial port - so we can communicate with it using any serial
communication program. MATLAB and Arduino interaction is done to develop programs to
acquire analog and digital data, and to control DC, servo, and stepper motors.
So in short, we can use Arduino serial communication to physically actuate decisions taken by a
program running on, say, MATLAB in our computer.
Tools used for the experiment:

MATLAB
Arduino Uno
GPIO Board
A-B connector (USB cable suitable for Arduino BOARD)

Now we will see a number of lab experiments that MATLAB interfaces with each of the
components in the GPIO Board. The list of experiments Matlab interfacing with the GPIO board
are:1. Interfacing with all the LED
a. Blinking each LED based on user input
b. Blinking each LED serially to and fro
2. Interfacing with the Buzzer
a. Generate a Tone from the Buzzer
b. Generate different Tones from the Buzzer
3. Interfacing with the 7 Segment Display
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

4.

5.
6.
7.

a. Displaying the numbers 0-F continuously in ascending order and descending


order
b. Displaying the corresponding number based on user input
Interfacing with the Push Buttons
a. Determining which Push button is pressed
b. Increment and decrement method
Interfacing with the Potentiometer
Interfacing with the Light Dependent Resistor (LDR)
Interfacing with the Temperature Sensor (LM35)

Setting up the Arduino tool for communication:The Arduino tool initial configuration is necessary for communication. Before we go into coding
we need to make sure that we have selected the right Port and Board for communication. Open
the tool and check the Board selected and click Arduino UNO as shown below,
But to know what port the Arduino is currently using, we need to check the port used by Arduino
in the Device manager of the Windows desktop. So we need to make sure we select the same port
number in the Arduino tool also. We had seen how to select the Arduino board earlier.

But to know what port the Arduino is currently using, we need to check the port used by Arduino
in the Device manager of the Windows desktop. So we need to make sure we select the same port
number in the Arduino tool also. We had seen how to select the Arduino board earlier.
To check the Port selected we need to follow the procedure as shown below,

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

LAB 1: INTERFACING WITH LED


There are 8 LEDs in the GPIO board and we will try to blink each LED. Before that we need to
give the correct connection from Arduino to the corresponding LEDs. We connect the LEDs 1-8
to the Arduino pins 4-11 correspondingly.
The program will allow the Arduino to communicate with the LEDs in the GPIO board. Before
we go into coding we need to make sure that we have selected the right Port and Board for
communication.

1. BLINKING EACH LEDS BASED ON USER INPUT


Here we will try to blink each LED based on the users input and the corresponding LEDs will
blink. Before that we need to give the correct connection from Arduino to the corresponding
LEDs. We connect the GPIO LEDs 1-8 to the Arduino pins 4-11 correspondingly.
The program will interface with Arduino which will in turn communicate with the LEDs. The
code is as follows,
int ledPin1= 4;
int ledPin2= 5;
int ledPin3= 6;
int ledPin4= 7;
int ledPin5= 8;
int ledPin6= 9;
int ledPin7= 10;
int ledPin8= 11;
int MATLABData;
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

void setup()
{
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
pinMode(ledPin4,OUTPUT);
pinMode(ledPin5,OUTPUT);
pinMode(ledPin6,OUTPUT);
pinMode(ledPin7,OUTPUT);
pinMode(ledPin8,OUTPUT);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);
}

Serial.begin(9600);

void loop()
{
if(Serial.available()>0)
// if there is data to read
{
MATLABData=Serial.read(); // read data
}
if (MATLABData==1)
{
digitalWrite(ledPin1,HIGH); // turn light on
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);
}
else if (MATLABData==2)
{
digitalWrite(ledPin2,HIGH);

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

digitalWrite(ledPin1,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);
}
else if (MATLABData==3)
{
digitalWrite(ledPin3,HIGH);

// turn light on

digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);
}
else if (MATLABData==4)
{
digitalWrite(ledPin4,HIGH);

digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);

else if (MATLABData==5)
{
digitalWrite(ledPin5,HIGH);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);
}
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

else if (MATLABData==6)
{
digitalWrite(ledPin6,HIGH);

digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);

else if (MATLABData==7)
{
digitalWrite(ledPin7,HIGH);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin8,LOW);
}
else if (MATLABData==8)
{
digitalWrite(ledPin8,HIGH);

digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);

2. BLINKING EACH LEDS IN SERIES TO AND FRO

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Here we will try to blink each LED serially one by one continuously. Before that we need to give
the correct connection from Arduino to the corresponding LEDs. We connect the GPIO LEDs
1-8 to the Arduino pins 4-11 correspondingly.
The program will interface with Arduino which will in turn communicate with the LEDs. The
code is as follows,
int ledPin= 4;
int ledPin1= 5;
int ledPin2= 6;
int ledPin3= 7;
int ledPin4= 8;
int ledPin5= 9;
int ledPin6= 10;
int ledPin7= 11;
int MATLABData;
void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
pinMode(ledPin4,OUTPUT);
pinMode(ledPin5,OUTPUT);
pinMode(ledPin6,OUTPUT);
pinMode(ledPin7,OUTPUT);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0)
{

// if there is data to read

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

MATLABData=Serial.read(); // read data


}
if (MATLABData==1 || MATLABData==17)
{
digitalWrite(ledPin,HIGH); // turn light on
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
}
else if (MATLABData==2 || MATLABData==16)
{
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
}
else if (MATLABData==3 || MATLABData==15)
{
digitalWrite(ledPin2,HIGH); // turn light on

digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);

else if (MATLABData==4 || MATLABData==14)


{
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin4,LOW);
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
}
else if (MATLABData==5 || MATLABData==13)
{
digitalWrite(ledPin4,HIGH);

digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);

else if (MATLABData==6 || MATLABData==12)


{
digitalWrite(ledPin5,HIGH);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
}
else if (MATLABData==7 || MATLABData==11)
{
digitalWrite(ledPin6,HIGH);

digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin7,LOW);

else if (MATLABData==8 || MATLABData==10)


{
digitalWrite(ledPin7,HIGH);

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin4,LOW);
}
else if (MATLABData==9)
{
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin,HIGH);
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin4,HIGH);
digitalWrite(ledPin5,HIGH);
digitalWrite(ledPin6,HIGH);
digitalWrite(ledPin7,HIGH);
}

if (MATLABData==18)
{
digitalWrite(ledPin,LOW);

// turn light on

digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

LAB 2: INTERFACING WITH A BUZZER


There is a Buzzer in the GPIO board and we will try to generate or initiate a tone in the Buzzer.

1. GENERATE THE TONE IN BUZZER


We connect the Buzzer Male pin to the Arduino pin 4. When we press 1, a tone should generate
and when we press 2, the tone should get off or the Buzzer must stop the sound generated.
The program must interface with Arduino which will in turn communicate with the Buzzer
connected to pin 4. The coding is given below,
int tune= 4;
int MATLABData;
void setup()
{
pinMode(tune,OUTPUT);
digitalWrite(tune,LOW);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0)
// if there is data to read
{
MATLABData=Serial.read(); // read data
}
if
{

(MATLABData=='1')
digitalWrite(tune,HIGH);
delay(50);

else if
{

(MATLABData=='2')

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

digitalWrite(tune,LOW);
}
}

2. GENERATE DIFFERENT NOTES FROM A BUZZER

We connect the Buzzer pin to the Arduino pin 4. This example shows how to use the tone ()
command to play different notes on multiple outputs. The tone () command works by taking
over one of the Atmega's internal timers, setting it to the frequency we want, and using the
timer to pulse an output pin. Since it's only using one timer, we can only play one note at a
time.
The coding is given below,
int tune= 4;
int MATLABData;
void setup()
{
pinMode(tune,OUTPUT);
digitalWrite(tune,LOW);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0)
// if there is data to read
{
MATLABData=Serial.read(); // read data
}
if
{

(MATLABData=='1')
digitalWrite(tune,HIGH);
tone(4, 440, 200);
delay(50);

else if (MATLABData=='2')
{
digitalWrite(tune,HIGH);
tone(4, 294, 500);
delay(50);
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

}
else if (MATLABData=='3')
{
digitalWrite(tune,HIGH);
tone(4, 723, 800);
delay(50);
}
else if (MATLABData=='4')
{
digitalWrite(tune,LOW);
}
}

LAB 3: INTERFACING WITH 7 SEGMENT DISPLAY


The GPIO board consists of a Common Anode 7 Segment Display. We will communicate with
the 7 segment display and the connections are shown below.
Common Anode 7 Segment Display Part:-

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

We need to connect the corresponding pin to the Arduino as shown below


Arduino
Pin

7
Segment
Connection

7 (A)

6 (B)

4 (C)

2 (D)

1 (E)

9 (F)

10 (G)

5 (DP)

Pin

Note: - If we are using a Common cathode 7 segment display it will have reversed wiring

1. DISPLAY THE CORRESPONDING NUMBER BASED ON USER INPUT


This experiment displays the numbers from 0 to F, i.e when we give a number between 0-F, the
corresponding number will be displayed in the 7 Segment display. The coding for 7 Segment
display is given below,
int MATLABData;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(9, 0); // start with the "dot" off

Serial.begin(9600);

void loop()
{
while (Serial.available()) // if there is data to read
{
MATLABData=Serial.read(); // read data
Serial.println(MATLABData);
}
// write '9'
if (MATLABData==9)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
}
// write '8'
if (MATLABData==8)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
}
// write '7'
if (MATLABData==7)
{
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
// write '6'
if (MATLABData==6)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
}
// write '5'
if (MATLABData==5)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
}
// write '4'
if (MATLABData==4)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
}
// write '3'
if (MATLABData==3)
{
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
}
// write '2'
if (MATLABData==2)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 0);
}
// write '1'
if (MATLABData==1)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
}

2. DISPLAY THE NUMBERS 0-F CONTINUOUSLY IN ASCENDING ORDER AND


DESCENDING ORDER

This experiment is similar to the previous experiment except that it displays the numbers from 0 to
F serially in ascending order and then in descending order. The code is as follows,
int MATLABData;

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(9, 0); // start with the "dot" off

Serial.begin(9600);

void loop()
{
while (Serial.available()) // if there is data to read
{
MATLABData=Serial.read(); // read data
Serial.println(MATLABData);
}
// write '1'
// write '9'
if (MATLABData==9)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
//delay(1000);
}
// write '8'
if (MATLABData==8)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
//delay(1000);

}
// write '7'
if (MATLABData==7)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
// delay(1000);
}
// write '6'
if (MATLABData==6)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
//delay(1000);
}
// write '5'
if (MATLABData==5)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
//delay(1000);
}
// write '4'
if (MATLABData==4)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
//delay(1000);

// write '3'
if (MATLABData==3)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
//delay(1000);
}
// write '2'
if (MATLABData==2)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 0);
// delay(1000);
}
// write '1'
if (MATLABData==1)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
// delay(1000);
}
// write '0'
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

if (MATLABData==0)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
// delay(4000);
}
if (MATLABData==10)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
// delay(4000);
}
if (MATLABData==11)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
// delay(4000);
}
if (MATLABData==12)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
// delay(4000);
}

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

if (MATLABData==13)
{
digitalWrite(2, 0);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
// delay(4000);
}
if (MATLABData==14)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
// delay(4000);
}
if (MATLABData==15)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
// delay(4000);
}
}

LAB 4: INTERFACING WITH PUSH BUTTONS

There are 4 Push Buttons in the GPIO board and we could use them for any purpose and we will
just see how the mere pressing of the Push buttons could set an output.

1. DETERMINING WHICH PUSH BUTTON IS PRESSED


In this experiment, we will display the increment and decrement values based on the Push Button
pressed. We connect the Push Button SW2 and SW3 to 2nd and 3rd Arduino analog pins
correspondingly. When we press Push button SW2, the Push button2 is displayed in the screen
until user stops and in the same way if Push button SW3 is pressed, the Push button3 is
displayed in the screen until user stops.
We could use any Push Buttons as you wish, but make sure the connections are correctly given.
The coding is given below,
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

/*
Button
To know which button is pressed.
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton1 pin
const int buttonPin2 = 3; // the number of the pushbutton2 pin
const int buttonPin3 = 4; // the number of the pushbutton3 pin
const int buttonPin4 = 5; // the number of the pushbutton4 pin
// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

// variable for reading the pushbutton1 status


// variable for reading the pushbutton2 status
// variable for reading the pushbutton3 status
// variable for reading the pushbutton4 status

void setup()
{
Serial.begin(9600);
// initialize the pushbutton pins as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
void loop()
{
// read the state of the pushbutton values:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
// check if the pushbutton1 is pressed.
// if it is, the buttonState1 is HIGH:
if (buttonState1 == HIGH)
{
delay(100);
Serial.println("Button 1 pressed");
}
// check if the pushbutton2 is pressed.
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

// if it is, the buttonState2 is HIGH:


else if (buttonState2 == HIGH)
{
delay(100);
Serial.println("Button 2 pressed");
}
// check if the pushbutton3 is pressed.
// if it is, the buttonState3 is HIGH:
else if (buttonState3 == HIGH)
{
delay(100);
Serial.println("Button 3 pressed");
}
// check if the pushbutton4 is pressed.
// if it is, the buttonState4 is HIGH:
else if (buttonState4 == HIGH)
{
delay(100);
Serial.println("Button 4 pressed");
}

2. INCREMENT AND DECREMENT METHOD


In this experiment the Push Buttons and their connections are similar to the previous experiment
but the final output is different from the previous experiment. We connect the Push Button SW2
and SW3 to 2nd and 3rd Arduino analog pins correspondingly. When we press Push button SW2,
the number will keep increasing until the user stops and in the same way if Push button SW3 is
pressed it will start decrementing the numbers from where we stopped recently and will continue
decrementing until user stops.
The coding is given below,
/*
Button
To Increment the count when pushbutton1 (SW1) is pressed and
decrement the count when pushbutton2 (SW2) is pressed.
*/
// set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton1 pin
const int buttonPin2 = 3; // the number of the pushbutton12 pin
int buttonState1;
int buttonState2;

// variable for reading the pushbutton1 status


// variable for reading the pushbutton2 status

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

int buttonPushCounter;

// variable for printing the count status

void setup()
{
Serial.begin(9600);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop()
{
// read the state of the pushbutton values:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton1 is pressed.
// if it is, the buttonState1 is HIGH:
if (buttonState1 == HIGH )
{
buttonPushCounter++;
delay(300);
Serial.println(buttonPushCounter);
}
// check if the pushbutton2 is pressed.
// if it is, the buttonState2 is HIGH:
if(buttonState2 == HIGH )
{
buttonPushCounter--;
delay(300);
Serial.println(buttonPushCounter);
}
}

LAB 5: INTERFACING WITH POTENTIOMETER


READING THE POTENTIOMETER VALUES

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

This experiment is done to read the analog values of the Potentiometer. The coding is given
below,
int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// Print the Potentiometer values
Serial.println(sensorValue);
}
Now we will the snap shots of the outputs from Arduino. The Arduino output from the Serial
monitor

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

LAB 6: INTERFACING WITH A TEMPERATURE SENSOR


LM35
READING THE TEMPERATURE SENSOR LM35 VALUES AND
PLOTTING IT CORRESPONDINGLY
There is a Temperature Sensor LM35 in the GPIO board and we will read it as Celsius and
Fahrenheit. We connect the Light Dependent Resistor (LDR) to any Arduino analog pins. The
coding is given below,
int sensorPin = A0; // select the input pin for the Temperature sensor LM35
int sensorValue = 0; // variable to store the value coming from the sensor
void setup()
{
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// converting the sensor values to Celsius
float millivolts= (sensorValue/1024.0) * 5000;
float celsius= millivolts/10;
// Printing the Temperature in Celsius
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Serial.print(celsius);
Serial.print(",");
// Converting Celsius to Fahrenheit and printing the Temperature in Fahrenheit
Serial.print((celsius * 9)/5 + 32);
Serial.println ();
delay(1000);
}
Now we will the snap shots of the outputs from Arduino. The Arduino output from the Serial
monitor

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

LAB 7: INTERFACING WITH A LIGHT DEPENDENT


RESISTOR (LDR)
READING
THE
LDR
CORRESPONDINGLY

VALUES

AND

PLOTTING

IT

There is a Light Dependent Resistor (LDR) in the GPIO board and we will read the Light
Dependent Resistor (LDR) Analog values. We connect the Light Dependent Resistor (LDR) to
any Arduino analog pins. The coding is given below,
int sensorPin = A0; // select the input pin for the Light Dependent Resistor (LDR)
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// Print the Light Dependent Resistor (LDR) values
Serial.println(sensorValue);
}
The snap shots of the Serial Monitor readings for LDR. The Arduino output from the Serial
monitor

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Setting up the Matlab:We had done with the Arduino and now we in order to communicate with the GPIO board via
Arduino with Matlab, we need to write a simple serial communication code that can control the
GPIO components. The Matlab code to control the GPIO board components is written in the
form of Functions to minimize the coding and to control all the components from a single code
rather than an individual program for each experiment listed above.
A Function is a group of statements that together perform a task. In MATLAB, Functions are
defined in separate files. The name of the file and of the function should be the same. Functions
can accept more than one input arguments and may return more than one output arguments.
MATLAB communicates with the Arduino board over a USB cable. This is based on a server
program running on the board, which listens to commands arriving via serial port, executes the
commands, and, if needed, returns a result.
Now we need to write a program in the Script file to send or receive data through Serial
communication. We will write all the experiments in a single code divided into functions as
explained earlier. The code for all the experiments is as follows and you need to copy each
program in different script file but in the same file.
Once all the codes are written, click on the green button in red circle as shown in figure
below or press F5 to run the MATLAB code

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

The main code that interlinks all the code is given below followed by the corresponding
experiments code.
function interface_with_gpio()
clear classes;
clear ports;
delete(instrfindall);
clear all;
clc;
% Intialize the Serial Port
s = serial('COM40');
set(s, 'FlowControl', 'hardware');
set(s, 'BaudRate', 9600);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',10);
fopen(s);
disp('COM40 IS OPENED');
print_numb=0;
p=0;
data=0;

% now we need to select the component in the GPIO board that you would like
% to interface with
component_selected = input('Press a number b/w 1 to 7 (1- LED, 2- Buzzer, 37 Seg, 4- Push Buttons, 5- POT, 6-LDR, 7- Temp Sensor)= ');

% if condition for 7 categories and the corresponding call by function's


# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

if
(component_selected==1)
disp('Interfacing with LED');
select = input('Press 1 or 2 (1- LED blinks based on user input, 2- LED
blinks serially to and fro)= ');
if (select==1)
led_user(s);
else
led_serial(p);
end

elseif
(component_selected==2)
disp('Interfacing with a Buzzer');
select = input('Press 1 or 2 (1- Just produce a sound in the Buzzer, 2Produce different tones in the Buzzer)= ');
if (select==1)
buzz_sound(print_numb);
else
buzz_tones(print_numb);
end

elseif
(component_selected==3)
disp('Interfacing with the 7 Segment Display');
select = input('Press a number 1 or 2 (1- Display the corresponding number
pressed, 2- Display 0-F serially in ascending order)');
if (select==1);
sev_seg_user(print_numb);
else
sev_seg_serial(p);
end

elseif
(component_selected==4)
disp('Interfacing with the Push buttons');
select = input('Press a number 1 or 2 (1- Display the button number that is
pressed, 2- Display the Increment and Decrement value based on the button
pressed)= ');
if (select==1);
push_button_determine(data);
else
push_button_inc_dec(data);
end

elseif
(component_selected==5)
disp('Interfacing with the Potentiometer');
plot_pot_values(data);
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

elseif
(component_selected==6)
disp('Interfacing with the LDR');
plot_ldr_values(data);

else
disp('Interfacing with the Temperature Sensor');
plot_temp_values(data);

end
end

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to blink the corresponding LED based on the user
function led_user(print_numb)

% clear the used ports and classes earlier, delete the ports and clear the
screen initially
clear ports;
clear classes;
delete(instrfindall);

s= serial('COM40', 'Baudrate', 9600);


with a ComPort and required Baudrate
fopen(s);
disp('port is opened')
print_numb= 0;

% Initializing the Serial port


% Opening the Serial port
% Initializing the Variable

% Press 1= Switches the LED1 ON, 2= Switches the LED1 OFF, 3= Switches the
LED2 ON, 4= Switches the LED1 OFF
% 5= Switches both the LEDs ON, 6= Switches both the LEDs OFF, 7= Buzzer ON
and 8= BuzzerOFF

while 1
p = 1;

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

fwrite(s, p, 'uint8', 'sync');


fprintf(s, '%s', char(print_numb));
print_numb= input('Press any number between 1 and 6= ');
if
print_numb== 1
disp(['Blinking LED',num2str(print_numb)])
elseif
print_numb== 2
disp(['Blinking LED',num2str(print_numb)])
elseif
print_numb== 3
disp(['Blinking LED',num2str(print_numb)])
elseif
print_numb== 4
disp(['Blinking LED',num2str(print_numb)])
elseif
print_numb== 5
disp(['Blinking LED',num2str(print_numb)])
elseif
print_numb== 7
disp(['Blinking LED',num2str(print_numb)])
elseif
print_numb== 8
disp(['Blinking LED',num2str(print_numb)])
break
end
end

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to blink the LEDs serially to and fro
function led_serial(p)
clear ports;
clear classes;
delete(instrfindall);
s = serial('COM40','BaudRate',9600);

% insert the

serial

fopen(s);
pause (2)
while 1
for p = 1:18
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

fwrite(s, p, 'uint8', 'sync');


if

p== 1 || p==17
disp('Blinking LED1')

elseif
p== 2 || p==16
disp('Blinking LED2')
elseif
p== 3 || p==15
disp('Blinking LED3')
elseif
p== 4 || p==14
disp('Blinking LED4')
elseif
p== 5 || p==13
disp('Blinking LED5')
elseif
p== 6 || p==12
disp('Blinking LED6')
elseif
p== 7 || p==11
disp('Blinking LED7')
elseif
p== 8 || p==10
disp('Blinking LED8')
elseif
p== 9
disp('All LEDs Blinking')
elseif
p== 18
disp('All LEDs OFF')
end
% wait 0.2 seconds before the next loop
pause (0.2)
end
end

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to produce sound in Buzzer
function buzz_sound(print_numb)

clear ports;
clear classes;
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

delete(instrfindall);

% Intitializing the Serial port with a ComPort and required Baudrate


s= serial('COM40', 'Baudrate', 9600);
fopen(s);
disp('port is opened')
print_numb=
% Initializing the Variable

% Press 1=
while 1

% Opening the Serial port


0;

Buzzer ON and 2 = Buzzer OFF

fprintf(s, '%s', char(print_numb));


print_numb= input('Press 1 or 2= ');
if
print_numb== 1
disp(['Buzzer is ON',num2str(print_numb)])
elseif
print_numb== 2
disp(['Buzzer is OFF',num2str(print_numb)])
end
end

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to produce different tones in Buzzer
function buzz_tones(print_numb)
clear ports;
clear classes;
delete(instrfindall);

% Intitializing the Serial port with a ComPort and required Baudrate


s= serial('COM40', 'Baudrate', 9600);
fopen(s);
disp('port is opened')
print_numb= 0;

% Opening the Serial port


% Initializing the Variable

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

% Press 1= Generates a Tone1, 2 = Generates a Tone2, 3= Generates a Tone3,


% 4- Buzzer gets to OFF mode

while 1

fprintf(s, '%s', char(print_numb));


print_numb= input('Press a number between 1 to 4= ');
if
print_numb== 1
disp(['Generate tone 1',num2str(print_numb)])
elseif
print_numb== 2
disp(['Generate tone 2',num2str(print_numb)])
elseif
print_numb== 3
disp(['Generate tone 3',num2str(print_numb)])
elseif
print_numb== 4
disp(['Buzzer is OFF',num2str(print_numb)])
end
end

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to display the corresponding number in the 7 segment
display given by the user
function sev_seg_user(print_numb)
clear ports;
clear classes;
delete(instrfindall);

% Intitializing the Serial port with a ComPort and required Baudrate


s= serial('COM40', 'Baudrate', 9600);
fopen(s);
disp('port is opened')

% Opening the Serial port

print_numb= 0;

% Initializing the Variable

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

% Press a number between 0-9 and the corresponding number will be


% displayed in 7 Segment Display

while 1

fprintf(s, '%s', char(print_numb));


print_numb= input('Press any number between 0 and 9= ');
if
print_numb== 0
disp(['Displays ',num2str(print_numb)])
elseif
print_numb== 1
disp(['Displays ',num2str(print_numb)])
elseif
print_numb== 2
disp(['Displays number= ',num2str(print_numb)])
elseif
print_numb== 3
disp(['Displays number= ',num2str(print_numb)])
elseif
print_numb== 4
disp(['Displays number= ',num2str(print_numb)])
elseif
print_numb== 5
disp(['Displays number= ',num2str(print_numb)])
elseif
print_numb== 6
disp(['Displays number= ',num2str(print_numb)])
elseif
print_numb== 7
disp(['Displays number= ',num2str(print_numb)])
elseif
print_numb== 8
disp(['Displays number= ',num2str(print_numb)])
elseif
print_numb== 9
disp(['Displays number= ',num2str(print_numb)])

end
end

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to display from 0-F serially in ascending order
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

function sev_seg_serial(p)
clear ports;
clear classes;
delete(instrfindall);

% Intitializing the Serial port with a ComPort and required Baudrate


s= serial('COM40', 'Baudrate', 9600);
fopen(s);
disp('port is opened')

% Opening the Serial port

pause (2)
while 1
for p = 0:16

fwrite(s, p, 'uint8', 'sync');


if

p== 0
disp('Blinking 0')

elseif
p== 1
disp('Blinking 1')
elseif
p== 2
disp('Blinking 2')
elseif
p== 3
disp('Blinking 3')
elseif
p== 4
disp('Blinking 4')
elseif
p== 5
disp('Blinking 5')
elseif
p== 6
disp('Blinking 6')
elseif
disp('Blinking 7')
elseif
disp('Blinking 8')
elseif

p== 7

p== 8

p== 9

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

disp('Blinking 9')
elseif
disp('Blinking A')
elseif
disp('Blinking B')
elseif
disp('Blinking C')
elseif
disp('Blinking D')
elseif
disp('Blinking E')
elseif
disp('Blinking F')

p== 10

p== 12

p== 13

p== 14

p== 15

p== 16

end
pause (1)
end
end

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to display as to which button is pressed
function push_button_determine(data)

clear ports;
clear classes;
delete(instrfindall);

sample_number = 0;
serialObj = serial('COM40' , 'BaudRate', 9600);
fopen(serialObj);
disp('COM40 IS OPENED');

while(1)
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

sample_number= sample_number+1;
data = fgetl(serialObj);
disp(data);
if (sample_number==65535)
%This is to make sure that we set the graph the right way in terms of the
%x-axis. Instead of real system time this makes use of the sample number.
sample_number=0;
end
end

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to display the increment and decrement values based on
% the Push Button pressed and plot a 2D graph accordingly
function push_button_inc_dec(data)

clear classes;
clear ports;
delete(instrfindall);
serialPort = 'COM40';
scrollWidth = 10;

time = 0;
data = 0;
count = 0;

plotGraph = plot(time,data,'-ro',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
title('Push button incrementing and decrementing');
xlabel('Elapsed Time (s)');
ylabel('Data');
grid on;
s = serial(serialPort);
disp('Port is opened now');
fopen(s);
tic;
while ishandle(plotGraph)
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

dat = fscanf(s,'%f');
disp('The data received is: ');
disp(dat);
if(~isempty(dat) && isfloat(dat))
count = count + 1;
time(count) = toc;
data(count) = dat(1);

if(scrollWidth > 0)
set(plotGraph,'XData',time(time
>
scrollWidth),'YData',data(time > time(count)-scrollWidth));

time(count)-

else
set(plotGraph,'XData',time,'YData',data);
end
pause(0.1);
end
end

The MATLAB plot clearly shows that the graph that the increment value is the time SW2
is being pressed and the decrement value is the time SW3 is pressed.

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to display the Potentiometer values in the console
% and plot the reading correspondingly

function plot_pot_values(data)

clear classes;
clear ports;
delete(instrfindall);
clear all;
close all;
clc;
serialPort = 'COM40';
scrollWidth = 10;

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

time = 0;
data = 0;
count = 0;

plotGraph = plot(time,data,'-ro',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
title('Potentiometer reading');
xlabel('Elapsed Time (s)');
ylabel('Data');
grid on;

s = serial(serialPort);
disp('Port is opened now');
fopen(s);
while 1
p = 3;

fwrite(s, p, 'uint8', 'sync');

tic;
while ishandle(plotGraph)

dat = fscanf(s,'%f');
dat = fgetl(s);%Read Data from Serial as Float
disp('The data received is: ');
disp(dat);
if(~isempty(dat) && isfloat(dat))
count = count + 1;
time(count) = toc;
data(count) = dat(1);

if(scrollWidth > 0)
set(plotGraph,'XData',time(time
>
scrollWidth),'YData',data(time > time(count)-scrollWidth));

time(count)-

else
set(plotGraph,'XData',time,'YData',data);
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

end

pause(0.1);
end
end
end

The MATLAB windows console output is as shown but before we execute the MATLAB
we need to close the Arduino Serial monitor,

The MATLAB Potentiometer Plot

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to display the LDR values in the console
% and plot the reading correspondingly
function plot_ldr_values

clear classes;
clear ports;
delete(instrfindall);
clear all;
close all;
clc;
serialPort = 'COM40';
scrollWidth = 10;
time = 0;
data = 0;
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

count = 0;

plotGraph = plot(time,data,'-ro',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
title(' Light Dependent Resistor (LDR) reading');
xlabel('Elapsed Time (s)');
ylabel('Data');
grid on;

s = serial(serialPort);
disp('Port is opened now');
fopen(s);
tic;
while ishandle(plotGraph)

dat = fscanf(s,'%f');
dat = fgetl(s);%Read Data from Serial as Float
disp('The data received is: ');
disp(dat);
if(~isempty(dat) && isfloat(dat))
count = count + 1;
time(count) = toc;
data(count) = dat(1);

if(scrollWidth > 0)
set(plotGraph,'XData',time(time
>
scrollWidth),'YData',data(time > time(count)-scrollWidth));

time(count)-

else
set(plotGraph,'XData',time,'YData',data);
end

pause(0.1);
end
end

The MATLAB windows console output is as shown but before we execute the MATLAB
we need to close the Arduino Serial monitor,

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

The MATLAB Plot

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to display the Temperature sensor values in the console
% and plot the reading correspondingly
function plot_temp_values(data)
%Clean up functions to make sure when the code is started we are t a steady
%state to run the code
a= instrfindall; %find all open serial ports from last instances
delete(a);
%delete the objects.
clear all;
clc;
close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
serialPort = 'COM40'; %Change Serial as needed according to the Comport
% on the arduino seen on the pc
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plotTitle = 'Temperature Readings';
xLabel = 'Time (s)';
yLabel = 'Data';
legend('Celsius','Fahrenheit')
plotGrid = 'on';
scrollWidth = 10;
delay = .01;

%Init time as well as data to zero.


time = 0;
data = 0;
data1= 0;
count = 0;
figure(1);
to the same.

% Create one figure and plot

%% Plotting the Celsius value


plotGraph = plot(time,data,'-ro',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
hold on; %The data from last plot is retained.
%% Plotting the Fahrenheit value
plotGraph1 = plot(time,data1,'-ro',...
'LineWidth',1,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',2);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
grid(plotGrid);
hold on;

s = serial(serialPort);
fopen(s);
disp('Port is opened now');

% Assigning a variable for the serial port


% Opening the serial port

tic
while ishandle(plotGraph)
while ishandle(plotGraph1)

dat = fgetl(s);%Read Data from Serial as Float


[a_component,b_component] = iExtractAB(dat);

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

%%%%%% Uncomment only if u need to see individual sensor datas. %%%%%%%%%%%


disp('The temperature value in Celsius: ');
disp(a_component);
disp('The temperature value in Fahrenheit: ');
disp(b_component);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
count = count + 1; %increment to keep track of samples.
time(count) = toc; %#ok
data(count) = str2double(a_component);%#ok
data1(count) = str2double(b_component);%#ok

%Grow and shrink time axis when the data is coming in and update the
%Value of the sensor in the Y-Axis.
if(scrollWidth > 0)
set(plotGraph,
'XData',time(time
>
time(count)scrollWidth),'YData',data(time > time(count)-scrollWidth));
set(plotGraph1,'XData',time(time
>
time(count)scrollWidth),'YData',data1(time > time(count)-scrollWidth));
else
set(plotGraph, 'XData',time,'YData',data);
set(plotGraph1,'XData',time,'YData',data1);
end
count = count + 1;
pause(delay);
end
end
end
function [a_component,b_component] = iExtractAB(dat)
[a_component, remain] = strtok(dat,
',');
[b_component, ~] =
strtok(remain, ',');
end

The MATLAB windows console output is as shown but before we execute the MATLAB
we need to close the Arduino Serial monitor,

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

The MATLAB Temperature Sensor Plot for both Celsius and Fahrenheit

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

INTERFACING SIMULINK WITH THE GPIO


BOARD
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Simulink is a graphical extension to MATLAB for the modeling and simulation of systems. In
Simulink, systems are drawn on screen as block diagrams. Many elements of block diagrams are
available (such as transfer functions, summing junctions, etc.), as well as virtual input devices
(Such as function generators) and output devices (such as oscilloscopes). Simulink is integrated
with MATLAB and data can be easily transferred between the programs.
Simulink is supported on UNIX, Macintosh, and Windows environments, and it is included in the
Student version of MATLAB for personal computers.

STARTING SIMULINK
Simulink is started from the MATLAB command prompt
>> Simulink
Alternatively, you can hit the New Simulink Model button at the top of the MATLAB command
window as shown below.

The Simulink Library Browser window should now appear on the screen. Most of the blocks
needed for modeling basic systems can be found in the subfolders of the main "Simulink" folder
(opened by clicking on the "+" in front of "Simulink"). Once the "Simulink" folder has been
opened, the Library Browser window should look like:

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

In Simulink, a model is a collection of blocks which, in general, represents a system. In addition, to


drawing a model into a blank model window, previously saved model files can be loaded either
from the Filemenu or from the MATLAB command prompt.
Example:
digital_one.slx
There are two major classes of items in Simulink: blocks and lines. Blocks are used to generate,
modify, combine, output, and display signals. Lines are used to transfer signals from one block to
another.
And Lastly, Simulink provides a graphical editor, customizable block libraries, and solvers for
modeling and simulating dynamic systems. It is integrated with MATLAB, enabling us to
incorporate MATLAB algorithms into models and export simulation results to MATLAB for
further analysis.
The recent versions of Simulink have support package for Arduino (UNO, Mega2560, etc.),
BeagleBone, etc. This has made it easier to simulate and upload the designs directly to the board.
But Before getting to it, you need to check for some pre-requisites. Your Computer must have
MATLAB-R2013 or above, (This tutorial is on MATLAB-R2013b). If you have Student version of
MATLAB -2013 or 2014, it will do.
Now we need to install the Arduino Support Package from the official MathWorks site. Now
follow the procedure as shown below,

Set up Simulink support package for Arduino


# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726


Open

Start MATLAB
MATLAB

and

click

the

Add-Ons

drop

down

menu

on

the

top

right

Start
Support
Package
Installer
Click on Get Hardware Support Packages in the drop down menu to start the installer.
Select 'Internet' as a source for installing the support package

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Select
Arduino
from
a
list
of
support
Click Next to see a list of support packages and select Arduino from the list

packages

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

MathWorks
Account
Click next and log in to your MathWorks account. If you don't have a MathWorks
account, you can create one during the install process or by visiting this page on the
MathWorks website.

Continue
and
Complete
the
Installation
Accept the license agreement on the next screen and click Next through the following
screens to finish the installation

Serial Port communication:


# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

The concept of Serial communication is simple, in serial communication data is sent one bit at a
time. Although this is slower than parallel communication, which allows the transmission of an
entire byte at once, it is simpler and can be used over longer distances.
One of the better features of Arduino is that it can communicate serially with MATLAB and
Simulink. The Arduino driver emulates a serial port - so we can communicate with it using any
serial communication program. Simulink and Arduino interaction is done to develop programs to
acquire analog and digital data, and to control DC, servo, and stepper motors.
So in short, we can use Arduino serial communication to physically actuate decisions taken by a
program running on, say, Simulink in our computer.
Tools used for the experiment:

MATLAB Tool (which includes Simulink)


Arduino Uno
GPIO Board
A-B connector (USB cable suitable for Arduino BOARD)

LAB 1: INTERFACING WITH LED


1. BLINKING ALL THE LEDS ON THE IO BASED MODEL
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

There are 8 LEDs in the GPIO board and we will try to blink all the LEDs on the IO based
model. Before that we need to give the correct connection from Arduino to the corresponding
LEDs.
In this experiment we use,
Constant block
-1
Arduino Digital Output block - 8
We connect the LEDs L1-L8 in the IO Board to the Arduino pins 4-11 correspondingly as shown
below in the snapshot of the model. Once the hardware is ready we can setup the Simulink for
communication.
From the Simulink Library Browser, we select the Counter or Constant Block. This block is used
to give a constant high input but the Counter block gives a high input based on the sample
frequency given by the user. So we select the Counter block for our experiment and we also select
the 8 Digital Output Blocks for 8 LEDs from the Simulink Support Package from Arduino
section. Connect the Counter block with each of the 8 Digital Output Block.
Now double click on each of the 8 Digital output Block and select the corresponding pin number
from 1-8. And also double click on the Counter block and select the sample time to 0.5 and save
the file.

Deploying Simulink Model on Arduino Hardware:Note- This procedure will be shown only in the first experiment, please follow the same procedure for
other experiments also.
Before we run the Simulink model, we need to do further configurations. So select the Model
Configuration Parameters from the Simulation icon in the top of the Simulink model as shown below,

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Now select the Hardware as Arduino Uno in the Target Hardware Selection and let the Host
COM Port be set as Automatically. And also select the Baud rate to 9600.

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Once all the above parameters have been set accordingly, we can now run the hardware by clicking
the Deploy to Hardware button as shown below and the hardware will start deploying and it
may take a few minutes based on the number of blocks used and complexity.

We can see all the LEDs blinking ON and OFF with a time interval of 0.5 seconds. If any issues
with running of the hardware, make sure all the above parameters and hardware are configured
correctly.

2. BLINKING ALL THE LEDS SERIALLY ON THE IO BASED MODEL


In this experiment we use,
Counter block
-1
Compare to Constant block - 8
Arduino Digital Output block - 8
The Counter block upper limit must be set to 8 and Sample frequency as 1. The Counter block
increments the given value from 1 to 8 at a Sample frequency of 1. The Compare to Constant
block is used to compare the given value from the Counter block and give it accordingly to
Arduino Digital Output. For example, it Compare to Constant block is equal (select this symbol in
the properties == ) to 1, the Arduino pin 1 is set high , and == 2 for Arduino pin 2 and the same
follows for all the other blocks correspondingly. Double click on the Arduino Digital Outputs
blocks and type the corresponding pin number required. The snap shot of the model is shown
below.

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

We deploy the hardware now once all the connections are done perfectly in the Arduino, GPIO
board and in Simulink. Now once the hardware is deployed, the LEDs blink in serial fashion until
the deployment is stopped

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

LAB 2: INTERFACING WITH A BUZZER


1. GENERATE THE TONE IN BUZZER
There is a Buzzer in the GPIO board and we will try to generate or initiate a tone in the Buzzer.
Before that we need to give the correct connection from Arduino to the corresponding Buzzer pin
and we can configure the Simulink for the input.
In this experiment we use,
Constant block
-1
Arduino Digital Output block - 1
We connect the Buzzer pin to the Arduino pin 4. Once the hardware is ready we can setup the
Simulink for communication.
From the Simulink Library Browser, we select the Constant Block. This block is used to give a
constant high input. And we also select the Digital Output Block for Buzzer from the Simulink
Support Package from Arduino section. Connect the Constant block with the Digital Output
Block. Now double click on the Digital output Block and select pin number 4.
The model must look like this as shown below,

Final Execution:Once all the parameters have been set accordingly, we can now run the hardware by clicking the
Deploy to Hardware button as shown below and the hardware will start deploying and it may
take a few minutes based on the number of blocks used and complexity.
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Now the Buzzer produces a sound continuously.

LAB 3: INTERFACING WITH THE PUSH BUTTONS


SWITCHING ON THE CORRESPONDING LEDS BASED ON THE
PUSH BUTTON PRESSED
A Push-button (also spelled Pushbutton) or simply Button is a simple switch mechanism for
controlling some aspect of a machine or a process. Here in this experiment we will blink the
corresponding LEDs based on the Push buttons pressed in Simulink. The blocks used for this
experiment are,
Arduino Digital Input block - 4 (Give pin number 1-4)
Arduino Digital Output block - 8 (Give pin number 5-12)
The connections are simple; between Arduino and GPIO board just connect the Arduino pins 1-4
to Push buttons SW1- SW4 correspondingly and Arduino pins 5-12 to LEDs 1-8 correspondingly.
In the Simulink, connect pin1 to pin5 and pin6, pin2 to pin7 and pin8, pin 3 to pin 9 and pin10
and lastly, pin4 to pin 11 and pin12 The model is shown below,

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Now we can deploy the hardware to Arduino and once it is completely done, and now when we
press the SW1 to SW4, the corresponding LEDs will blink. In the same way the Push Buttons
could be used to activate any components in this way.

LAB 4: INTERFACING WITH A POTENTIOMETER


READING THE VALUES OF POTENTIOMETER
A Potentiometer informally a Pot is a three-terminal resistor with a sliding or rotating contact
that forms an adjustable voltage divider. As we know that there is a Potentiometer (Pot) in the
GPIO board and we will be just reading the values of the Pot. Before that we need to give the
correct connection from Arduino to the corresponding Pot pin and we can configure the Simulink
for the serial input. We connect the Pot pin to the Arduino pin 4 in this experiment.
For this experiment, we need to build 2 models to interact with each other to get the Pot values
displayed in the Simulink display block.
For the first model, we set the Arduino pin selected to activate the Pot pin in the GPIO board.
The components used for the first model are,
Arduino Analog Input block- The Pot readings will be analog, so we use an Arduino
Analog Input block which is connected to the 4 th pin in Arduino Digital to the Pot in the
GPIO board.
Arduino Digital Output block- This block is used to receive the readings in the Digital
form
Data Type Conversion block- The Data Type Conversion block converts an input signal
of any Simulink data type to the data type you specify for the Output data
type parameter. The input can be any real- or complex-valued signal. If the input is
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

real, the output is real. If the input is complex, the output is complex. We convert the
input signal to uint8or an 8-bit unsigned integer
Arduino Serial Transmit block- Sends a buffered data to serial port

We need to connect the Arduino Analog Input block to both the Digital output block and the
Serial Transmit block. The Serial Transmit block will receive a data converted to an unsigned
integer. Once the hardware is ready we can setup the Simulink for communication. The model
must look like this as shown below,

The second model is used to receive the Pot data from the first model (Serial Transmit block) and
display and plot the POT values in the Display block and Scope block correspondingly. The
second model consists of the following components,
Serial Receive block
Scope block- This plots the POT readings
Display block- Displays the POT values

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

The COM20 is the port that Arduino represents when it is connected to the PC. Once the
connection is done as in the models above, we need to make sure that we have selected the Baud
rate to 115200 and Target Hardware as Arduino Uno from the Tools-Run to Target Hardware in
both the models. Once done, we need to deploy the first model until it is completely deployed and
then deploy the second model.
Once done we can see the POT readings displaying in the Display block and it keeps changing
according to the change manually done by the user in POT and likewise the plotting is done
simultaneously in the Scope block and can be seen if we double click the Scope block.

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

LAB 5: INTERFACING WITH LIGHT DEPENDENT


RESISTOR (LDR)
READING THE VALUES OF LIGHT DEPENDENT RESISTOR (LDR)
LDRs or Light Dependent Resistors are very useful especially in light/dark sensor circuits.
Normally the resistance of an LDR is very high, sometimes as high as 1000 000 ohms, but when
they are illuminated with light, resistance drops dramatically. And we know that there is a Light
Dependent Resistor (LDR) in the GPIO board and we will be just reading the values of the LDR.
Before that we need to give the correct connection from Arduino to the corresponding LDR pin
and we can configure the Simulink for the serial input. We connect the B LDR pin to the Arduino
pin 4 in this experiment.
For this experiment, we need to build 2 models to interact with each other to get the LDR values
displayed in the Simulink display block. The experiment is quite similar to the previous experiment
(Interfacing with POT).
For the first model, we set the Arduino pin selected to activate the LDR pin in the GPIO board.
The components used for the first model are,
Arduino Analog Input block- The LDR readings will be analog, so we use an Arduino
Analog Input block which is connected to the 4 th pin in Arduino Digital to the Pot in the
GPIO board.
Arduino Digital Output block- This block is used to receive the readings in the Digital
form
Data Type Conversion block- The Data Type Conversion block converts an input signal
of any Simulink data type to the data type you specify for the Output data
type parameter. The input can be any real- or complex-valued signal. If the input is
real, the output is real. If the input is complex, the output is complex. We convert the
input signal to uint8or an 8-bit unsigned integer

Arduino Serial Transmit block- Sends a buffered data to serial port

We need to connect the Arduino Analog Input block to both the Digital output block and the
Serial Transmit block. The Serial Transmit block will receive a data converted to an unsigned
integer. Once the hardware is ready we can setup the Simulink for communication. The model
must look like this as shown below,

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

The second model is used to receive the LDR data from the first model (Serial Transmit block)
and display and plot the LDR values in the Display block and Scope block correspondingly. The
second model consists of the following components,
Serial Receive block
Scope block- This plots the LDR readings
Display block- Displays the LDR values

The COM20 is the port that Arduino represents when it is connected to the PC. Once the
connection is done as in the models above, we need to make sure that we have selected the Baud
rate to 115200 and Target Hardware as Arduino Uno from the Tools-Run to Target Hardware in
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

both the models. Once done, we need to deploy the first model which will send the LDR readings
in serial port and then deploy the second model to receive it.
Once done we can see the LDR readings displaying in the Display block and it keeps changing
according to light intensity that falls on the LDR. The more light intensity, it resistance decreases
and it exhibits more passage of current flow. So the readings received from the LDR are also
plotted simultaneously in the Scope block and can be seen if we double click the Scope block.

LAB 6: INTERFACING WITH TEMPERATURE SENSOR


LM35
READING THE VALUES OF TEMPERATURE SENSOR LM35
The Temperature Sensor LM35 is an integrated circuit sensor that can be used to measure
temperature with an electrical output proportional to the temperature (in oC). And we know that
there is a Temperature Sensor LM35 in the GPIO board and we will be just reading the values of
the Temperature Sensor LM35. Before that we need to give the correct connection from Arduino
to the corresponding Temperature Sensor LM35 pin and we can configure the Simulink for the
serial input. We connect the Temperature Sensor LM35 pin to the Arduino pin 3 in this
experiment.
For this experiment, we need to build 2 models to interact with each other to get the LDR values
displayed in the Simulink display block. The experiment is quite similar to the previous
experiments (Interfacing with POT and Interfacing with LDR).
For the first model, we set the Arduino pin selected to activate the Temperature Sensor LM35 pin
in the GPIO board. The components used for the first model are,
Arduino Analog Input block- The Temperature Sensor LM35 readings will be analog,
so we use an Arduino Analog Input block which is connected to the 3rd pin in Arduino
Digital to the Pot in the GPIO board.
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

Arduino Digital Output block- This block is used to receive the readings in the Digital
form
Data Type Conversion block- The Data Type Conversion block converts an input signal
of any Simulink data type to the data type you specify for the Output data
type parameter. The input can be any real- or complex-valued signal. If the input is
real, the output is real. If the input is complex, the output is complex. We convert the
input signal to uint8or an 8-bit unsigned integer
Arduino Serial Transmit block- Sends a buffered data to serial port

We need to connect the Arduino Analog Input block to both the Digital output block and the
Serial Transmit block. The Serial Transmit block will receive a data converted to an unsigned
integer. Once the hardware is ready we can setup the Simulink for communication. The model
must look like this as shown below,

The second model is used to receive the Temperature readings from the first model (Serial
Transmit block) and display and plot the temperature values in the Display block and Scope block
correspondingly. The second model consists of the following components,
Serial Receive block
Scope block- This plots the temperature readings
Display block- Displays the temperature

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

The COM20 is the port that Arduino represents when it is connected to the PC. Once the
connection is done as in the models above, we need to make sure that we have selected the Baud
rate to 115200 and Target Hardware as Arduino Uno from the Tools-Run to Target Hardware in
both the models. Once done, we need to deploy the first model which will send the LDR readings
in serial port and then deploy the second model to receive it.
Once done we can see the temperature displaying in the Display block and it keeps changing
according to temperature around the sensor LM35. And the temperature is also plotted
simultaneously in the Scope block and can be seen if we double click the Scope block.
For more information please visit: www.tenettech.com
For technical query please send an e-mail: [email protected]

# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726

You might also like