Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
138 views

Gas Sensor + Arduino Experimental Set Up

This thesis describes the development of a gas data acquisition system using an Arduino microcontroller. The system is part of a larger electronic nose project that includes mechanical, thermal, electrical, electronic and software components. The Arduino collects data from gas sensors and sends it to a computer to be analyzed using MatLab software. The thesis discusses the previous work on the mechanical and thermal parts. It then describes the electrical circuit, sensor calibration process, statistical analysis methods, and software programs developed to control the sensors, display readings, and transmit data for analysis. The results demonstrate the ability of the system to differentiate between different types of gases.

Uploaded by

Alas Titu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

Gas Sensor + Arduino Experimental Set Up

This thesis describes the development of a gas data acquisition system using an Arduino microcontroller. The system is part of a larger electronic nose project that includes mechanical, thermal, electrical, electronic and software components. The Arduino collects data from gas sensors and sends it to a computer to be analyzed using MatLab software. The thesis discusses the previous work on the mechanical and thermal parts. It then describes the electrical circuit, sensor calibration process, statistical analysis methods, and software programs developed to control the sensors, display readings, and transmit data for analysis. The results demonstrate the ability of the system to differentiate between different types of gases.

Uploaded by

Alas Titu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

FACULTY OF ENGINEERING AND SUSTAINABLE DEVELOPMENT

Department of Electronics, Mathematics and Natural Sciences

Gas Data Acquisition


using Arduino

Lander Lejarza Lasuen


Gävle, June 2017

Bachelor Thesis in Electronics, 15 ECTS

Supervisor: Niklas Rothpfeffer

Examiner: José Chilo


ii

Abstract

The aim of this project is to acquire gas data using an Arduino microcontroller. This
project is part of a bigger project called The electronic nose where mechanical, thermal,
electrical, electronic and software parts work together. It is the continuation of a project
handled before where mainly the mechanical, thermal and electrical parts were done.

The whole e-Nose project is divided in 4 main subcategories: general mechanical


structure, thermal and piston electric circuit, gas sensors and software. My work will be
focused in the last two subcategories of the general project.

The sample to be measured is placed inside of a moving cylinder, which will lift
up the sample reaching near the sensors and warming it using a thermal resistance, to
release more odor. That is where the sensors act, synchronized with the piston, will get
the data through the Arduino and sends it to the computer to be analysed. The sensors
will be activated using an Arduino Mega 2560 and transferred to the computer to be
analysed with MatLab.

To control the measurement, a push button, a LCD display and a LED will be
placed; having like this a full control of the project and an easy interface for the user. Six
gas sensors will be used, which will be enough to be able to differentiate between
different kind of gases. With such variety it is possible to categorize between
combustible gas (methane, propane, LPG etc.), NH3, alcohol and more gases.

The e-Nose will be able to measure different gases in more than ways depending
on the program we choose. For a more accurate response, more sensors would be
needed using a sensor fusion method or more accurate sensors.
iii
Dedication

To my family and everyone who helped me in my career.


iv

Acknowledgements

I want to show my gratitude to all the people who helped, collaborated or guided me
during the development of this work. I start with my university professors, José Chilo
and Niklas Rothpfeffer, who guided me implementing the project and suggested me
different paths to find solutions to the problems that appeared during this process. I also
want to thank to all my mates who supported me when I asked them for help. This work
has been possible thanks to all your collaboration.
Table of Contents v

CHAPTER 1 ............................................................................................................................ 1
Introduction .......................................................................................................................... 1
1.1 Background ..........................................................................................................................1
1.2 Objective ...............................................................................................................................2
1.3 Outline ...................................................................................................................................3
CHAPTER 2 ............................................................................................................................ 4
Theory................................................................................................................................... 4
2.1 Electronics basics ...............................................................................................................4
2.1.1 Sensors ........................................................................................................................................ 4
2.1.2 Potentiometer ............................................................................................................................. 4
2.2 Arduino .................................................................................................................................6
2.2.1 Arduino Mega 2560 ..................................................................................................................... 6
2.2.2 Arduino Software ........................................................................................................................ 8
2.3 MatLab Software ..................................................................................................................9
2.4 PyEGG program ................................................................................................................. 10
2.5 Components of the project ............................................................................................... 10
2.5.1 Gas sensor ................................................................................................................................. 10
2.5.2 Shields ....................................................................................................................................... 16
2.5.3 Piston ......................................................................................................................................... 17
2.5.4 LCD............................................................................................................................................. 17
2.5.5 Buzzer ........................................................................................................................................ 18
CHAPTER 3 .......................................................................................................................... 19
Method and Results ............................................................................................................ 19
3.1 Previous Project ................................................................................................................ 20
3.1.1 Piston ......................................................................................................................................... 20
3.1.2 Thermal configuration ............................................................................................................... 21
3.1.3 User system ............................................................................................................................... 22
3.1.4 Software .................................................................................................................................... 24
3.2 Electric circuit .................................................................................................................... 25
3.3 Calibration .......................................................................................................................... 27
3.4 Statistical analysis ............................................................................................................. 29
3.5 Obtaining concentrations ................................................................................................. 36
3.6 Software .............................................................................................................................. 38
3.6.1 Declaration ................................................................................................................................ 38
3.6.2 Calibration ................................................................................................................................. 38
3.6.3 Programs ................................................................................................................................... 39
3.6.4 Functions ................................................................................................................................... 41
3.7 Results ................................................................................................................................ 43
CHAPTER 4 .......................................................................................................................... 46
Conclusions ......................................................................................................................... 46
4.1 Summary ............................................................................................................................. 46
4.2 Problems ............................................................................................................................. 46
vi
4.3 Future work ........................................................................................................................ 47
References .......................................................................................................................... 48
Appendix A.......................................................................................................................... 50
Appendix B .......................................................................................................................... 53
Appendix C .......................................................................................................................... 58
vii
List of Tables

Table 1: Arduino Uno specifications .............................................................................................. 6


Table 2: Arduino Mega 2560 specifications ................................................................................... 7
Table 3: RL for different sensors .................................................................................................. 27
Table 4: Calculating RO ............................................................................................................... 28
Table 5: Features of voltages readings.......................................................................................... 33
Table 6: Empirical data from manufacturers graph ...................................................................... 37
List of Figures viii

Figure 1: Schematic of potentiometer ............................................................................................. 5


Figure 2: Arduino Mega 2560......................................................................................................... 8
Figure 3: MQ series internal circuit .............................................................................................. 11
Figure 4: Vout changes in gas presence for MQ-6 ...................................................................... 12
Figure 5: Measurement dependence on humidity and temperature for MQ-2.............................. 13
Figure 6: Voltage divider .............................................................................................................. 13
Figure 7: Vout vs ppm .................................................................................................................. 14
Figure 8: MQ-2 Relation between RS/RO and Gas ppm .............................................................. 15
Figure 9: Fc-22 shield ................................................................................................................... 16
Figure 10: Pistons cables .............................................................................................................. 17
Figure 11: 2x16 LCD display ....................................................................................................... 18
Figure 12: Project Flowchart ........................................................................................................ 20
Figure 13: General structure ......................................................................................................... 21
Figure 14: Thermal Resistance ..................................................................................................... 22
Figure 15: Piston FSM .................................................................................................................. 23
Figure 16: Piston and Thermal General Electrical Circuit............................................................ 24
Figure 17: LCD-Arduino mounting schematic ............................................................................. 25
Figure 18: Electric Circuit ............................................................................................................ 26
Figure 19: %7.2 alcohol beer samples voltage readings ............................................................... 30
Figure 20: %14.5 alcohol wine samples voltage readings ............................................................ 31
Figure 21: %26 alcohol liquor samples voltage readings ............................................................. 31
Figure 22: Features of beer ........................................................................................................... 33
Figure 23: Features of wine .......................................................................................................... 34
Figure 24: Features of liquor......................................................................................................... 34
Figure 25: MQ-6 Alcohol equation............................................................................................... 37
Figure 26: No alcohol ppm measurement ..................................................................................... 43
Figure 27: %5.2 Beer ppm measurement ...................................................................................... 44
Figure 28: %14.5 wine ppm measurement ................................................................................... 44
Figure 29: %26 liquor ppm measurement ..................................................................................... 45
Figure 30: Removing wine ppm measurement ............................................................................. 45
1

CHAPTER 1

Introduction

1.1 Background

The Electronic Nose is a project that measures and analyses gas data and
concentrations, using for this purpose mechanical, thermal, electrical, electronic
and software parts. In the previous project (look Chapter 3), the mechanical and
thermal parts were implemented, and all ran with specific software; whereas in
this project, the problem of data acquisition and analyses is affronted.

The e-Nose is a very practical project, which with some changes for
example, can serve as a gas leakage detector. It allows to measure different
gases concentration in a certain moment or to see the evolution of a gas
concentration in time. In a world where the air pollution is increasing rapidly
affecting severely our health [1], to have a full understand of the materials and
gases we use is critical, and so on this project concerns also this investigation
area.

It is not the purpose of this project to use it in gas leakages, but taking
this project as a base, e code can be changed to make interesting programs,
such as guessing which product was used as a sample in the e-Nose. However,
we have to underline that these options are secondary in this project, and the
main development is based on the microcontroller and sensors implementation,
acquisition and analyses of data.

To manage the acquisition and analyses of gas data, specific hardware


and software is necessary. The main purpose of the hardware would be to
manage the inputs and outputs, and to make possible to interact with the project.
For this purpose, a microcontroller is needed. Nowadays, there are several
options in the market, such as Microchip or Arduino. In our case the Arduino will
be used, the reason is explained in the next subchapter.

As far as the software, to control Arduino boards, usually the Arduino


software is used. However, it is not the only option available; MatLab software is
also perfectly viable for this purpose, and actually, it is also better analysing data.
So, in our case MatLab will be used.
2

1.2 Objective

As we said before, the main objective is to acquire and analyse gas data.
However, this is not enough to make a general understanding of the project. To
acquire data, 6 different sensors will be used, each one with his own
characteristics. All this data has to be handled by a microcontroller and different
actions have to be made: connections between the micro and sensors, program
the micro to acquire the data in the correct moment, and save the data.

As far as the software, data needs to be more than just collected; has to
be analysed. The Arduino language allows having total control on the board
without having connected any cables to it, which is a huge advantage. But there
are also some problems with it: it is not possible to view data or graphs in the
computer, which obviously cuts down a lot of possibilities regarding the analyses.
There is another famous possibility very extended in the scientific areas, which is
MatLab. It allows having full control of the Arduino and at the same time plot
graphs, controlling all the data we collected etc.

However, some limitations appear with this decision. First of all, usually
the Arduino boards are less powerful than other kind of boards such Microchip
ones. It is not a big problem for this project, because the power of the
microcontroller is not the most important feature here; but it will definitely delay a
little bit the data acquisition process. Moreover, using the MatLab software, one
problem appears: computer needs to be connected to the Arduino board to
execute the program. In the previous project, the piston and the thermal
resistance were controlled using the Arduino software, making it more useful
because we do not have to use any external devices. But in this case, data has
to be managed and analysed, to plot graphs for example, and for this purpose,
the computer is essential.

As far as the interface with the user, a LCD display is used to improve the
communication. This allows following all the measuring process easily and
smoothly. Moreover, a digital fingerprint is realized with a python based
program, analysing the data we collected and collecting some useful information.
This information characterizes the data, marks its specifications and enables the
comparison between different sample readings.
3

1.3 Outline

The thesis is divided in five main parts. This part is the Introduction, where the
project is put in context. Next, in Chapter 2, comes the Theory, where the
fundamental general knowledge to understand the project is explained. In
Chapter 3, in the most important part of all, the Method and Results are showed.
There, is explained in detail how the project has been carried out, with all the
technical specifications and the results. In Chapter 4, the conclusions appear,
were a general synthesis is done, next to the problems we had during the
process and the basis for a future work. Finally, but not less important, the
Appendix has been added, where all the graphs of the sensors are shown,
besides the calibrations that have been done for each sensor, and all the code of
MatLab.
4

CHAPTER 2

Theory

To develop and understand this project, some basic electronic understanding


and software is needed. The complexity of the electronics is not high, the general
circuits are very simple, usually composed just by resistors, LEDs and some
push buttons. As far as the software, some more specific understanding is
needed, especially in MatLab and Arduino.

2.1 Electronics basics

Although some electronics basics theory is explained, other basic knowledge


using multimeter, voltage supply equipment, welding, and datasheets
understanding, is equally crucial. However, as the reader will understand, all this
can’t be explained in this project. The elements will be explained briefly.

2.1.1 Sensors

A sensor is an electronic element which purpose is to detect changes in a


measurable magnitude. There are different type of sensors, and can be classified
depending on different criteria; however, the only one used here are resistive
sensors, which means that the changes in the magnitude conclude in a change
in the resistance of the sensor. This can be easily transformed in a change of
voltage. More deeply will be analysed in Chapter 3. The only thing to be
understood here is what is the purpose of this element.

2.1.2 Potentiometer

A potentiometer is an electrical resistive element where the resistance can


generally be adjusted with a slide bar or with a wiper. This component has 3
connections, where 2 of them are always connected to the ground and to the
supply voltage, whereas the third one is connected to the variable pin, which can
be adjusted. Here is the general schematic:
5

Figure 1: Schematic of potentiometer

As we can see above, adjusting the pin number two, different resistances
appear between this connection and any of the other two. It is important to
mention that summing the resistances between pin number one and two, and pin
number two and three, will result in the same resistance as the one that is
between pin number one and three. So, applying a voltage supply between the
pins 1 and 3, a desirable voltage in the pin number 2 can be obtained. This is
great for to adjust the brightness of the LCD panel.
6

2.2 Arduino

Arduino is an electronic company that allows to install and work with different
microcontrollers, giving full support. The Arduino board will be used to acquire
data from sensors and to manage it dependin g on the purposes. The reason to
choose this microcontroller and not another one, is the facility and support that
this community shows. Moreover, an Arduino Mega 2560 will be used in this
project, just because more analogical inputs to manage all the sensors are
needed. The previous part of the e-Nose uses an Arduino Uno to control the
piston and the heater.

2.2.1 Arduino Mega 2560

This Arduino model is suitable for our project. As said before, the general
purpose of the project is to acquire and manage large amounts of data, so a
suitable board to do this is necessary. It’s true that the Arduino Uno is smaller
and has less pins, but it’s not enough to manage all this data. We can see its
specifications in the next figure:

Table 1: Arduino Uno specifications


7

We can compare the previous board with the Arduino Mega 2560 we are
going to use. It is based in Atmel’s Atmega2560 microchip, which runs with a
16MHz crystal oscillator [2]. The board has 54 digital input/output pins, 16
analogue inputs, a power jack and a USB connection. This last power source will
be the one used to supply the board and also to connect it with MatLab. All the
specifications can be found in the next table:

Table 2: Arduino Mega 2560 specifications


8

Moreover, in the following picture we can see an image of it:

Figure 2: Arduino Mega 2560

This microcontroller will be used not only to acquire the data from the
sensors, but also to supply them with enough voltage. However, having too many
sensors working at the same time, the micro might can’t handle it, so an external
supply voltage will be used for the sensors.

2.2.2 Arduino Software

The Arduino Software, also called IDE, is a C based language which allows to
write programs and upload them to the Arduino microcontroller. In this case we
will only have to know this language to understand the previous project, i.e. the
part that refers to the heater and to the piston. For this project the main language
used will be MatLab.

An Arduino based program is based on 3 main subparts: the variables


declaration, the configuration and the working. In the first part, the variables that
are going to used are declared, saying if they are constants or not, what kind of
variables are –like int, char or float–, and also which values have in case they are
constants. As far as the definition, is usually distinguished by the function void
setup(), and is basically used to configure the Arduino –if a pin will work as
output or input, the libraries we are going to use etc.–. Finally, there is the void
loop() and is the part where the program works or just repeats again and again.

Some commands need to be known to be able to understand the program


[3]:

 pinMode() : Defines de configuration of the pin, usually input or


output.
 analogRead() : Reads the analogue value associated to the pin
number into the parenthesis.
9

 analogWrite() : Writes an analogue value in the pin number that it is


into the parenthesis.
 digitalRead() : Reads the digital value associated to the pin number
into the parenthesis.
 digitalWrite() : Writes a digital value in the pin number that it is into
the parenthesis.
 map() : Re-maps the range of a variable.
 millis : Sets the time.

2.3 MatLab Software

MatLab is the program used to transfer commands to the Arduino and get data
from it. The reason to choose MatLab is that it enables to manage a lot of data,
making for example useful graphs. With this graphs a better understanding of the
samples is possible, e.g. what’s going on in the gas sensors more precisely.
Moreover, the understanding between both platforms is total, so shouldn’t appear
big problems to manage it, and in case that this happens the online community
support is huge.

As far as MatLab itself, it is a matrix based language, with its own


programing language, language M. The list of tasks that can be carried out with
this software is huge, from matrix calculus to implementation of algorithms.
However, we are going to focus on the control of Arduino boards and data
processing.

The whole software is going to be explained in Chapter 3, but first the


general programing functions need to be understood. These are the most used
ones [4]:

while() end : This is a typical command from any language program. It is


basically used to do loops: while the condition between the parenthesis is true,
the program will stay inside the loop.

if() end : As the one before, this is also a typical command. What it
basically does is to execute the program written inside the command if the
condition given between the parenthesis is true.

readVoltage() : After making the connection between the Arduino board


and MatLab, measuring the voltages from sensors connected to the pins is
fundamental. This command is used for this purpose.

writeDigitalPin() : On the contrary, it is also possible to send instructions to


10

the output pins in the board. With this command a HIGH or a LOW value is
written in the selected pin.

printLCD() : As far as the LCD, instructions have to be sent. The


communication between the Arduino and the LCD is not done just by one pin,
instead there are lots of connections. So to write something on the LCD we can’t
just write a HIGH or a LOW in a pin, but we have to use this command.

clearLCD() : Following the previous explanation, this command is used to


clear the LCD.

num2str (): Finally, what this command does is to convert a number to a


char chain type, or more precisely to a string. Will be used to show numbers in
the LCD.

2.4 PyEGG program

As far as to do the statistical analyses, i.e. the digital fingerprint, we are going to
use an open-source program that will be more deeply explained and used in the
next Chapter. The main purpose of it, is having some alcohol samples
measurements, get some statistical parameters from the graphs that define the
shape, the trend and the predictability of the readings. Although this program is
usually used in encephalograms, it is perfectly viable to use it also in any
adequate measurement where these parameters are needed.

2.5 Components of the project

In order to understand the purpose, the configuration and the results of the
project, is necessary to have a precise –in some cases just general–
understanding of certain components, such as the gas sensors.

2.5.1 Gas sensor

It is probably the element where the most accurate understanding is needed, not
just for the data acquisition but especially for the signal processing part. As we
know, 6 gas sensors are used, resulting to have different data, depending on the
model of the sensor.

With different gas sensors, a wider variety of data appears too, resulting to
be able to sample diverse substances. However, it won´t be as exact as would
be using a sensor fusion method. It would also be possible to use more than one
same model sensor, so the results would be much more precise –more even
11

using an unbiased method such as the Gauss-Newton method– but obviously the
range of substances to be measured would dramatically be reduced.

2.5.1.1 MQ Series

Is the time to look at the sensors will be managed in this project. All of them are
taken by the same manufacturer, and they belong to a series called MQ [5].

This series are made of a small heater and an electro-chemical sensor.


Are suitable for indoor measurements and they don´t have an AD converter, so
the output is analogical, suitable for Arduino. In our case there is a preinstalled
shield, to make the connections easier.

The heater, despite it improves the measurements, it is not obligatory to


use. The MQ series have more than one model, and the ones that we are going
to use have different characteristics, and so different preheat times. Some of
them are heated for more than 12 hours, until readings get stable. Some others
just need 20 seconds. The heater is suitable to make really accurate readings,
but having 6 sensors –with different heater voltages and times– we will not take it
into account it. However, in the preinstalled shield the heater is directly
connected to the supply voltage, so the ones that have small heating up period,
will just work with it. But as said it is not a problem because the sensitivity does
not change too much.

The way this sensors work is pretty simple. They are all resistive sensors,
which means that the resistivity –and so on the resistance– of the sensor
changes when the variable that is being measured also changes –in this case the
gas concentration–. This, connected to a basic electric circuit, enables to
measure a certain voltage that depends on the physical magnitude that is being
measured. Let’s see the circuit:

Figure 3: MQ series internal circuit

In the figure, the pin A is the heater and the pin B is the power supply. In
this case both are connected to 5V so they appear together (the heater is going
to be dismissed here). So, as explained before, the material used in the
12

resistance is sensitive to the gas– one of this material for example is SnO2 –. The
RL, i.e. the load resistance, in the scheme a potentiometer, is necessary to get
the VOUT data, and in this case can be calibrated. This VOUT will be the Arduino’s
analogue input for each sensor. When the gas concentration increases, the
conductivity of the resistance increases too, and so the total resistance RS
decreases proportionally. Taking a look to the previous scheme, we will rapidly
notice that VOUT will increase when the gas is present. This fact can empirically
be appreciated in the next figure, where, between 12 and 48 seconds, a gas has
been put in the sensors measuring area.

Figure 4: Vout changes in gas presence for MQ-6

The load resistor is valued from 2kOhm up to 47kOhm: it is more sensitive


to gas concentration changes at higher RL values, but also less accurate. As
shown in the next calibration part, a fixed resistor that the manufacturer
recommended is used instead of the potentiometer.

There are more variables that can change the result in a sensors reading.
Two of them are the temperature and the humidity. The standard detecting
temperature and humidity –as specified in the graphs– is 20±2℃ and a
65±5% RH, which is not far from our laboratory. However, changes in this
magnitudes will affect the measurements:
13

Figure 5: Measurement dependence on humidity and temperature for MQ-2

In our measurements however, these two parameters will be dismissed,


because the temperature and humidity in the laboratory is quite constant and
close to ones the manufacturer has used in his calibration.

As we can see in the figure 8, the provider gives a general graph to get
the concentration: after getting the ratio between RS and RO, is possible to get as
well the ppm of the gas that is being measured. RS makes reference to the
resistance in the sensor in presence of certain gas, in our sensors case, the
resistance that appears between A and A in the Figure 9. Meanwhile, RO is a
constant that each sensor has, which will be calculated later. As the sensor is a
resistive sensor, the data acquisition circuit is very simple, a simple voltage
divider is going to be used:

Figure 6: Voltage divider

The variable resistor is the RS parameter. The conductivity of RS increases


when a certain gas is present, so the output voltage will increase when the
concentration of the gas increases. This can be seen in the graph that the sensor
manufacturer provides:
14

Figure 7: Vout vs ppm

We said before that for the calibration the air has to be used. That’s
because the ratio RS/RO is constant for the air in all the graphs of the sensors
(see Appendix A). So, knowing that RS/RO is a known constant for the air, getting
the RS of the air we will get the RO of the sensor for this given air. The RS value in
the voltage divider is calculated this way just applying Ohm’s law:

(𝑉𝑐− 𝑉𝑂𝑢𝑡 )
𝑅𝑆 = (1)
(𝑉𝑂𝑢𝑡 /𝑅𝐿 )

where,
RS = Sensing Resistance (Ohm)
VC = Input voltage (V)
VOut = Output or measured voltage (V)
RL = Manufacturers calibrating resistance (Ohm)

2.5.1.2 Models

Inside the MQ series, there is a big variety of models. Here we will briefly
describe those which we are going to use only.

MQ-2: This sensor is used in gas leakage, especially for detection of


hydrogen, LPG (liquefied petroleum gas), methane, CO, alcohol, smoke and
propane [6]. In this case will be used to detect propane and alcohol.

The following graph will be the one used to get the concentration of gas
from the data obtained from the Arduino and some posterior calculations. The
graphs of the other sensors are in Appendix A.
15

Figure 8: MQ-2 Relation between RS/RO and Gas ppm

Taking a look above, to get the gas concentration described in “parts per
million”, the ratio between R and Ro is needed. RS makes reference to the
sensing resistance in different gases, whereas Ro in this case is defined by the
datasheet as the resistance of sensor in 1000ppm (parts per million) hydrogen.
However, can also be defined for example as the resistance of the sensor in
50ppm LPG, because there the ratio of R/SRO is also 1. So, the definition given
for each Ro in the next models can change (for example in MQ-3 is defined as
the resistance in clear air, because for air the ratio RS/RO is constant in 1).

The air is a mixture of different substances, and is the normal gas in the
ambient, so the RS/RO ratio will maintain constant in all the sensor graphs. We
will use this to calculate RO firstly, and after, this RO (which is a constant) to
calculate RS for different gases will be used.

MQ-4: This sensor is used mainly to measure natural gas. However, it is


also possible to use it for methane and even propane or butane [7]. The
sensitivity graph has been calibrated with 20kOhm resistance in the R L. In this
case will be used to measure methane and alcohol.

MQ-5: It is usually used in gas-leakage detecting equipment to detect


LPG, natural gas, town gas, alcohol or cooking fumes, so it can be installed in an
industrial area or even at home. The sensitivity graph has been calibrated with
20kOhm resistance in the RL [8]. In this case will be used to measure LPG and
alcohol.

MQ-6: It has a high sensitivity to LPG, especially propane. So as the MQ-


5, is useful for gas leakage at home or at the industry [9]. The sensitivity graph
16

has been calibrated with 20kOhm resistance in the RL. In this case will be used
to measure alcohol.

MQ-135: It is widely used in offices or buildings to define the general air


quality due to its high range of different gas detection. It can detect NH 3, NOX,
alcohol, Benzene, CO2, smoke etc. [10]. The sensitivity graph has been
calibrated with 20kOhm resistance in the RL. In this case will be used to measure
CO2 or more generally, the quality of the air.

MQ-137: It has a good sensitivity to ammonia and also to organic amine.


Therefore, can be applied at home or in the industry as a domestic or industrial
ammonia detector. The sensitivity graph has been calibrated with 47kOhm
resistance in the RL [11]. In this case will be used to measure NH3.

2.5.2 Shields

There are two different shields preinstalled. The only thing to know is how to
connect them.

The first one is the fc-22 [12], which has 4 pins: input or supply voltage
VCC, the ground GND, the output analogue voltage AO, and the output digital
voltage DO. VCC and GND will be respectively connected to 5V and 0V of the
Arduino Mega 2560. Meanwhile, the AO will be attached to the analogue
readings of the Arduino. As the shield has an ADC, the DO could also be used
and divide it by 1024 (8 bits reading) and multiply it by 5V resulting in the same
as the AO value.

Figure 9: Fc-22 shield


17

There is also another kind of shield, from the house Sainsmart [13]. The
pins are the same as the previous one and so are the connections to be made,
thus we will not spend more time with it.

2.5.3 Piston

The piston is one of the main parts of the previous project [14]. In this project
however, the precise functioning of it is not necessary, just know how to use it
and which parts are important. As explained before, there are two operating
functions for this piston: the one that doesn’t stop in the upper and down part,
and the one that stops. The second one will be used in this to have more time to
make the readings when the piston is up.

To do the readings then, the next information is crucial: when the piston is
actually in the upper side and when is not. For this purpose, the piston has a
specific cable, which is the feedback cable; the data that it is in this cable is going
to be read to know when to do the measurements. When the piston is up, there is
an average voltage of 0,32V in the cable, so we are going to program to do the
readings when we have 0,25 and 0,5V in the purple cable.

In the next figure appear the cables and its configuration. The purple one
is the one used to read. All the voltage is between 0 and 3,3V.

Figure 10: Pistons cables

2.5.4 LCD

To improve the communication between the program and the user, a LCD
display is used. It will also give the answer of the gas concentration, feature that
can be useful when we just want to know the concentration and no further
information that can be found in the MatLab program.

The LCD, is an alphanumeric 2x16 LCD display with background light [15].
What this means is that there is a predetermined background light, and the
contrast of the numbers and letter displayed is adjustable with a resistor.
Moreover, there are 2 rows in the display and in each row can be written 16
digits.
18

Figure 11: 2x16 LCD display

As we can see above, there are several pins to connect. A short


description of the connectors can be given to understand the circuit.

VSS is connected to the ground and VDD is connected to 5V. VO makes


reference to the contrast of the display, which can be calibrated with a
potentiometer, so VO would be connected to the middle pin of the potentiometer
to regulate the voltage, while the other two pins of the potentiometer would be
connected to 5V and ground. RS is the register selector, which at logical 1
registers commands and at logical 0 registers data. In the RW, read/write, with a
logical 0 data is read and with a logical 1 data is written. The E is the famous
enable pin, where a 0 disables the functions and the 1 enables them.

From D0 pin to D7 pin, are data bus pins, which as the names says, they
transfer data. We will not use the pins that go from D0 to D3 because the
operations needed are for just 4 bits, which will be enough with the pins from D4
to D7. At last, there are the anode and cathode pins, which will be respectively
connected to 5V and ground.

2.5.5 Buzzer

The buzzer will only be used in the third program. The purpose of it is to buzz
when the gas we expected has been detected, i.e. to buzz when the samples
concentration is in certain range of alcohol concentration. The kind of buzzer
used in the project [16], is a piezoelectric buzzer, which means that needs an
oscillating signal to work. As the purpose of it is just to beep, this signal can be
easily programmed with MatLab as seen in the Software subpart.
19

CHAPTER 3

Method and Results

Here how the development of the project has been carried out is explained. It is
the most technical and important part of the project.

The purpose of the project is to detect the concentration of different gases


with 6 sensors. However, the data acquisition can be carried out in different
ways. Three different ways to acquire data have been programmed: the first one
makes a sensor fusion with four sensors measuring alcohol concentration; the
second one is able to measure 6 different gases, one gas for each sensor at a
time; the third one is able to measure in large periods of time and detect the
alcohol we have used. In all the programs the calibration is carried out before,
and there is a LED, a buzzer, a LCD and a button to control and manage
everything.

This chapter is divided in 8 main parts. Firstly, we make a general


understanding of the previous project; then, we describe the components of the
project; after we focus on the electrical circuit; in the fourth part the calibration is
explained; we do some statistical calculus in the fifth part; we make some
calculations to get the gas concentrations in the sixth part; after we explain the
MatLab software; and finally we analyse the results.

Related to the previous explanation, a flowchart where the general path


has been followed is showed after. In the next figure there is the flowchart
followed to implement the project:
20

Figure 12: Project Flowchart

3.1 Previous Project

As explained before, this is a continuation of a general project called e-Nose, and


therefore is crucial to know what has been done before to understand this
continuation. So a general vision is required, not too focused on the technical
specifications but more in the functionality and purpose of the different parts. We
can divide our understanding area in four main parts: piston, thermal, user
system, and software.

3.1.1 Piston

By installing a piston in this project, an impulse can be given to each measurable


object. When an object is placed in a measuring place, this just ejects gases or
aroma in any direction, depending on lots of factors. However, when it is placed
in a moving piston which goes up and down, due to acceleration changes, the
gases are pushed upwards and get a pulse signal. As we will do it after, it will be
crucial to coordinate this impulse with the lectures of the sensors.
21

Figure 13: General structure

In the figure above the general structure of the project is shown. The
sample to be measured is placed just in the middle gap platform, and pulled up
and down by the piston. To get the impulse signal, the sensors will be placed at
the top of the structure, pointing them down.

So, the function of this piston is to give an impulse to the gases. For this
purpose, two different configurations have been stablished, which can be chosen
with two buttons. The first one is a continuous up and down movement, without
stopping. The second one stops in the upper side for 3 seconds (the stop time
can be easily changed in the Arduino program). Moreover, one of the goals is to
coordinate the sensor readings with the piston movement, and the data needed
for this is provided by a position feedback signal cable which, using an encoder,
allows a precise linear control position –is deeply analysed after–.

3.1.2 Thermal configuration

The thermal part is composed by a thermal resistance and one RTD. The
purpose of this part is to heat up the sample, so that it will pour more gas, and
then it will ease the measure with better data. This temperature must remain
constant in all the process, with a minimum variability, and can be easily changed
in the Arduino program. For this project, the thermal part is already totally
implemented, so we just have to know what is the configuration and how it works.

As far as the resistance, a silicon rubber insulated heater has been used.
The wires that heat up the sample –by the law of Joule– are covered with a
silicon rubber, to protect the sample from burning it. The sample will be placed on
the top of the resistance, maintaining a temperature near 60 ℃, and the surface
is a 90mm diameter circular area.
22

Figure 14: Thermal Resistance

To reach and maintain certain level of temperature, a feedback is


necessaire. In this case a RTD is been used, which is been configured in the
previous project.

3.1.3 User system

This is probably the most important section to understand. Here, how the
previous project works is explained, so that the user will be able to initialize and
work with the previous part.

As far as the first connections, enough power has to be supplied. On one


hand the Arduino Uno can be supplied with a USB connection, or with 5V
(depending on the Arduino, the supply power can vary and have a range). At the
same time this Arduino will supply enough voltage to the LEDs, the control of the
relay, the control of the thermal resistance, the RTD, and the control of the
piston. When the Arduino is supplied with power, a small LED turns on, to know
the chip is working correctly.

On the other hand, however, the piston and the thermal resistance need a
higher voltage supply, 12V, for what a signal generator is used, which will be
connected to the red and black cables of the output of the relay. The power
supply used in this project is the Agilent E3631A Triple Output DC Power Supply
device. The power circuit of the piston and the thermal resistance is made with a
relay and an opto-isolator device. When these 12V are applied, a green LED will
turn on. The ground of both electrical power supplies is common to avoid further
problems, and the same will be done in the future with some more electrical
power supplies. When these connections have been made, the Arduino program
can be transferred to the Arduino Uno. Now we are able to start using the
functions.

To heat up the sample, the push button number 3 has to be pressed, and
the thermal resistance will start heating up, until we press it again. If we do not
press it again, it will maintain the temperature defined in the program, 60 ℃.
While the resistance has not reached to the specified temperature, two LEDs will
be turned on: on one hand, the red LED will be turned on when the resistance is
working, and on the other hand, the yellow LED indicates the push button
23

number 3 has been pressed. When the predefined temperature is achieved, a


third green LED will activate. It is the moment to put the sample in the base, to
turn on the piston and to start collecting data.

The control of the piston is also easy. Pushing the button number 1, the
first mode will be activated, i.e. the up and down moving program without
stopping. Pushing the button number 2, the second mode will be activated,
moving up and down but waiting a pre-defined time in the upper and lower state.
With this working mode, an extra white LED is also activated. In the next diagram
the finite state machine of the piston is shown:

Figure 15: Piston FSM

The piston will only stop after pressing the same button as pressed to start
the movement. Having understood the previous information, in the next figure the
general electric circuit of the previous project is shown:
24

Figure 16: Piston and Thermal General Electrical Circuit

3.1.4 Software

We will not explain here the whole software of the previous project; we just need
to know the punctual information that might be helpful for us if we have to change
some parameters at certain moment.

The time that the piston waits in the upper side or the at the bottom is
important when making readings. This is very easy to change in the Arduino Uno
program. In the declaration of variables, the next two declarations appear:

int timeUP = 3; //Time in seconds that the piston stops up


int timeDOWN = 0; // Time in seconds that the piston stops down
25

So, it is possible to easily change the time that the piston waits. Moreover,
to obtain a different temperature in the thermal resistance there is the next
variable:

int Tuser = 45; //Target temperature in degrees Celsius

The problem with this variable is that the thermal resistance can’t just
reach to any value, so the datasheet of the thermal resistance and of the RTD
must be read before. This information is enough to start using this sub-parts of
the project.

3.2 Electric circuit

The electrical circuit is not very complex. The main elements are the Arduino, the
sensors and the LCD display. In the middle of it and facilitating the connections
there is the protoboard with resistors, LEDs, buzzers and buttons.

Arduino.cc offers the schematic to mount the LCD [17]. Is important to


mention that the connections made here have to agree with the code written in
the MatLab software. In our case, instead of connecting the LCD pins DB4, DB5,
DB6 and DB7 to D2, D3, D4 and D5 Arduino pins, we have connected them to
the pins D7, D8, D9 and D10. The basic schematic is shown in the figure 16:

Figure 17: LCD-Arduino mounting schematic

Having understood the LCD circuit, is easy to understand the other


connections. As far as the sensors, a load resistor between the ground and the
sensor output signal is necessary. Each sensor has a different resistor –specified
by the sensor manufacturer, which will be explained in the next subchapter– and
are connected to the analogue inputs from A0 to A5. Moreover, there is a LED
and a press button connected with a resistor also, and a buzzer. The press
button is connected to D4, as digital input; the buzzer is connected to D2, as
26

digital output; and finally the LED is connected to D13, as digital output also.
Here there is the whole schematic:

Figure 18: Electric Circuit

Finally, is important to mention that this project has to work hand by hand
with the previous project in all the senses. This also implies that the different
electric supplies need to have the same ground in common to avoid future
problems. So, the ground of the Arduino Mega2560, the ground of the Arduino
Uno and of the 12V supply, are connected.
27

3.3 Calibration

This is one of the most sensitive parts in the project. The aim is to acquire the
most precise data from the sensors. However, these sensors are not generally
too exact, and for very precise data acquisition, even the sensor provider
company recommends to buy other kind of sensors. One of the problems is that
each of this sensors is prepared to measure at the same time more than one
gas, so for example if we put a sample that emits two or more of the gases that
the sensor can measure, the data is going to get mixed up. To solve this
problem, the only thing to do is to know what substance are being measured, or
use better sensors. However, these sensors are enough to measure changes in
the concentration of certain gases and give an approximation of the
concentration.

However, a fixed resistor as shown in the voltage divider figure is needed.


Here is usually used a potentiometer, a variable resistor to calibrate the sensor;
but we need a precise instrument for each gas to complete this purpose. The
graphs that the manufacturer has provided are made with a concrete resistor
value in RL –the fixed resistor–, so in this case this value is going to be used.
The problem of this value is that has been calculated with a particular type of air
and calibrated with some known gas concentrations. As it is impossible for us to
use the same air –the sensor will be calibrated with air– the results will vary from
the ones that the provider gives. However, with better equipment, such any
calibrated measuring device, the RL could be calibrated with a potentiometer and
the data that the manufacturer provided us. In this case the same RL value that
the manufacturer provided will be used. Some error in the measurement process
is assumed.

Apart from that, shields are also being used. This shields have a known
fixed RL resistance, which will have to be added to the external RL. There are two
different shields, but both of them have the same resistance, 1kOhm. So, the
external resistance to be implemented, is the one that the manufacturer has
recommended minus the shield resistance. In the next table it is the summary:

Sensor Recommended Shield Resistance RL implemented Total RL


model RL (kOhm) (kOhm) (kOhm) (kOhm)
MQ-2 5 1 4,7 5,7
MQ-4 20 1 18 19
MQ-5 20 1 18 19
MQ-6 20 1 18 19
MQ-135 20 1 18 19
MQ-137 47 1 47 48
Table 3: RL for different sensors
28

Above are visible the values of the resistances to be implemented and the
total values to be used for the calibrations. These resistors will be implemented in
serial with the sensors.

Having the equation number 1 from theory, a table where the RO has been
calculated with experimental data at the laboratory is shown. It is important to
understand that these RO-s will be recalculated in each measurement in the
Arduino board in the calibration part, because VC and the quality of the air will
vary a little bit. However, the next table is helpful to understand how R O is
calculated:

RL total VOUT (V)


Sensor VC (V) RS Air (Ohm) Ratio Air RO (ohm)
(ohm) Air
MQ-2 4,89 5.700 0,3475 74.510,1 9,8 7.603,1
MQ-2 4,89 5.700 0,2361 112.355,9 9,8 11.464,9
MQ-4 4,89 19.000 0,1652 543.409,2 4,4 123.502,1
MQ-5 4,89 19.000 0,719 110.221,1 6,5 16.957,1
MQ-6 4,89 19.000 0,3177 273.445,7 10 27.344,6
MQ-135 4,89 19.000 0,1212 747.584,2 3,7 202.049,8
MQ-137 4,89 48.000 0,5254 398.745,3 3,6 110.762,6
Table 4: Calculating RO

Knowing VC, RL and VOut, calculate RS Air for each sensor using the
formula (1). After this, RO is calculated using the next formula:
𝑅𝑆 𝐴𝑖𝑟
𝑅𝑎𝑡𝑖𝑜 𝐴𝑖𝑟 =
𝑅𝑂

where Ratio Air is a constant. Having RO calculated, the calibration has been
finished.
29

3.4 Statistical analysis

After having implemented the circuit, is possible to acquire data from the
sensors. Yet, we are only able to read voltages from the sensors –to obtain the
gas concentrations more calculations are needed, which are specified in the next
subpart 3.6–.

A priori, we could think that just reading voltage from sensors is not very
useful, but actually a lot of information appears on it. Putting an alcohol sample in
the piston and making readings while the piston goes up and down, will appear a
kind of sinusoidal path in the graph. The nearer the alcohol sample is from the
gas sensor the stronger the voltage signal will be –as explained in subpart 4.3–
and in the opposite side, the further the sample is, the weaker the voltage will be.
In the same way, using stronger alcohols, the reading voltages will also be
higher. Three different alcohols have been recorded–beer, wine and liquor– while
the piston went up and down 3 times. With each alcohol, we used 40 different
samples, i.e. we put one sample and we make readings while the piston goes up
and down 3 times, continuing with the same process with 40 equal samples. We
make 200 readings with each sample, resulting one sample readings in 50
seconds, which is the time the piston needs to go up and down 3 times. The
MatLab code used to record the data is in the Appendix C, with all the rest
MatLab code. Here is the pseudo code:

DEFINE and CREATE All the variables - MEGA2560, lcd,


createLCD
WHILE We have done less recordings than 40 different
samples
PRINT "Making readings"
WHILE We have done less than 200 recordings for each
sample
SAVE Sensor voltage
ENDWHILE
PRINT "Wait"
WHILE We don't press the button for new sample
Wait
ENDWHILE
ENDWHILE
30

What we get from the previous code is the M 40x200 matrix, which means
we have recorded 200 readings in each of the 40 samples. After obtaining the
data, we can plot it. Here is the pseudo code for it:

WHILE We haven't plot all the 40 signals


PLOT Next signal
HOLD The figure
ENDWHILE
DEFINE Graphs characteristics

Obviously, we have to change the title, the labels and the data depending
on which graph we want to get. In the next graphs we can see the results:

Figure 19: %7.2 alcohol beer samples voltage readings


31

Figure 20: %14.5 alcohol wine samples voltage readings

Figure 21: %26 alcohol liquor samples voltage readings


32

As we can see in the graphs plotted above, there are peeks in the
readings, which correspond to the phase where the piston is up, i.e. when the
piston is closest to the sensor. Moreover, the last peek in the beer graph is
bigger than the first one, which is probably because the aroma that the alcoholic
sample releases is lower at the beginning and increases with time. Leaving too
much time the sample in the piston, it would lose its aroma –read, alcohol gas
concentration– and the voltage readings would get smaller. In the wine graph
and the liquor graph, at the beginning the voltage is quite far from the mean
voltage, which is probably because the gas needs some time since you put it in
the piston to spread until reaches to the sensors.

Having recorded all this information, the analysing part can start. For this
purpose, an open source python module has been used [18]. The purpose of this
module is to analyse big amounts of data in EEG (electroencephalography), but it
is also suitable to extract features from different time series, such as signal
processing. PyEEG is the python language based program used to acquire all
this features. As the developer says, there are various algorithms and
parameters, which means that the results given by this toolbox can differ from
others with the same purpose.

To use this program, some basic knowledge of programming is necessary:


using python to change some few parameters and the names of archives, and
using the bash or the terminal. Having the 40x200 voltage data matrix and using
MatLab, we get a 7x200 matrix and we save it in .txt – ASCII format:

beer7=zeros(7,200);
i=1;
while(i<=7)
beer7(i,:)=Beer(i,:);
i=i+1;
end

myMatrix2 = beer7';
save beer7.txt myMatrix2 -
ASCII
In this case we only save 7 readings because the python program has
been implemented to make only 7 different calculations. However, if we want to
calculate all the 40 features (one for each sample) we just have to change the
index in the while loop. In my case I will analyse the sixth sample features only
because it is a quite stable reading. In a future work, a mean of all can be
calculated and a more exhaustive analyses be done.
33

This file called beer7.txt, have to be introduced in the same folder where
the python program features1.py is. After this, just execute features1.py from the
terminal and get the results in the specified archive. In the next table the results
are shown:

Feature Beer %7.2 Wine %14.5 Liquor %26


Hurst 8.14 e-01 8.11 e-01 7.89 e-01
Hjorth 4.28 e-03 4.85 e-03 4.92 e-03
PFD 7.25 e-01 8.12 e-01 6.46 e-01
Approximate Entropy -6.02 e-03 -1.01 e-02 -2.49 e-02
HFD 9.71 e-02 9.06 e-02 3.51 e-01
Fisher Information 9.03 e-01 9.40 e-01 9.59 e-01
SVD Entropy 3.75 e-01 2.54 e-01 2.14 e-01
Table 5: Features of voltages readings

There are several features to explain in this table. The values obtained for
the three cases are quite similar, which means that the time series have a similar
shape and characteristics. Here there is a column graph for each alcohol sample:

Beer %7.2
1
0,9
0,8
0,7
0,6
0,5
0,4
0,3
0,2
0,1
0
-0,1

Figure 22: Features of beer


34

Wine %14.5
1
0,8
0,6
0,4
0,2
0
-0,2

Figure 23: Features of wine

Comparing all the graphs, although the variables have a similar


distribution –because the shape and the trend is also similar-, is easily
observable that there is a difference between them.

Liquor %26
1,2
1
0,8
0,6
0,4
0,2
0
-0,2

Figure 24: Features of liquor

As we can see in the graphs, all the features have a similar distribution,
which is what was expected knowing that the evolution of the signals in time is
also similar in the three cases.
35

The first feature is called The Hurst Exponent, also called Rescaled
Range statistics, which is a scalar feature. It makes reference to the long-term
memory that the time series has. Basically, if we have a value between 0.5 and 1
–as it is our case–, means that probably a high value will appear in the series
after another high value [19]. In the graphs, the time series does not oscillate too
much –no to say that negative values do not appear–, so it is totally correct.

The Petrosian Fractal Dimension, or PFD, is also a scalar feature. There


are several ways to define what a fractal dimension is in mathematics, but the
easiest one is probably the one that defines it as the complexity ratio between
the details in a pattern and the scale. The bigger the scale is, the shape will
better be defined. It is calculated with the next equation:

𝑙𝑜𝑔10 𝑁
𝑃𝐹𝐷 =
𝑙𝑜𝑔10 𝑁 + 𝑙𝑜𝑔10 (𝑁/(𝑁 + 0.4𝑁𝛿 )

where N is the series length, and 𝑁𝛿 is the number of signal changes in


the signal derivative. So, as seen in the graphs, the shape of the figures is a sine,
it is not a random stochastical process where there are a lot of changes in the
sign of the derivative. As a result, the PFD of the three graphs is quite high.

The approximate entropy is a feature used in several scientific areas such


as medicine, finance, psychology etc. Classical statistical variables such as the
mean or the variance are not practical to define the predictability of a time series,
and for that purpose the entropy is used. However, to calculate the exact entropy
a lot of data is needed and the process is very complex and it is very sensitive to
the noise, so there is another extended method to calculate this, the approximate
entropy or ApEn. This parameter needs less computing power and is less
sensitive to the noise. As far as the interpretation, a predictable process has a
small ApEn, and the opposite in the other way. In this case, there are values
multiplied by e-03 and e-02, so, as they are small values, the time series is quite
predictable, as expected.

The HFD, Higuchi Fractal Dimension, is another parameter to measure


the irregularity of a time series. Historically, the power spectrum analysis has
been used to measure the irregularity of a time series using the parameter 𝛽 as
the index. However, sometimes is better to use the HFD [20]. The same as
before, the value is very small, resulting in a small irregularity.
36

3.5 Obtaining concentrations

The calibration has to be done before any measurement. What we get from it, is
the constant RO which will completely depend on the air. That’s why the
calibration has to be made while there are no other gases in presence.

After calculating RO for each sensor, we just have to put our measuring
gas or sample under the sensors and measure the new VOUT. With this
parameter, is possible to recalculate the RS variable, but now not for the air as
was done before, but for the sample. With VOUT and applying the formula (1)
again RS is obtained. We can now calculate the ratio RS/RO for the new gas. With
this ratio is possible to know which the concentration of the gas is, we just have
to take the graphs that are in Appendix A, which have been given by the
manufacturer, and check what concentration corresponds to the ratio.

The ratio RS/RO is given in the y axis and the ppm concentration in the x
axis. An important characteristic of this graph is that the scale is logarithmical. As
the gas concentration has to be shown in the LCD display, the ratio is not
enough, so an equation is necessary to calculate the concentration. As the
manufacturer has not provided any kind of equation, will have to be derived
empirically from the graph that. Here is where the program Excel is going to be
used.

As we know, there are three different main programs: the one that
calculates the alcohol concentration with a sensor fusion method, the one that for
each sensor there is assigned one type of gas to make the measurements, and
the one that records the temporal concentration and buzzes when the
concentration of a certain gas is detected. If one sensor is going to measure only
one gas, only the equation of one gas will have to be calculated; if it is going to
measure 2 gases, 2 equations will have to be calculated etc. In our case, the
sensors MQ-2, MQ-4 and MQ-5 will measure two gases –will work in program
one calculating alcohol concentration for the fusion program, and also in program
two calculating another gas which has been defined in the part 4.1.1.2–. The
other 3 sensors, MQ-6, MQ-135 and MQ-137 will only work with only one gas, so
only one equation needs to be calculated.

Firstly, to get the equation, some empirical data from the graph above has
to be taken, and a table in Excel made. In this example we will only do it with the
sensor MQ-6 and the alcohol. The others one can be found in Appendix B.
37

MQ-6 ppm Alcohol Rs/Ro


200 8
300 7,5
500 7
600 6,8
800 6,4
1000 6,2
2000 5,5
3000 5
5000 4,7
6000 4,6
8000 4,4
9000 4,3
10000 4,2

Table 6: Empirical data from manufacturers graph

As shown above, to each concentration (x axis), each ratio RS/RO (y axis)


is matched. After this, the only thing that needs to be done is to plot a graph with
this data and get the equation of the graph:

MQ-6 ppm Alcohol vs Rs/Rol

12000

10000

y = 6E+07x-5,999
8000

6000

4000

2000

0
0 1 2 3 4 5 6 7 8 9

Figure 25: MQ-6 Alcohol equation


38

To get the equation, we just have to add a power tendency line, and Excel
will automatically give the equation. As we will have a lot of this equations, 9
exactly, we will create in MatLab two vector where we will save the data from this
equation. In the first vector we will save the multiplying part, in this case 6E+07,
and in the other vector we will save the power part, -5,999. To get the
concentration, we just have to replace the variable x with the ratio we got on the
previous part. With this we have got our results. The other graphs can be found
in Appendix B.

3.6 Software

The software has been developed in MatLab, and can divided in 4 main parts.
Declaration, calibration, programs and functions. The programs and the functions
are divided as well in more subparts. All the code is in the Appendix C, here it is
only shown the pseudo code, a better and faster way to understand the purpose
of our code (for a precise analyses go to the appendix). The button is used to
interact with the program; the LED is used to know when the program, is in the
sensor reading par; and the buzzer is used only in the third program to say when
has detected the alcohol we specified.

3.6.1 Declaration

Is the part where the constants and the variables are declared, and the Arduino
Mega2560 is configured. It is important to know how to declare the LCD with
MatLab [21]:

CLEAR ALL
DEFINE and CREATE All the variables -
MEGA2560, lcd, createLCD, RL, Ratios,
Constants, Limits
READ Vc Voltage

3.6.2 Calibration

Having declared all the variables, the calibration starts. But first, the program to
be executed has to be chosen:
PRINT "Choose program"
WHILE We haven't chosen the program
EXECUTE FUNCTION choose_program
ENDWHILE
39

After having this done, is the calibration turn. In the calibration we don’t put any
gas sample in the piston, and we make the readings while the piston is working.
Here is the pseudo code for it:

PRINT "Press the button for calibration"


WHILE We don't press the button
READ Button
IF Button has been pressed
HIGH LED Indicates that readings are being made
ENDIF
ENDWHILE

WHILE Calibration has not been made


IF Piston is up
SAVE Sensors data, 50 readings each sensor
PLOT Sensors data
MEAN Of the sensors readings, delete noise
CALCULATE RO and Calibration has been made
LOW LED
ENDIF
ENDWHILE

3.6.3 Programs

There are three different programs to choose in this code. The first one
measures alcohol concentrations with 4 different sensors and calculates the
mean.
IF We chose Program 1 before
EXECUTE waiting_time function, waiting to start
or to stop new readings
WHILE We want to do new readings
EXECUTE read_data function to read data from
sensors
LOW LED
CALCULATE All the concentrations
PRINT Concentration
EXECUTE waiting_time function
ENDWHILE
ENDIF
40

The second one is able to measure different gases. Only one gas with only one
sensor at a time.

IF We chose Program 2 before


EXECUTE waiting_time function
WHILE We want to do new readings
EXECUTE choose_gas function to choose which
gas we want to measure
EXECUTE read_data function to read data from
sensors
LOW LED
CALCULATE All the concentrations
PRINT Concentration
EXECUTE waiting_time function
ENDWHILE
ENDIF

And the third one is able to detect when certain alcohol is in the piston. When the
Arduino detects a concentration which corresponds to the concentrations of the
alcohol we chose, the buzzer buzzes:

IF We chose Program 3 before


EXECUTE waiting_time function
WHILE We want to do new readings
EXECUTE choose_alcohol function to choose which
alcohol we want to detect
WHILE We do 30 different readings for the chosen
alcohol
SAVE Data of the sensors
MEAN Of the readings of the sensors
CALCULATE All the concentrations
PRINT Concentration
IF Concentration matches to the chosen
alcohol
HIGH Buzzer
ENDIF
ENDWHILE
PLOT Readings
EXECUTE waiting_time function, waiting to start
or to stop new readings
ENDWHILE
ENDIF
41

3.6.4 Functions

Finally, there are 5 functions, used to shorten the code, each one with his
purpose.

The first one enables to choose which program number we want to execute
(remember that we have 3 of them):

WHILE Timer is not finished and program has not been


chosen
SWITCH Index, having 3 cases, one for each program
READ If button has been pressed
IF Button has been pressed
SAVE Program number
ENDIF
ENDWHILE

The second function is used to make new readings or to stop the whole program:

WHILE We haven't chosen what we want to do


READ Button
IF The button has been pressed
We want to do a new reading
IF The button continues pressed after 2
seconds
We want to stop the whole program
ENDIF
ENDIF
ENDWHILE

The third function is used to save sensors data and calculate the new RS:
WHILE We haven't read anything
IF The piston is up
HIGH LED
SAVE Sensors data
MEAN Data
CALCULATE RS
ENDIF
ENDWHILE
42

The fourth function is used to choose the gas we want to measure:

WHILE Timer is not finished and gas has not


been chosen
SWITCH Index, having 6 cases, one for
each gas
READ If button has been pressed
IF Button has been pressed
SAVE Gas number
ENDIF
ENDWHILE

The fifth function is used to choose the alcohol we want to detect:

WHILE Timer is not finished and alcohol has


not been chosen
SWITCH Index, having 3 cases, one for
each alcohol
READ If button has been pressed
IF Button has been pressed
SAVE Alcohol number
ENDIF
ENDWHILE
43

3.7 Results

The results have to be understood in two parts. First of all, the software and
hardware that was developed have to work as expected –the readings have to be
synchronised with the piston, the LCD has to show the expected information, the
buzzer has to buzz when the concentration is in certain range etc.–. This can
only be seen in the moment when the whole project is working. As is not only a
theoretical project, instead, mainly practical, the results working of the project has
to be seen “in situ”.

However, there is another part where results can be found: the


measurements that have been done with the project, plotted in graphs. In the
program 2 it is possible to choose and measure 6 different gases. However, the
main part of the project has been focused measuring and working with alcohol,
and the results are also going to be focused on this kind of samples. The other
gases measurements and analyses is opened for a future project –this will be
discussed in the next chapter–.

A comparison of different alcoholic samples has been done in the next


work. Different alcoholic drinks –which emit more or less alcohol gas– have been
measured and compared. The first measure is with no alcoholic samples on it, so
the alcohol measurements are minimum, as expected:

Figure 26: No alcohol ppm measurement

The concentration shown above is very small. It stabilizes around 25 ppm,


which is perfectly normal. Compared to the next graphs it is minimal. The next
figure is the %5.2 alcohol beer:
44

Figure 27: %5.2 Beer ppm measurement

As seen above, the concentration of alcohol is much bigger. The


concentration is around 250ppm. The results are not too stable, which is
probably due to the noise and the lack of accuracy described by the
manufacturer. But it is perfectly useful to measure different alcohol samples and
also trends in time. Analysing now a %14.5 alcohol wine:

Figure 28: %14.5 wine ppm measurement

The scale in this graph has been significantly increased compared to the
previous alcohol. This is just because a bigger graduation of alcohol has been
used. Appears to be an increasing trend in the graph, probably because the wine
alcohol particles needed more time to expand (compared to previous graphs).
Them, a liquor was tested:
45

Figure 29: %26 liquor ppm measurement

With the liquor test, the scale was dramatically increased. The first and the
last peeks don’t seem to be a truthful measurement, because these sensors are
not adapted to such high concentrations. Even the low part is too high for these
sensors, from where we derive that this measurement is not completely truthful.
An important conclusion is derived from this, that very high graduation alcohols,
are not well measured with these sensors in such a small distance.

Finally, we can see what happens when we remove the wine. It decreases
with a slight exponential trend. In this case we needed more readings to
appreciate more the decrease on the concentrations:

Figure 30: Removing wine ppm measurement


46

CHAPTER 4

Conclusions

4.1 Summary

What finally has been achieved in this project, is basically to implement a


functional e-Nose synchronised with the previous project and with a nice
interface for the user to make the measurements easier. The hardware works
perfectly in consonance with the software, resulting in a fluid operating project.
There three different programs to use: two of them make basic readings –which
is the purpose of the e-Nose– while the last one is something more creative,
being able to detect alcohols, and at the same time showing the potential this
project has, completely up to the imagination, leaving a lot of ways opened for
future work.

As far as the accuracy of the sensors, it is not too precise. If the e-Nose
programming and designing area opens several doors, the lack of accuracy and
range closes some others. These sensors, as the manufacturer says, are not
suitable for too precise measurements, and also the range they can manage is
limited. However, they are completely valid to appreciate odor trends –if it is
increasing, maintaining constant or decreasing–, and also to make a general
approximation about how much concentration is the e-Nose exposed to.

4.2 Problems

As said before, the biggest problem of the project has been the lack of
accuracy of the sensors, and the lack of range. For example, using too strong
alcohols, the sensor is not able to make correct measurements, and looses
completely the path. Moreover, the calibration has been made according to the
manufacturers specifications, but to do a better calibration –instead of using the
load resistor that the manufacturer recommends–, we would need another more
accurate device to calibrate our own sensors.

As far as the readings, the procedure used to make them must be


extremely cautious. Measuring a wine sample right after having measured a beer
sample will definitely give wrong results, because the gases get mixed. So, it was
fundamental to have a good systematized procedure to make the measurements
47

and not to get wrong results. This is also completely valid for future
measurements.

There has been one more problem, which finally got solved. The supplying
voltage via USB for both Arduinos was not enough to supply all the 6 sensors,
the LCD panel, the buzzer, and the LED. So, another supply voltage was added,
solving the problem.

4.3 Future work

The problems and the summary explained above, mark the road for future
work. First of all, would be great to have an alcohol calibrating device, so that the
sensors load resistors could be calibrated. This would definitely give much better
and accurate results. The same could be done with all the other gases respective
sensors. Moreover, testing the other gases and making the analyses of the would
also be great, completing all the information. The python module could also be
used in more gases and with more readings, resulting in a more extended
analyses.

Another good point would be to make the hardware smaller, especially by


putting together both Arduinos. Of course, the Arduino Mega 2560 should be
used, because the Arduino Uno has not enough inputs to manage all the
sensors.

Finally, one last option appears in the horizon. Using another type of gas
sensor, would open the door to make comparisons between both sensors. By
using a better sensor, it wouldn’t just result in better results, but also would be
possible to do a concrete and interesting comparison between both.
48

References

[1] International Agency for Research on Cancer:


http://www.iarc.fr/en/media-centre/iarcnews/pdf/pr221_E.pdf

[2] Arduino Mega2560.


https://www.arduino.cc/en/main/arduinoBoardMega2560

[3] Monk, S. Programming Arduino: Getting Started with Sketches


McGraw-Hill Education TAB, 2016

[4] I. Kattan, P. MatLab for Beginners: A Gentle Approach


Createspace Independent Publishing Platform, 2014

[5] MQ Gas Sensors:


http://playground.arduino.cc/Main/MQGasSensors#list

[6] MQ-2 Datasheet:


https://www.parallax.com/sites/default/files/downloads/605-00008-MQ-2-
Datasheet.pdf

[7] MQ-4 Datasheet:


https://www.sparkfun.com/datasheets/Sensors/Biometric/MQ-4.pdf

[8] MQ-5 Datasheet:


https://www.parallax.com/sites/default/files/downloads/605-00009-MQ-5-
Datasheet.pdf

[9] MQ-6 Datasheet:


http://www.datasheetspdf.com/PDF/MQ-6/699270/2

[10] MQ-135 Datasheet:


http://www.dreamgreenhouse.com/datasheets/MQ-135/MQ-135.pdf

[11] MQ-137 Datasheet:


http://eph.ccs.miami.edu/precise/GasSensorSpecs/NH3.pdf

[12] Fc-22 Gas Shield:


49

http://www.dx.com/es/p/fc-22-1-harmful-gas-detector-sensor-module-for-
arduino-179225#.WSfqp1IrzR0

[13] Sainsmart Gas Shield:


https://www.sainsmart.com/sainsmart-mq7-co-carbon-monoxide-gas-
sensor-module-for-arduino-uno-mega2560-r3.html

[14] Linear Actuator L12-I 100mm:


https://www.actuonix.com/L12-I-Micro-Linear-Actuator-Internal-Controller-
p/l12-i.htm

[15] LCD 2x16 Display:


https://www.openhacks.com/uploadsproductos/eone-1602a1.pdf

[16] Piezoelectric buzzers:


https://www.kemo-electronic.de/en/Components/Elements/Speaker/S105-
Piezo-speakers-and-microphones-approx-20-pcs-.php

[17] LCD-Arduino schematics:


https://www.arduino.cc/en/Tutorial/HelloWorld

[18] PyEEG: An Open Source Python Module for EEG/MEG Extraction:


http://pyeeg.org

[19] The Hurst Exponent: Predictability of Time series:


http://analytics-magazine.org/the-hurst-exponent-predictability-of-time-
series/

[20] Higuchi’s algorithm:


http://iopscience.iop.org/article/10.1088/1742-6596/475/1/012002/pdf

[21] Create LCD Add-on in MatLab:


https://es.mathworks.com/help/supportpkg/arduinoio/ug/add-lcd-
library.html
50

Appendix A

The graphs used in the calibration and data processing are displayed here. Each graph
makes reference to one sensor, giving the relation between the Ratio RS/RO and ppm.

Appendix Figure A.1: MQ-2 Relation between Rs/Ro and Gas ppm
51

Appendix Figure A.2: MQ-4 Relation between Rs/Ro and Gas ppm

Appendix Figure A.3: MQ-5 Relation between Rs/Ro and Gas pp


52

Appendix Figure A.4: MQ-6 Relation between Rs/Ro and Gas ppm

Appendix Figure A.5: MQ-135 Relation between Rs/Ro and Gas ppm
53

Appendix Figure A.6: MQ-137 Relation between Rs/Ro and Gas ppm

Appendix B

The other tables and graphs created to calculate the equation for each sensor and gas:
54

Appendix Figure B.1: MQ-2 Propane Equation

Appendix Figure B.2: MQ-2 Alcohol Equation

Appendix Figure B.3: MQ-4 Methane Equation


55

Appendix Figure B.4: MQ-4 Alcohol Equation

Appendix Figure B.5: MQ-5 LPG Equation


56

Appendix Figure B.6: MQ-5 Alcohol Equation

Appendix Figure B.7: MQ-6 Alcohol Equation


57

Appendix Figure B.8: MQ-135 CO2 Equation

Appendix Figure B.9: MQ-137 NH3 Equation


58

Appendix C

C.1 Supplementary Software

C.1.1 MatLab code to record the voltage data:

clear all;
addpath('/Users/Lander/Documents/MATLAB/SupportPackages/R2015b/arduinoi
o/toolbox/matlab/hardware/supportpackages/arduinoio/arduinoioexamples/S
DKExampleLCD');
%We add the arduino toolbox to MatLab

Mega2560=arduino('/dev/cu.usbmodem1421','Mega2560','libraries','Example
LCD/LCDAddon');
%We define our Arduino Mega 2560 as a new variable including the LCD
%library. This variable will change if we change the port we have
connected the Arduino.

lcd=addon(Mega2560,'ExampleLCD/LCDAddon',{'D12','D11','D10','D9','D8','
D7'});
%We define a new variable for the LCD, depending on the connections we
have done.

createLCD(lcd,{'D12','D11','D10','D9','D8','D7'});
initializeLCD(lcd); %We create and initialize the LCD.

clearLCD(lcd); %It is used to clear all the LCD.


59

printLCD(lcd,'Put the sample.'); %First recording


printLCD(lcd,'Press to stop.');
pause(2)
M=zeros(40,200); %we create the matrix where we will save all the data:
40 samples with 200 readings in each.
i=1;

while(i<=40) %Each loop will record a different sample. We have to put


40 samples.
h=1;
printLCD(lcd,'Making readings.');
pause(1);
while(h<=200) %We make 200 readings for one sample, it will take 50
seconds.
M(i,h)=readVoltage(Mega2560,'A3'); %A3 is the sensor that
records alcohol readings.
h=h+1;
pause(0.25) %time between readings.
end
clearLCD(lcd);
printLCD(lcd,'Wait');
show = i;
show = num2str (show);
printLCD(lcd,show); %We print in the LCD in which sample number we
are now.
while(readDigitalPin(Mega2560,'D4')==0)
end
%We use this while to change the sample from the piston. When we
%put a new alcohol sample, we press the button connected to the
%pin number D4 and at the same time we press the piston.
i=i+1;
end

C.1.2 MatLab code to plot the figures:

z=1;
figure(1)
while(z<=40)
plot(Beer(z,:));
hold on
z=z+1;
end
title('%7.2 Beer samples voltage readings');
xlabel('Number of readings');
ylabel('Voltage signal');
hold off
60

C.2 MatLab General Software:

C.2.1 Declaration

function []=Gas_dector %Main function

clear all;
addpath
('/Users/Lander/Documents/MATLAB/SupportPackages/R2015b/arduinoio/toolb
ox/matlab/hardware/supportpackages/arduinoio/arduinoioexamples/SDKExamp
leLCD');
%We add the arduino toolbox to MatLab
Mega2560=arduino('/dev/cu.usbmodem1421','Mega2560','libraries','Example
LCD/LCDAddon');
%We define our Arduino Mega 2560 as a new variable including the LCD
%library. This variable will change if we change the port we have
connected the Arduino.
lcd =
addon(Mega2560,'ExampleLCD/LCDAddon',{'D12','D11','D10','D9','D8','D7'}
);
%We define a new variable for the LCD, depending on the connections we
have done.
createLCD(lcd,{'D12','D11','D10','D9','D8','D7'});
initializeLCD(lcd); %We create and initialize the LCD.

%Parameters and constants used for calibration


Vc = readVoltage(Mega2560,'A9') %Sensors supply voltage
RL = [5700 19000 19000 19000 19000 48000]; %RL for MQ-2,RL for MQ-4,MQ-
5,MQ-6,MQ-135,RL for MQ-137
Ratio_Air = [9.8 4.4 6.5 10 3.7 3.6]; %Air ratios

%Program 1 - RS/RO vs ppm Alcohol Data for MQ-2,MQ-4,MQ-5,MQ-6


Mult_Const_Alc = [3518 100000000000 85665 60000000];
Power_Alc = [-2.603 -14.44 -4.682 -5.999];

%Program 2 - RS/RO vs ppm Data for MQ-2,MQ-4,MQ-5,MQ-6,MQ-135MQ-137


Mult_Const = [695.01 1090.1 76.283 60000000 122.05 41.19];
Power = [-2.313 -2.743 -2.56 -5.999 -2.823 -3.229];

%Program 3 Alcohol limits


upper_limit = [3000 2000 140000];
down_limit = [500 200 20000];

C.2.2 Calibration

%First we choose what program we want to run. We have 3 different


programs:
% 1-Measuring alcohol with 4 sensors.
% 2-Measuring different gases with only 1 sensor.
% 3-Detecting when certain alcohol is in the piston.
61

%Initialize
printLCD(lcd,'Hello!');
printLCD(lcd,'Choose program.');
pause(3);
clearLCD(lcd);

program=0; %Program choose variable


i=1; %Program index variable

while(program==0 && i<=3) %Loop to choose which program we want to


execute
program = choose_program(program,i,lcd,Mega2560); %Choose which
program we want in the function
pause(1); %Wait
i=i+1; %Next program
end

%Calibration
reading=0;
calibration=0;

printLCD(lcd,'Presh the button');


printLCD(lcd,'for calibration.');

while(reading==0) %While is not recording data


button=readDigitalPin(Mega2560,'D4'); %Push the button connected to
D4 to enable reading
if(button==1) %If the button has been pressed
reading=1; %We are out of the while next loop
writeDigitalPin(Mega2560,'D13',1); %A signal to say that data
is able to be recorded
printLCD(lcd,'Green LED ON');
pause(2);
end
end

%We define the arrays and matrix we will use


v=1:1:6;
RS_Air = 1:1:6;
RO = 1:1:6;
stop=0;
M=zeros(50);
z=1:1:6;
RS= 1:1:6;
y = 1:1:6;
Ratio = 1:1:6;

printLCD(lcd,'Press piston.')
while(calibration==0) %While calibrations is not been made
pause(0.25)

if((readVoltage(Mega2560,'A8')<=0.5)&&(readVoltage(Mega2560,'A8')>=0.25
))
62

%Only we will read data when the piston is up, which electrically
means
%that only when the piston voltage signal is between 0.5 and 0.25

i=1; %Index for multiple reading


while(i<=50) %We make 50 readings with each sensor and save it in
the matrix M
M(1,i)=readVoltage(Mega2560,'A0'); %We save in the first row
the number i data
M(2,i)=readVoltage(Mega2560,'A1'); %We save in the second row
the number i data
M(3,i)=readVoltage(Mega2560,'A2'); %We save in the third row
the number i data
M(4,i)=readVoltage(Mega2560,'A3'); %We save in the fourth row
the number i data
M(5,i)=readVoltage(Mega2560,'A4'); %We save in the fifth row
the number i data
M(6,i)=readVoltage(Mega2560,'A5'); %We save in the sixth row
the number i data
i=i+1; %Next reading
end

figure(1) %we can plot the readings


i=1;
while (i<=6) %Plot each reading
plot(M(i,:));
hold on;
i=i+1;
end
%Graph characteristics
legend('A0','A1','A2','A3','A4','A5');
title('Reads in time of each sensor');
xlabel('Reads');
ylabel('Voltage signal');
ylim([0 5]);
hold off

i=1; %We calculate the mean to decrease the reading error and
noise
while(i<=6)
v(i)=mean(M(i,:)); %Mean of 50 readings for each sensor and
save it in v
i=i+1;
end

v %Show the mean values in matlab


i=1;
while(i<=6) %We calculate the R0 for each sensor
RS_Air(i) = (Vc-v(i))/(v(i)/RL(i)); %we calculate the
resistance air produced
RO(i) = RS_Air(i)/Ratio_Air(i); %We calculate the constant
values RO
i=i+1;
63

end
RO %Show RO
calibration=1; %calibration is finished
end
end

printLCD(lcd,'Calibration made');
printLCD(lcd,'Stop piston');
pause(7);
reading=0; %We desactivate the reading
writeDigitalPin(Mega2560,'D13',0); %Data is not being recorded
clearLCD(lcd);

C.2.3 Programs

%%%%%%%%%%%%%%%%%%%%%%%%
%We enter now in the program we selected
if (program==1) %Alcohol measuring program with sensor fusion
clearLCD(lcd);
[reading,stop] = waiting_time(reading,stop,lcd,Mega2560); %Calls
the function
while(stop==0)
s=4; %How many sensors we are going to use
[z,RS]=read_data(reading,M,z,s,Vc,RL,RS,Mega2560,lcd); %We get
RS back from this function
writeDigitalPin(Mega2560,'D13',0); %Data is not being recorded
n=1;
while(n<=4) %We calculate the concentration from 4 different
sensors
Ratio(n) = RS(n)/RO(n); %The ratio RS/RO
y (n) = Mult_Const_Alc(n)*(Ratio(n)^(Power_Alc(n))); %Put
the ratio in the formula
n=n+1;
end

Y = mean(y); %mean of 4 different sensors and save it in array


Y
show = num2str(Y);
printLCD(lcd,' ');
printLCD(lcd,'Alcohol ppm is:')%Show ppm values
printLCD(lcd,show); %Show the concentration
pause(10)
clearLCD(lcd);
reading=0;
[reading,stop] = waiting_time(reading,stop,lcd,Mega2560);
%Calls the function
end %No more readings Stop
end %Program 1 finished

%%%%%%%%%%%%%%%%%%%%%%%
if (program==2) %Any gas measuring program
clearLCD(lcd);
64

printLCD(lcd,'Program 2.');
printLCD(lcd,' ');
pause(4);
printLCD(lcd,'Put the sample,');
printLCD(lcd,'and press button');
pause(6);

[reading,stop] = waiting_time(reading,stop,lcd,Mega2560); % New


Readings
while(stop==0) %If stop==1, program finishes

%Choose which kind of gas we want to measure


printLCD(lcd,'Choose gas:');
printLCD(lcd, ' ');
pause(5)
i=1; %Program index variable
gas=0;
reading=0;
while(i<=6 && gas==0) %We have 6 different gases to choose
gas = choose_gas(gas,i,lcd,Mega2560); %Choose which gas we
want
pause(1);
i=i+1;
end

if (gas~=0)
s=6; %How many sensors we will use
[z,RS]=read_data(reading,M,z,s,Vc,RL,RS,Mega2560,lcd); %To
read data from sensors
printLCD(lcd, ' ');
writeDigitalPin(Mega2560,'D13',0); %Data is not being
recorded
n=1;
while(n<=6)
Ratio(n) = RS(n)/RO(n); %The ratio RS/RO
y (n) = Mult_Const(n)*(Ratio(n)^(Power(n))); %Put the
ratio in the formula and
%get the concentration of the gas
n=n+1;
end

y %Show ppm values


show = y(gas); %Show only the concentration of the gas we
chose
show = num2str(show);
printLCD(lcd,'Ppm is:')
printLCD(lcd,show);
pause(20)
clearLCD(lcd);
end
[reading,stop] = waiting_time(reading,stop,lcd,Mega2560); %A
new reading if we want
end %No more readings Stop
65

end %Program 2 is finshed

%%%%%%%%%%%%%%%%%%%%%%%
if (program==3) %3-Detecting when certain alcohol is in the piston.
%Beer is %7.2 alcohol, wine %14.5 and liquor %26
clearLCD(lcd);
printLCD(lcd,'Program 3.'); %Alcohol time measurement
pause(2);
stop=0;
reading=1;
pause(2);
while(stop==0)
[reading,stop] = waiting_time(reading,stop,lcd,Mega2560); %A
new reading if we want
clearLCD(lcd);
printLCD(lcd,'Choose alcohol');
pause(3)
i=1; %Program index variable
alcohol=0; %If we haven't chosen any alcohol
while(i<=3 && alcohol==0)
alcohol = choose_alcohol(alcohol,i,lcd,Mega2560); %Choose
which program we want
pause(1); %Wait
i=i+1; %Next program
end

if (alcohol~=0) %If we have chosen one alcohol


printLCD(lcd,'Press button');
printLCD(lcd,'to stop readings');
pause(3)
clearLCD(lcd);
printLCD(lcd,'Put the sample.');
pause(0.1)
printLCD(lcd,'Wait 15s');
pause(15);
end

%We make the readings


h=1;
reads = zeros(1,30);
while(reading==1 && stop==0 && h<=300 && alcohol~=0) %New
readings
i=1;
writeDigitalPin(Mega2560,'D13',1); %A signal to say that
data is able to be recorded
while(i<=30) %We make 30 readings with each sensor and save
it in a
M(1,i)=readVoltage(Mega2560,'A0'); %We save in the
first row the number i data
M(2,i)=readVoltage(Mega2560,'A1'); %We save in the
second row the number i data
M(3,i)=readVoltage(Mega2560,'A2'); %We save in the
66

third row the number i data


M(4,i)=readVoltage(Mega2560,'A3'); %We save in the
fourth row the number i data
i=i+1; %Next reading
end

i=1;
while(i<=4) %Means of s sensors
z(i)=mean(M(i,:));
i=i+1;
end
z; %Show mean values

%Results
i=1;
while(i<=4) %New resistances given by the gas
RS(i) = (Vc-z(i))/(z(i)/RL(i));
i=i+1;
end
n=1;
while(n<=4)
Ratio(n) = RS(n)/RO(n); %The ratio RS/RO
y (n) = Mult_Const_Alc(n)*(Ratio(n)^(Power_Alc(n)));
%Put the ratio in the formula
n=n+1;
end

Y = mean(y); %mean of 4 different sensors


reads(h) = Y;
h = h+1;
show = num2str(Y);
printLCD(lcd,'Alcohol ppm is:')%Show ppm values
pause(0.1)
printLCD(lcd,show);

if(Y<=upper_limit(alcohol) && Y>=down_limit(alcohol))


playTone(Mega2560,'D2',200,1);
end
pause(1)

if(readDigitalPin(Mega2560,'D4')==1)
reading=0;
end
clearLCD(lcd);
end
writeDigitalPin(Mega2560,'D13',0); %A signal to say that data
is able to be recorded
figure(2)
plot(reads);
reading = 0;
[reading,stop] = waiting_time(reading,stop,lcd,Mega2560);
printLCD(lcd,' ');
end
67

end

printLCD(lcd,'Finished');
end %Whole program is closed

C.2.4 Functions

%%%%%%%%%%%%%%%%%%%%%%%%%
function program=choose_program(program,i,lcd,Mega2560) %Function to
choose program

t=3;
while(t>=0 && program==0) %If timer>=0 and the program is not been
chosen
timer = num2str(t); %We convert the number to string format
switch i
case 1
printLCD(lcd,'1-Alcohol:'); %Print option 1
pause(0.1)
case 2
printLCD(lcd,'2-Gases:'); %Print option 2
pause(0.1)
case 3
printLCD(lcd,'3-Gas detecting:'); %Print option 3
pause(0.1)
end
printLCD(lcd,timer); %Print timer
pause(1); %Timer waits one second
t=t-1; %Countdown variable
button=readDigitalPin(Mega2560,'D4'); %Read button
if(button==1) %If the user has pressed the button
program=i; %We define the program to be used
end
end
clearLCD(lcd); %Clear LCD

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [reading,stop]=waiting_time(reading,stop,lcd,Mega2560)
%Function to make new readings or to stop
printLCD(lcd,'Press for new');
printLCD(lcd,'Press 3s finish');
while(reading==0 && stop==0)
button=readDigitalPin(Mega2560,'D4');%Read button
if(button==1) %If we presh it
reading=1; %We enable reading
pause(2.5)
button=readDigitalPin(Mega2560,'D4');%Read button
clearLCD(lcd);
if(button==1) %If the button is been pressed 2s, no more
68

readings
stop=1;
reading=0;
end
end
end
end

function [z,RS]=read_data(reading,M,z,s,Vc,RL,RS,Mega2560,lcd)
%Function to read new data
printLCD(lcd,'Press piston.')
while(reading==1) %New readings
pause(0.25);

if((readVoltage(Mega2560,'A8')<=0.5)&&(readVoltage(Mega2560,'A8')>=0.25
)) %If piston is up
i=1;
writeDigitalPin(Mega2560,'D13',1); %A signal to say
that data is able to be recorded
pause(2)
while(i<=10) %We make 10 readings with each sensor and
save it in a
M(1,i)=readVoltage(Mega2560,'A0'); %We save in the
first row the number i data
M(2,i)=readVoltage(Mega2560,'A1'); %We save in the
second row the number i data
M(3,i)=readVoltage(Mega2560,'A2'); %We save in the
third row the number i data
M(4,i)=readVoltage(Mega2560,'A3'); %We save in the
fourth row the number i data
M(5,i)=readVoltage(Mega2560,'A4'); %We save in the
fifth row the number i data
M(6,i)=readVoltage(Mega2560,'A5'); %We save in the
sixth row the number i data
i=i+1; %Next reading
end

i=1;
while(i<=s) %Means of s sensors
z(i)=mean(M(i,:));
i=i+1;
end
z %Show mean values

%Results
i=1;
while(i<=s) %New resistances given by the gas
RS(i) = (Vc-z(i))/(z(i)/RL(i));
i=i+1;
end

reading=0;
end
69

end %Last reading is finshed

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function gas=choose_gas(gas,i,lcd,Mega2560)
t=3;
while(t>=0 && gas==0) %If timer>=0 and the program is not been
chosen
switch i
case 1
printLCD(lcd,'1-Propane:'); %Print option 1
case 2
printLCD(lcd,'2-Methane:'); %Print option 2
case 3
printLCD(lcd,'3-LPG:'); %Print option 3
case 4
printLCD(lcd,'4-Alcohol:'); %Print option 4
case 5
printLCD(lcd,'5-CO2:'); %Print option 5
case 6
printLCD(lcd,'6-NH3:'); %Print option 6
end
timer = num2str(t);
printLCD(lcd,timer); %Print timer
pause(1); %Timer waits one second
t=t-1; %Countdown variable
button=readDigitalPin(Mega2560,'D4'); %Read button
if(button==1) %If the user has pressed the button
gas=i; %We define the program to be used
end
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function alcohol=choose_alcohol(alcohol,i,lcd,Mega2560)
t=3;
while(t>=0 && alcohol==0) %If timer>=0 and the program is not been
chosen
clearLCD(lcd);
switch i
case 1
printLCD(lcd,'1-Beer:'); %Print option 1
case 2
printLCD(lcd,'2-Wine:'); %Print option 2
case 3
printLCD(lcd,'3-Liquor:'); %Print option 3
end
timer = num2str(t);
printLCD(lcd,timer); %Print timer
70

pause(1); %Timer waits one second


t=t-1; %Countdown variable
button=readDigitalPin(Mega2560,'D4'); %Read button
if(button==1) %If the user has pressed the button
alcohol=i; %We define the program to be used
end
end
end

You might also like