Está en la página 1de 14

UNIVERSIDAD CATÓLICA DE SANTA MARÍA

ESCUELA PROFESIONAL DE INGENIERÍA MECÁNICA, MECÁNICA


ELÉCTRICA Y MECATRÓNICA
ASIGNATURA : MECANICA COMPUTACIONAL II LABORATORIO 07 – 08 – 09

TERCERA FASE Docente (s):


Ing. Henry Christian Zegarra Gago.
Desarrollo de Aplicaciones I – II – III
Matlab - Arduino Fecha: 2021.12.29.

I. OBJETIVOS.
a. Identificar características y especificaciones del módulo de entrenador PIC-Arduino
b. Realizar la instalación del soporte de Arduino desde Matlab.
c. Conocer las instrucciones de programación para salida digital y entrada analógica.
d. Elaborar aplicaciones básicas para el manejo de entrada y salida de datos usando
GUIDE Matlab.

II. HERRAMIENTAS
1. Computador con acceso a internet.
2. Apuntes de laboratorio.

III. BIBLIOGRAFIA
[1] Fernández F. Arduino Libro de Proyectos, Arduino 2013.
[2] Corona L., Abarca G. y Mares J. Sensores y actuadores. Aplicaciones con Arduino. Patria 2014.
IV. Actividades

1. Identifique cada uno de los bloques de la tarjeta entrenadora de PIC/Arduino.

1. Panel LCD - Pantalla LCD – 12. Entradas analógicas 0– 5 V –


Bloque de salida - LCD Bloque de E. Analógicas
2. Panel Display – Bloque de 13. Entrada sonorización térmica –
Salida Display 7 segmentos Bloque de entrada de datos -
3. Bloque de alimentación 7-12 V sensorización Térmica
4. Salidas 5 – 12 V – Bloque de 14. Relés – Zonas de Control RELÉ-
salidas de voltaje Bloque de Salida de control
5. Entrada Universal Serial (USB) 15. Entradas BLUETOOTH – Bloque
– Bloque de Entrada Universal Entrada BLUETOOTH HC 05/06
(USB) 16. Entrada comunicación serial
6. Teclado – Bloque de entrada VGA – BLOQUE DE
de datos - Teclado Matricial COMUNICACIÓN SERIAL
7. Bloque de Salida - AUDIO - 17. Salidas de Relés
Buzzer 16 KB -5V 18. Zona de control de Potencia –
8. Entradas Digitales – Bloque de BLOQUE DE CONTROL DE
Entradas Digitales (0 - 1) POTENCIA
9. Zona intercambiable 19. Bloque de Entrada Modulo
ARDUINO-PIC – Bloque de Ultra Sonido
intercambio PIC - ARDUINO 20. Bloque de Comunicación 12C
10. Salidas Digitales 8bits – 21. Bloque de comunicación y
BLOQUE salidas digitales 8 bits control SERVOMOTOR
(0 – 1) 22. Jumper, alternador de
11. Entradas analógicas 0 – 5 V - alimentación USB-Jack
Bloque de E. Analógicas 23. Salidas 5 – 12 V – Bloque de
salidas de voltaje
24. Bloque entrada, sensor 25. Entrada analógica de 4 pines
INFRARROJO 26. Bloque de salida LCD Gráfico

2. Manejo de salidas digitales. Elabore el GUIDE para encender un led según diseño
presentado en clase.
a. Elabore Guide.

b. Coloque le programa de los componentes programados, separelos por


componente.
% --- Executes just before DelgadoV_ArduinoG05
Opening Function is made visible.
function
DelgadoV_ArduinoG05_OpeningFcn(hObject,
eventdata, handles, varargin)
% This function has no output args, see
OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
% varargin command line arguments to
DelgadoV_ArduinoG05 (see VARARGIN)
handles.a=arduino;
set
(handles.eTextPue,'String',handles.a.Port);
%Capturando puesrto COMX
set
(handles.pMenuPdig,'String',strseq('D',2:13));
handles.opcion='D2';
% Choose default command line output for
DelgadoV_ArduinoG05
handles.output = hObject;

% Update handles structure


guidata(hObject, handles);
% --- Executes on selection change in
pMenuPdig.
function pMenuPdig_Callback(hObject,
eventdata, handles)
% hObject handle to pMenuPdig (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)

% Hints: contents =
cellstr(get(hObject,'String')) returns
pMenuPdig contents as cell array
% contents{get(hObject,'Value')}
Togle Button
returns selected item from pMenuPdig
lista=get(hObject,'String');
pin=get(hObject,'Value');
handles.opcion=lista{pin};
guidata(hObject,handles);
% --- Executes on button press in
tButtonOnOff.
function tButtonOnOff_Callback(hObject,
eventdata, handles)
% hObject handle to tButtonOnOff (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle


state of tButtonOnOff
% La propiedad VALUE de nuestro toggle button
puede tomar 2 valores
%Value = 0 ---> si ha sido presionado
Push Button nuevamente
%Value = 1 ---> Si ha sido presionado
estado=get(hObject,'Value');
if estado ==1

writeDigitalPin(handles.a,handles.opcion,1);
set(handles.tButtonOnOff,'String','Apagar
Led');
else

writeDigitalPin(handles.a,handles.opcion,0);

set(handles.tButtonOnOff,'String','Encender
Led');
end
% --- Executes when user attempts to close
Close Function figure1.
function figure1_CloseRequestFcn(hObject,
eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
clear handles.a
% Hint: delete(hObject) closes the figure
delete(hObject);

3. Según el programa mostrado en clase para lectura de entrada analógica, elabore un


GUIDE para poder capturar una señal de voltaje entre 0 y 5 voltios a través del
puerto A0.
a. Elabore Guide.

b. Coloque le programa de los componentes programados, separelos por

componente
function
Opening Function DelgadoV_RojasJ_Analogico_G05_OpeningFcn(hObject,
eventdata, handles, varargin)
% This function has no output args, see
OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future
version of MATLAB
% handles structure with handles and user data
(see GUIDATA)
% varargin command line arguments to
DelgadoV_RojasJ_Analogico_G05 (see VARARGIN)
global k IO
k=0;
IO=arduino('COM3')
% Choose default command line output for
DelgadoV_RojasJ_Analogico_G05
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes DelgadoV_RojasJ_Analogico_G05 wait
for user response (see UIRESUME)
% uiwait(handles.figure1);
function edit1_Callback (hObject, eventdata,
handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future
version of MATLAB
% handles structure with handles and user data
(see GUIDATA)
handles.dato1=get(hObject,'string')
Vmax handles.Ymax=str2double(handles.dato1)

guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents
of edit1 as text
% str2double(get(hObject,'String'))
returns contents of edit1 as a double

function edit3_Callback(hObject, eventdata,


handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future
version of MATLAB
% handles structure with handles and user data
(see GUIDATA)
handles.dato3=get(hObject,'string')
Vmin handles.Ymin=str2double(handles.dato3)

guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents
of edit3 as text
% str2double(get(hObject,'String'))
returns contents of edit3 as a double
function edit2_Callback(hObject, eventdata,
handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future
version of MATLAB
% handles structure with handles and user data
(see GUIDATA)
Núm. muestras handles.dato2=get(hObject,'string')
handles.Xmax=str2double(handles.dato2)
guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents
of edit2 as text
% str2double(get(hObject,'String'))
returns contents of edit2 as a double
% --- Executes on button press in pButtonLim.
function pButtonLim_Callback(hObject, eventdata,
handles)
% hObject handle to pButtonLim (see GCBO)
% eventdata reserved - to be defined in a future
version of MATLAB
Limpiar Campos % handles structure with handles and user data
(see GUIDATA)
lim = '';
set(handles.edit1,'String',lim);
set(handles.edit2,'String',lim);
set(handles.edit3,'String',lim);
% --- Executes on button press in pb1.
function pb1_Callback(hObject, eventdata,
handles)
% hObject handle to pb1 (see GCBO)
% eventdata reserved - to be defined in a future
version of MATLAB
% handles structure with handles and user data
(see GUIDATA)
global k IO
axes(handles.axes1)
Iniciar/Detener %y1=[];
for k=1:1:handles.Xmax
handles.ejex(k,1)=k;
y1(k,1)=readVoltage(IO,'A0');
%y1(k,1)=(5/1023)*IO.analogRead(0)
plot(handles.ejex,y1,'LineWidth',2);grid on;
axis([0 handles.Xmax handles.Ymin
handles.Ymax]);
pause(0.01);
end
% --- Executes on button press in pButtonSal.
function pButtonSal_Callback(hObject, eventdata,
handles)
% hObject handle to pButtonSal (see GCBO)
% eventdata reserved - to be defined in a future
version of MATLAB
% handles structure with handles and user data
(see GUIDATA)
Salir opc = questdlg('¿Desea Salir del
Programa?','Salir','Si','No','No');
if (strcmp(opc,'No'))
return;
end
clear IO
clc; close all;
% --- Executes when user attempts to close
figure1.
function figure1_CloseRequestFcn(hObject,
eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future
version of MATLAB
% handles structure with handles and user data
(see GUIDATA)
Close Function
% Hint: delete(hObject) closes the figure
opc = questdlg('¿Desea salir del
programa?','Salir','Si','No','No');
if (strcmp('No',opc))
return;
else
clear IO
delete(hObject);
end

V. Cuestionario Final
V.1 Manejo de salidas digitales. Elabore un GUIDE para encender simular un
semáforo.
a. Elabore diseño GUIDE.
b. Coloque le programa de los componentes programados, separelos
por componente.
% --- Executes just before
DelgadoVicente_RojasJohn_Semaforo_G05 is made visible.
function
DelgadoVicente_RojasJohn_Semaforo_G05_OpeningFcn(hObject,
eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version
of MATLAB
% handles structure with handles and user data (see
GUIDATA)
% varargin command line arguments to
DelgadoVicente_RojasJohn_Semaforo_G05 (see VARARGIN)
Opening handles.a=arduino;
set (handles.eTextPue,'String',handles.a.Port);
Function %Capturando puesrto COMX
set (handles.eTextTar,'String',handles.a.Board);
set (handles.pMenuPdig1,'String',strseq('D',2:13));
set (handles.pMenuPdig2,'String',strseq('D',3:13));
set (handles.pMenuPdig3,'String',strseq('D',4:13));
handles.opcion1='D2';
handles.opcion2='D3';
handles.opcion3='D4';
% Choose default command line output for
DelgadoVicente_RojasJohn_Semaforo_G05
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Executes on selection change in pMenuPdig1.
function pMenuPdig1_Callback(hObject, eventdata, handles)
% hObject handle to pMenuPdig1 (see GCBO)
% eventdata reserved - to be defined in a future version
of MATLAB
% handles structure with handles and user data (see
GUIDATA)

Pin 1 % Hints: contents = cellstr(get(hObject,'String'))


returns pMenuPdig1 contents as cell array
% contents{get(hObject,'Value')} returns selected
item from pMenuPdig1
lista1=get(hObject,'String');
pin1=get(hObject,'Value');
handles.opcion1=lista1{pin1};
guidata(hObject,handles);
% --- Executes on selection change in pMenuPdig2.
function pMenuPdig2_Callback(hObject, eventdata, handles)
% hObject handle to pMenuPdig2 (see GCBO)
% eventdata reserved - to be defined in a future version
of MATLAB
% handles structure with handles and user data (see
GUIDATA)

Pin 2 % Hints: contents = cellstr(get(hObject,'String'))


returns pMenuPdig2 contents as cell array
% contents{get(hObject,'Value')} returns selected
item from pMenuPdig2
lista2=get(hObject,'String');
pin2=get(hObject,'Value');
handles.opcion2=lista2{pin2};
guidata(hObject,handles);
% --- Executes on selection change in pMenuPdig3.
function pMenuPdig3_Callback(hObject, eventdata, handles)
% hObject handle to pMenuPdig3 (see GCBO)
% eventdata reserved - to be defined in a future version
of MATLAB
% handles structure with handles and user data (see
GUIDATA)

Pin 3 % Hints: contents = cellstr(get(hObject,'String'))


returns pMenuPdig3 contents as cell array
% contents{get(hObject,'Value')} returns selected
item from pMenuPdig3
lista3=get(hObject,'String');
pin3=get(hObject,'Value');
handles.opcion3=lista3{pin3};
guidata(hObject,handles);
% --- Executes on button press in tButtonOnOff.
function tButtonOnOff_Callback(hObject, eventdata,
handles)
% hObject handle to tButtonOnOff (see GCBO)
% eventdata reserved - to be defined in a future version
of MATLAB
% handles structure with handles and user data (see
GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of


tButtonOnOff
%Value = 0 ---> si ha sido presionado nuevamente
%Value = 1 ---> Si ha sido presionado
estado=get(hObject,'Value');
r=str2num(get(handles.eTextRep,'String')); %Número de
repeticiones
p=str2num(get(handles.eTextPau,'String')); %Tiempo
de pausa entre cada repetición
e=str2num(get(handles.eTextEnc,'String')); %Tiempo de
encendido
if estado == 1
%INICIO DE CICLO SEMAFORO
for i=1:r
Iniciar set(handles.tButtonOnOff,'String','Detener
Secuencia Secuencia');
writeDigitalPin(handles.a,handles.opcion1,1);
%Prender el LED
pause(e);
%Mantener prendido el LED el tiempo especificado
writeDigitalPin(handles.a,handles.opcion1,0);
writeDigitalPin(handles.a,handles.opcion2,1);
pause(e);
%Mantener prendido el LED el tiempo especificado
writeDigitalPin(handles.a,handles.opcion2,0);
writeDigitalPin(handles.a,handles.opcion3,1);
pause(e);
writeDigitalPin(handles.a,handles.opcion3,0);
pause(p); %Pausa
antes de prender de nuevo
end
else
writeDigitalPin(handles.a,handles.opcion1,0);
writeDigitalPin(handles.a,handles.opcion2,0);
writeDigitalPin(handles.a,handles.opcion3,0);
set(handles.tButtonOnOff,'String','Iniciar
Secuencia');
end
% --- Executes on button press in pButtonSal.
function pButtonSal_Callback(hObject, eventdata, handles)
% hObject handle to pButtonSal (see GCBO)
% eventdata reserved - to be defined in a future version
of MATLAB
% handles structure with handles and user data (see
GUIDATA)
Salir opc = questdlg('¿Desea Salir del
Programa?','Salir','Si','No','No');
if (strcmp(opc,'No'))
return;
end
clear handles.a
clc; clear all; close all;
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata,
handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version
Close of MATLAB
Function % handles structure with handles and user data (see
GUIDATA)
clear handles.a
% Hint: delete(hObject) closes the figure
delete(hObject);

V.2 Según el programa desarrollado en clase para el manejo de entradas digitales,


elabore un GUIDE para que presionando un botón se active una alarma sonora,
si en caso dejara de presionar el pulsador, la alarma sonora se desactivará.
a. Elabore diseño GUIDE.

b. Coloque le programa de los componentes programados, separelos


por componente.
% --- Executes just before
Opening Function DelgadoV_RojasJ_BuzG05 is made visible.
function
DelgadoV_RojasJ_BuzG05_OpeningFcn(hObject,
eventdata, handles, varargin)
% This function has no output args, see
OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
% varargin command line arguments to
DelgadoV_RojasJ_BuzG05 (see VARARGIN)
handles.a = arduino
set (handles.eTextPue,'String',handles.a.Port);
%Capturando puesrto COMX
set
(handles.eTextTar,'String',handles.a.Board);
set
(handles.pMenuPdig1,'String',strseq('D',8:13));
set
(handles.pMenuPdig2,'String',strseq('D',9:13));
handles.opcion1='D8';
handles.opcion2='D9';
% Choose default command line output for
DelgadoV_RojasJ_BuzG05
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Executes on selection change in
pMenuPdig1.
function pMenuPdig1_Callback(hObject,
eventdata, handles)
% hObject handle to pMenuPdig1 (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
Pin 1
% Hints: contents =
cellstr(get(hObject,'String')) returns
pMenuPdig1 contents as cell array
% contents{get(hObject,'Value')} returns
selected item from pMenuPdig1
lista1=get(hObject,'String');
pin1=get(hObject,'Value');
handles.opcion1=lista1{pin1};
guidata(hObject,handles);
% --- Executes on selection change in
Pin 2 pMenuPdig2.
function pMenuPdig2_Callback(hObject,
eventdata, handles)
% hObject handle to pMenuPdig2 (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)

% Hints: contents =
cellstr(get(hObject,'String')) returns
pMenuPdig2 contents as cell array
% contents{get(hObject,'Value')} returns
selected item from pMenuPdig2
lista2=get(hObject,'String');
pin2=get(hObject,'Value');
handles.opcion2=lista2{pin2};
guidata(hObject,handles);
% --- Executes on button press in tButtonOnOff.
function tButtonOnOff_Callback(hObject,
eventdata, handles)
% hObject handle to tButtonOnOff (see GCBO)
% eventdata reserved - to be defined in a
future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
estado=get(hObject,'Value');
pin = 'D8'
configurePin(handles.a,pin,'DigitalInput');
while(1)
Iniciar estado = readDigitalPin(handles.a,'D8');
disp(estado);
if(estado==0)
writeDigitalPin(handles.a,handles.opcion2,1);

set(handles.tButtonOnOff,'String','Detener');
else
writeDigitalPin(handles.a,handles.opcion2,0);

set(handles.tButtonOnOff,'String','Iniciar');
end
end
% --- Executes when user attempts to close
figure1.
function figure1_CloseRequestFcn(hObject,
eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a
Close Function future version of MATLAB
% handles structure with handles and user
data (see GUIDATA)
clear handles.a
% Hint: delete(hObject) closes the figure
delete(hObject);

VI. Conclusiones (Realice por lo menos 4 conclusiones)


 Es necesario tener claro cada comando aplicado al lenguaje de
programación compatibles entre Arduino y Matlab ya que existen varios
que a la fecha ya no son aplicables.
 Es necesario generación un CloseRequestFcn ya que este nos permitirá
borrar completamente el programa finalizado al terminar de
desconectarlo en el Arduino, de forma contrario nuestra placa de
pruebas, así como el Arduino seguirán funcionando, lo que genera
problemas en una siguiente prueba.
 Importante conoces la forma de conexión centre el Arduino uno y la
placa de pruebas ya que esto nos facilitará crear el programa de forma
adecuada para que no genere problemas, el ejemplo más claro se tiene
en el ejercicio del pulsador con alarma ya que sin el siguiente esquema
no se podría haber desarrollado el programa:

 Es importante la configuración icial de variables globales ya que


mediante estas es que finalmente podremos hacer el reconocimiento
del hardware ARDUINO uno para el uso compatible con Matlab. De igual
manera, se debe contar con una versión compatible con el HARDWARE
ARDUINO UNO, ya que se demostró que la versión 2017a en especial no
fue compatible por lo que fue necesario instalar una versión más
reciente del programa MATLAB.

También podría gustarte