Universal GPIO Board User Manual V1.0 With Matlab: Author: Thaufeek
Universal GPIO Board User Manual V1.0 With Matlab: Author: Thaufeek
Universal GPIO Board User Manual V1.0 With Matlab: Author: Thaufeek
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
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
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
# 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
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
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726
Fig. 8
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
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.
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
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);
# 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)
{
# 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(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
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);
digitalWrite(ledPin,LOW);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin7,LOW);
# 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
(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);
}
}
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);
}
}
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726
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
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);
}
}
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);
}
}
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.
/*
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;
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
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726
int buttonPushCounter;
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);
}
}
# 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
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
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
(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);
% 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
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% 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
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);
% Press 1=
while 1
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% This program is to produce different tones in Buzzer
function buzz_tones(print_numb)
clear ports;
clear classes;
delete(instrfindall);
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726
while 1
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
% 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);
print_numb= 0;
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726
while 1
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);
pause (2)
while 1
for p = 0:16
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;
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,
# 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
# 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;
s = serial(serialPort);
fopen(s);
disp('Port is opened now');
tic
while ishandle(plotGraph)
while ishandle(plotGraph1)
# 9/3, 2nd floor, Sree Laksmi Complex, opp, to Vivekananda Park, Girinagar, Bangalore - 560085,
Email: [email protected], Phone: 080 - 26722726
%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
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
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
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
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:
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.
# 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
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
# 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.
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
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.
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