File

advertisement
Guide for Constructing Flow Meter
Compiled By: Jonathan Ma
Mario Zuniga
Yung Nguyen
Table of Contents
CODING MASTERSHEET ......................................................................................................................... 3
1. MainCode.............................................................................................................................................. 3
2. Buttons .................................................................................................................................................. 5
3. Conversions........................................................................................................................................... 6
4. LCDScreen............................................................................................................................................ 6
5. SensorDisplay ....................................................................................................................................... 7
6. Sensors .................................................................................................................................................. 8
INDIVIDUAL COMPONENT INSTALLATIONS ................................................................................... 10
1. Installing Temperature Sensor and Coding:........................................................................................ 10
2. Installing Push Buttons: ...................................................................................................................... 12
3. Installing Power Switch: ..................................................................................................................... 12
4. Installing and Configuring LCD Screen: ............................................................................................ 13
5. Differential Pressure Sensor................................................................................................................ 15
6. MEMs Sensor...................................................................................................................................... 16
7. Absolute Pressure Sensor .................................................................................................................... 16
8. Thermistor ........................................................................................................................................... 17
2
CODING MASTERSHEET
These are the coding accomplished in the flow meter. Each section is representative of each tab in the
Arduino IDE interface.
1. MainCode
/*
MAE 126B Course Project E5 - Handheld Flow Meter (JYMeter) for flow rates < 1L/min
Written by: Jonathan, Yung, and Mario
*/
//---------------------------Declaring Pins and Variables-----------------------------#include <LiquidCrystal.h>
#include <math.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);//defines the pins for the lcd
int backLight = 13; // pin 13 controls the backlight
int temperaturePin = A0; //the TMP36 (temperature sensor) reading
//the resolution is 10 mV / degree centigrade
//(500 mV offset) to make negative temperatures an option
/*
int WsensorPin= A1; //wind Sensor velocity reading
int WTsensorPin=A2; //wind sensor temperature reading
*/
int humidsensPin=A3; //humidity sensor reading
int diffp=A4;
//diff. pressure reading
int massPin=A5;
//mass flow
int abspressPin=A2;
int holdbutton=9, switchbutton=7, resetbutton=8;
float W=0, WT=0, averageW = 0, averageWT = 0;
float humid=0, averagehumid=0;
float abspress=0, averageabspress=0;
float temperatureF=0, temperatureC=0, averageAtemp=0; //initialize value for temperature
float Wsensor=0, Wind=0;
//initialize values for wind sensors
float WTsensor = 0;
// initialize wind sensor temperature value
float humidval = 0;
// initialize humidity value
float average=0;
float diffpress = 0, averagediffpress = 0;
float mass=0, averagemass=0;
float averageflow=0, flow=0;
float
oversampleAtemp=0,oversamplehumid=0,oversampleWT=0,oversampleW=0,oversamplediffpress=0,ove
rsamplemass=0;
float oversampleflow=0,oversampleabspress=0;
int holdval=0, butval=0, resetval=0;//initialize values for buttons
int signal=1; // signal is used to tell the system when to switch or reset
int counter=0; //counts the number of times measurements have been taken
int dcount=0, printcount=0, disp=0, overcount=0;
//constants for calculating flow rates from differential pressure
3
double miu=1.789*0.00001; // miu for air under standard conditions
double L=.16,diameter=.055;
int choice = 1, oversamplenumber=50;
int sensorswitch = 2; //how many switching features are available
//------------------------------------------------------------------------------------void setup(){
Serial.begin(9600);
//begins the Arduino's built in serial monitor
startup();
//starts the system
}
void loop(){
holdval=digitalRead(holdbutton);
if(printcount<0 && holdval==1 || printcount>999){
lcd.clear();
lcd.print("Warning:counter broke");
lcd.setCursor(0,1);
lcd.print("Please reset");
while(resetval!=1){
resetval = resetcheck();
delay(100);
}
}
templatecheck();
if (holdval==1){
dcount++;
disp = dcount % oversamplenumber;
counter++; overcount++;
getallnumbers();
if(disp==(oversamplenumber-1)){
printcount++;
if(choice==1){
dispmass();
dispdp();
dispflow();
}
else if(choice==2){
disptemp();
disphumid();
}
lcd.setCursor(16,3);
lcd.print("#");
lcd.print(printcount);
overcount=0;
}
}
checkfunc();
}
4
2. Buttons
int buttoncheck(){ //checks to see if button is pressed
butval=digitalRead(switchbutton); //reads whether the button is pressed)
if (butval==1){
signal=1;
while(butval==1){
butval=digitalRead(switchbutton);
delay(50);
}
if(choice<sensorswitch){
choice++;
}
else{
choice=1;
}
}
else signal=0;
butval=0;
return signal;
}
int resetcheck(){
resetval=digitalRead(resetbutton); //reads whether the button is pressed)
if (resetval==1){
while(resetval==1){
resetval=digitalRead(resetbutton);
delay(50);
}
resetval=1;
resetallvalues();
}
return signal;
}
void resetallvalues(){
W=0, WT=0, averageW = 0, averageWT = 0;
humid=0, averagehumid=0, abspress=0;
temperatureF=0, temperatureC=0, averageAtemp=0; //initialize value for temperature
averageflow=0,averagemass=0;averagediffpress=0,averageabspress=0;
flow=0;
Wsensor=0, Wind=0;
//initialize values for wind sensors
WTsensor = 0;
// initialize wind sensor temperature value
humidval = 0;
// initialize humidity value
average=0; mass=0;
counter=0;
signal=1;
dcount=0, printcount=0, disp=0; //counts the number of times measurements have been taken
oversampleAtemp=0,oversamplehumid=0,oversampleWT=0,oversampleW=0,oversamplediffpress=0;
oversamplemass=0,oversampleflow=0,oversampleabspress=0;
overcount=0;
5
}
3. Conversions
float getvoltage(float pin){ //converts the analog input into a digital reading
return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}
float inchtometer(float inches){
return (inches*.0254);
}
float m3pstoLps(float m3ps){
return (m3ps*1000.0);
}
float inh20toPa(float inh20){
return (inh20*248.8);
}
4. LCDScreen
// Connections for non serial LCD:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
void startup()
{
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
lcd.begin(20,4);
// lets the arduino how many rows and columns the lcd has
pinMode(holdbutton,INPUT);//declares button as digital input
pinMode(switchbutton,INPUT);
pinMode(resetbutton,INPUT);
}
void templatecheck(){
if(signal == 1 || resetval == 1){
lcd.clear();
if(choice==2){
lcd.setCursor(0,0);
lcd.print("T(C)=");
lcd.setCursor(13,0);
lcd.print("A=");
lcd.setCursor(0,1);
lcd.print("Hum(%)=");
lcd.setCursor(13,1);
6
lcd.print("A=");
lcd.setCursor(0,2);
lcd.print("abs.press.(bar)=");
lcd.setCursor(0,3);
lcd.print("A=");
}
else if (choice==1){
lcd.setCursor(0,1);
lcd.print("dP=");
lcd.setCursor(13,1);
lcd.print("A=");
lcd.setCursor(0,0);
lcd.print("FR(L/m)=");
lcd.setCursor(13,0);
lcd.print("A=");
lcd.setCursor(0,2);
lcd.print("Mass=");
lcd.setCursor(12,2);
lcd.print("A=");
}
//print previous values
if(holdval==0){
if(choice==1){
dispmass();
dispdp();
dispflow();
}
else if(choice==2){
disptemp();
disphumid();
dispabspress();
}
lcd.setCursor(16,3);
lcd.print("#");
lcd.print(printcount);
}
if(holdval==1)
resetoversample();
}
}
void checkfunc(){
resetval = resetcheck();
signal = buttoncheck();
}
5. SensorDisplay
void disptemp(){
lcd.setCursor(7,0);
lcd.print(oversampleAtemp);
7
lcd.setCursor(15,0);
lcd.print(averageAtemp);
}
void disphumid(){
lcd.setCursor(7,1);
lcd.print(oversamplehumid);
lcd.setCursor(15,1);
lcd.print(averagehumid);
}
void dispdp(){
lcd.setCursor(6,1);
lcd.print(oversamplediffpress);
lcd.setCursor(15,1);
lcd.print(averagediffpress);
Serial.println(oversamplediffpress);
}
void dispmass(){
lcd.setCursor(6,2);
lcd.print(oversamplemass);
lcd.setCursor(15,2);
lcd.print(averagemass);
}
void dispflow(){
lcd.setCursor(8,0);
lcd.print(oversampleflow);
lcd.setCursor(15,0);
lcd.print(averageflow);
}
void dispabspress(){
lcd.setCursor(10,3);
lcd.print(oversampleabspress);
lcd.setCursor(2,3);
lcd.print(averageabspress);
}
void resetoversample(){
oversampleAtemp=0,oversamplehumid=0,oversampleWT=0,oversampleW=0,oversamplediffpress=0;
oversamplemass=0,oversampleflow=0,oversampleabspress=0;
overcount=0;
}
6. Sensors
float getAtemp() {
double Temp;
double RawADC = getvoltage(temperaturePin);
8
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15;
// Convert Kelvin to Celcius
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}
float humidsens(float temperatureC){
float humidval = getvoltage(humidsensPin);
humidval = (((humidval)/5)-0.16)/0.0062;
humidval = humidval/(1.0546-0.00216*temperatureC);
return humidval;
}
float getdiffp(){
float diffpress = 10*getvoltage(diffp);
diffpress=32.709*pow(diffpress,3)-174.18*pow(diffpress,2)+609.13*diffpress+3.5698;
return diffpress;
}
float getmass(){
float mass=analogRead(massPin);
float mass = getvoltage(massPin);
mass= -.47367+mass;
if(mass<1.566)
mass = 55.461*pow(mass,5)-156.12*pow(mass,4)+205.6*pow(mass,3)98.81*(pow(mass,2))+216.33*mass-1.0278;
else
mass= 118.17*pow(mass,4)-266.58*pow(mass,3)+229.78*pow(mass,2)+138.38*mass+1.3026;
return mass;
}
float getflow(float diffpress){
diffpress = inh20toPa(diffpress);
float flow = (diffpress*(PI*pow(diameter,4.0)))/(L*128.0*miu);
Serial.println(flow);
flow = m3pstoLps(flow);
Serial.println(flow);
return flow;
}
float getabspress(){
abspress = 10*getvoltage(abspressPin);
return abspress;
}
float averager(float prevaverage, float currentvalue, int counter){
float average = (((counter-1)*prevaverage)+(currentvalue))/(counter);
return average;
}
9
void getallnumbers(){
temperatureC=getAtemp();
averageAtemp = averager(averageAtemp,temperatureC,counter);
oversampleAtemp=averager(oversampleAtemp,temperatureC,overcount);
humid = humidsens(temperatureC);
averagehumid = averager(averagehumid,humid,counter);
oversamplehumid=averager(oversamplehumid,humid,overcount);
diffpress = getdiffp();
averagediffpress = averager(averagediffpress,diffpress,counter);
oversamplediffpress=averager(oversamplediffpress,diffpress,overcount);
flow = getflow(diffpress);
averageflow = averager(averageflow,flow,counter);
oversampleflow=averager(oversampleflow,flow,overcount);
mass = getmass();
averagemass = averager(averagemass,mass,counter);
oversamplemass=averager(oversamplemass,mass,overcount);
abspress = getabspress();
averageabspress = averager(averageabspress,abspress,counter);
oversampleabspress = averager(oversampleabspress,abspress,overcount);
}
INDIVIDUAL COMPONENT INSTALLATIONS
1. Installing Temperature Sensor and Coding:
A. References:
http://learn.adafruit.com/tmp36-temperature-sensor
http://www.instructables.com/id/Arduino-Expermentation-Kit-How-to-get-Startedwi/step12/Temperature-TMP36-Precision-Temperature-Senso/
B. Quick Guide:
Description: With flat end facing toward you, the rightmost pin is ground, the center is the analog output
can be connected to any of the Arduino’s input terminals, and the leftmost pin to the 5V. No other
10
components are necessary. Connect them accordingly to the Arduino and run the following code using the
Arduino IDE:
C. Code:
/* --------------------------------------------------------* | Arduino Experimentation Kit Example Code
|
* | CIRC-10 .: Temperature :. (TMP36 Temperature Sensor) |
* --------------------------------------------------------*
* A simple program to output the current temperature to the IDE's debug window
*
* For more details on this circuit: http://tinyurl.com/c89tvd
*/
//TMP36 Pin Variables
int temperaturePin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade
//(500 mV offset) to make negative temperatures an option
float temperatureF=0;
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(9600); //Start the serial connection with the copmuter
//to view the result open the serial monitor
//last button beneath the file bar (looks like a box with an antenae)
}
void loop()
// run over and over again
{
float temperatureC = getVoltage(temperaturePin); //getting the voltage reading from the temperature
sensor
temperatureC = (temperatureC - .5) * 100;
//converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.print("Temperature(C): ");
//prints in Fahrenheit
Serial.println(temperatureC);
temperatureF = temperatureC*(1.8) + 32;
Serial.print("Temperature(F): ");
//prints in Fahrenheit
Serial.println(temperatureF);
//printing the result
Serial.println("");
delay(1000);
//waiting a second
}
/*
* getVoltage() - returns the voltage on the analog input defined by
* pin
*/
float getVoltage(int pin){
11
return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}
2. Installing Push Buttons:
A. References:
http://arduino.cc/en/tutorial/button
http://www.ladyada.net/learn/arduino/lesson5.html
B. Quick Guide:
When installing push buttons, be sure
to have a resistor handy. The switch has
minimal resistance that will allow a dangerous
amount of current to flow without additional
resistance. Any resistor should be fine, but a 1
KΩ or higher resistance is generally used. The
legs are connected in parallel, so that the legs
pointing away from each other are also
connected. Five volts would be connected to
one leg, the adjacent leg would be connected to
digital read pin on the Arduino and the resistor
chosen. The resistor is then connected to
ground.
C. Code:
int buttoncheck(){ //checks to see if button is pressed
butval=digitalRead(switchbutton); //reads whether the button is pressed)
if (butval==1){
signal=1;
while(butval==1){
butval=digitalRead(switchbutton);
delay(50);
}
if(choice<sensorswitch){
choice++;
}
else{
choice=1;
}
}
else signal=0;
butval=0;
return signal;
}
3. Installing Power Switch:
A. Reference:
http://www.instructables.com/id/Powering-Arduino-with-a-Battery/
12
B. Quick Guide:
Description: The power switch used is shown above. In order to install this power switch, the 9V battery
jack connector has to be cut so that the wires can be soldered on.
C. Code:
None Applicable
4. Installing and Configuring LCD Screen:
A. References:
http://homepage.hispeed.ch/peterfleury/avr-lcd44780.html
http://mbed.org/cookbook/Text-LCD
http://www.hacktronics.com/Tutorials/arduino-character-lcd-tutorial.html
B. Quick Guide:
13
Description: The pin connections for the LCD screen are shown on the left. The pins to the Arduino can
be changed, but the ones shown were the pins used from the online tutorials from hacktronics.
Below is an modified example code from hacktronics to be run on the LCD, it counts the number of
seconds that have gone by.
/* ------------------------------------------------------------------------------- */
// character LCD example code
// www.hacktronics.com
#include <LiquidCrystal.h>
// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 13; // pin 13 will control the backlight
int i=0;
void setup()
{
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
lcd.begin(20,4);
// columns, rows. use 16,2 for a 16x2 LCD, etc.
14
lcd.clear();
// start with a blank screen
lcd.setCursor(0,0);
// set cursor to column 0, row 0 (the first row)
lcd.print("Hello, World"); // change this text to whatever you like. keep it clean.
lcd.setCursor(0,1);
// set cursor to column 0, row 1
lcd.print("This is Jammy from");
// if you have a 4 row LCD, uncomment these lines to write to the bottom rows
// and change the lcd.begin() statement above.
lcd.setCursor(5,2);
// set cursor to column 0, row 2
lcd.print("Team Trio");
lcd.setCursor(6,3);
// set cursor to column 0, row 3
lcd.print("Welcome");
delay(5000);
}
void loop()
{
lcd.clear();
lcd.print("How many seconds");
lcd.setCursor(0,1);
lcd.print("have gone by?");
lcd.setCursor(0,2);
lcd.print(i);
lcd.print(" seconds");
lcd.setCursor(0,3);
lcd.print("Shame Shame");
i++;
delay(1000);
}
/* ------------------------------------------------------------------------------- */
5. Differential Pressure Sensor
References: http://www.meas-spec.com/downloads/MS4515.pdf
Description: The connections to the differential pressure sensors are
shown above. No additional components are necessary. Supply
voltage is 5V from the Arduino. The output would be connected to
the analog pins on the Arduino.
Code:
float getdiffp(){
float diffpress = getvoltage(diffp);
diffpress=diffpress-2.4175;
diffpress=32.709*pow(diffpress,3)174.18*pow(diffpress,2)+609.13*diffpress+3.5698;
//using cubic fit on matlab
return diffpress;
}
15
6. MEMs Sensor
References:
http://www.components.omron.com/components/web/pdflib.nsf/0/49DF8846996E7D0E86257385006E0
43E/$file/D6F_P_1010.pdf
Description: The connections are shown
on the figure in the left. No additional
hardware components need to be
connected. Vcc is the supply voltage 5V
from the Arduino and the Vout is
connected to the analog pin on the
Arduino.
Code:
float getmass(){ float mass = getvoltage(massPin);
mass= -.47367+mass;
if(mass<1.566)
mass = 55.461*pow(mass,5)-156.12*pow(mass,4)+205.6*pow(mass,3)98.81*(pow(mass,2))+216.33*mass-1.0278;
else
mass = 118.17*pow(mass,4)266.58*pow(mass,3)+229.78*pow(mass,2)+138.38*mass+1.3026;
return mass;
}
7. Absolute Pressure Sensor
References: http://www.freescale.com/files/sensors/doc/data_sheet/MPXH6300A.pdf
Description: The connections for the absolute pressure sensor are shown in the figure on the top left and
the relative pin locations are shown on the top right. No additional hardware components need to be
connected. The supply voltage is 5V and the output voltage is connected to an analog pin on the Arduino
C Code:
16
To be completed
8. Thermistor
References: http://playground.arduino.cc/ComponentLib/Thermistor
https://www.sparkfun.com/products/250
Description: The wiring schematic for the 10K thermistor is shown above. Be sure to use a 10KΩ The
pins are reversible on the thermistor, so 5V can be supplied to any of the two prongs. The Analog In
connection in the diagram will be connected to an analog pin in the Arduino.
C Code:
float getAtemp() {
double Temp;
double RawADC = analogRead(temperaturePin);
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))*
Temp );
Temp = Temp - 273.15;
// Convert Kelvin to Celcius
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}
17
Download