Documentos de Académico
Documentos de Profesional
Documentos de Cultura
POLITCNICA
NACIONAL
Prctica N6
Preparatorio
TEMA: Sistema de deteccin de nivel de agua utilizando comunicacin serial.
19
04
2015
19
04
2015
Preparatorio N 6
Hctor Naranjo, Diego Len
ESCUELA POLITCNICA NACIONAL,
Laboratorio de Comunicacin Digital
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.
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
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
% varargout
VARARGOUT);
% hObject
% eventdata
MATLAB
% handles
10