Está en la página 1de 10

ESCUELA

POLITCNICA
NACIONAL

Prctica N6
Preparatorio
TEMA: Sistema de deteccin de nivel de agua utilizando comunicacin serial.

Alumnos: Diego Len, Hctor Naranjo

19

04

2015

19

04

2015

Preparatorio N 6
Hctor Naranjo, Diego Len
ESCUELA POLITCNICA NACIONAL,
Laboratorio de Comunicacin Digital

Tema: Sistema de deteccin de nivel de agua utilizando comunicacin serial.


Objetivo:

Utilizar los conocimientos de la comunicacin serial asincrnica, adquirida en la teora para


realizar un sistema de deteccin de nivel de agua a travs del puerto serie de un PC.

Preparatorio:
1. Realizar un sistema que permita detectar el nivel de agua de un recipiente, utilizando un
convertidor analgico digital y un computador. El sistema debe constar de:

Un sensor de nivel de agua que permita tomar datos para ser enviados a un
convertidor analgico digital (adc0804 o similar) cuyas salidas deben convertirse en serie
para poder ser enviado al puerto serial RS-232 del computador.

Desarrollar en un lenguaje visual (GUI de MATLAB, Visual Basic, etc) una interfaz
que indique el nivel de agua del recipiente (bajo, medio o alto), si el nivel de agua desciende
por debajo del nivel bajo, la interfaz debe enviar una seal para encender un led de color
rojo, si el nivel de agua sobrepasa el nivel alto se debe enviar una seal para encender un
led de color verde, adems la interfaz debe permitir apagar los leds desde el computador.

Figura 1. Diagrama de Bloques del circuito

El trabajo preparatorio deber incluir el diseo y justificacin del sistema a


implementarse, as como el diagrama de conexiones del sistema y su respectiva simulacin.
NOTA: Se debe utilizar el CI. MAX232 para la conversin de voltajes de RS232 a TTL y
viceversa.
2

RV1(2)

89%

RV1

1k

D1
LED-BIGY

D2
LED-BIGY

RXD
TXD
RTS

PD4/T0/XCK
~ PD3/INT1
PD2/INT0
TX PD1/TXD
RX PD0/RXD

7
6
5
4
3
2
1
0
PD7/AIN1

~ PD6/AIN0
PD5/T1
~

PB5/SCK
PB4/MISO
~PB3/MOSI/OC2A
~ PB2/SS/OC1B
~ PB1/OC1A
PB0/ICP1/CLKO

AREF

13
12
11
10
9
8

CTS

ARD1
ARDUINO UNO R3

DIGITAL (~PWM)

PC0/ADC0
PC1/ADC1
PC2/ADC2
PC3/ADC3
PC4/ADC4/SDA
PC5/ADC5/SCL

1121
ATMEGA328P-PU

1
6
2
7
3
8
4
9

DCD
DSR
RXD
RTS
TXD
CTS
DTR
RI

ERROR
COMPIM

A0
A1
A2
A3
A4
A5

RESET

ANALOG IN

P1

Figura.1. Circuito en Proteus

Cdigo Arduino
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1);
int led1 = 6;
int led2 = 5;
int vol = 0;
int valor=A0;
int dato;
void setup()
{
// pines 5 y 6 como salidas
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);

Serial.begin(9600);
}

void loop(){
//lectura del valor analogico del sensor
vol = analogRead(valor);
//transformacion del valor obtenido del sensor
float val = (vol * 5) / 1024.0;
//conversion a 8 bits (256 valores)
int val2=(val*255/5);
//imprime los valores
Serial.println(val2);
//condicion para encendido y apagado de los leds
//entre 1 y 4 volt estan apagados
if (dato=='0'){
if (val>1 && val<4) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
//a menos de 1 volt se enciende el led2
if (val<1)
{
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
}
//a mas de 4 volt se enciende el led1
if (val>4)
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}
}
if (dato=='1'){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
delay (100) ;
}

void serialEvent()
{
if (Serial.available()){
dato=Serial.read();
}
}

Cdigo MATLAB
function varargout = nivelagua(varargin)
% NIVELAGUA MATLAB code for nivelagua.fig
%
NIVELAGUA, by itself, creates a new NIVELAGUA or raises
the existing
%
singleton*.
%
%
H = NIVELAGUA returns the handle to a new NIVELAGUA or the
handle to
%
the existing singleton*.
%
%
NIVELAGUA('CALLBACK',hObject,eventData,handles,...) calls
the local
%
function named CALLBACK in NIVELAGUA.M with the given
input arguments.
%
%
NIVELAGUA('Property','Value',...) creates a new NIVELAGUA
or raises the
%
existing singleton*. Starting from the left, property
value pairs are
%
applied to the GUI before nivelagua_OpeningFcn gets
called. An
%
unrecognized property name or invalid value makes property
application
%
stop. All inputs are passed to nivelagua_OpeningFcn via
varargin.
%
%
*See GUI Options on GUIDE's Tools menu. Choose "GUI
allows only one
%
instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help nivelagua
5

% Last Modified by GUIDE v2.5 18-May-2015 12:07:37


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @nivelagua_OpeningFcn, ...
'gui_OutputFcn', @nivelagua_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback',
[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before nivelagua is made visible.
function nivelagua_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 nivelagua (see VARARGIN)
% Choose default command line output for nivelagua
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes nivelagua wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command
line.
function varargout = nivelagua_OutputFcn(hObject, eventdata,
handles)
6

% varargout
VARARGOUT);
% hObject
% eventdata
MATLAB
% handles

cell array for returning output args (see


handle to figure
reserved - to be defined in a future version of
structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;
% --- Executes on button press in evaluar.
function evaluar_Callback(hObject, eventdata, handles)
% hObject
handle to evaluar (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles
structure with handles and user data (see GUIDATA)
global s
s=serial('COM2'); %configuracion del puerto serial y los
parametros de comunicacion
set(s,'BaudRate',9600);
set(s,'Parity','None');
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'FlowControl','none'); %configuracion sin handshake
fopen(s); %abro el puerto
global x
global datos
x=0;
flushoutput(s);
fprintf(s,'0');
datos=fscanf(s); %lectura de los datos que llegan
set(handles.nivel,'string',datos);
% --- Executes on button press in apagar.
function apagar_Callback(hObject, eventdata, handles)
% hObject
handle to apagar (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles
structure with handles and user data (see GUIDATA)
global s
flushoutput(s);
fprintf(s,'1');
fclose(s);%cierro el puerto
delete(s)
7

% --- Executes on button press in parar.


function parar_Callback(hObject, eventdata, handles)
% hObject
handle to parar (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles
structure with handles and user data (see GUIDATA)
global x s
global datos
x=1;
datos=0;
fclose(s);%cierro el puerto
delete(s)
Simulacin

Figura.2. Prueba cuando rebasa el nivel superior

Figura.3. Apaga el led de nivel superior

Figura.4. Prueba en un nivel intermedio

Figura.5. Prueba cuando rebasa el nivel inferior

Figura.6. Apaga el led de nivel inferior

10

También podría gustarte