Gas Sensor + Arduino Experimental Set Up
Gas Sensor + Arduino Experimental Set Up
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 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
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
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.
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
2.1.1 Sensors
2.1.2 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.
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:
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:
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.
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.
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.
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.
the output pins in the board. With this command a HIGH or a LOW value is
written in the selected pin.
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.
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.
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].
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:
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.
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
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:
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.
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
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.
has been calibrated with 20kOhm resistance in the RL. In this case will be used
to measure alcohol.
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.
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.
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
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
Here how the development of the project has been carried out is explained. It is
the most technical and important part of the project.
3.1.1 Piston
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–.
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
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.
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
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:
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
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:
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:
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.
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.
digital output; and finally the LED is connected to D13, as digital output also.
Here there is the whole schematic:
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.
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:
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:
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
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:
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:
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:
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.
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:
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
Wine %14.5
1
0,8
0,6
0,4
0,2
0
-0,2
Liquor %26
1,2
1
0,8
0,6
0,4
0,2
0
-0,2
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.
𝑙𝑜𝑔10 𝑁
𝑃𝐹𝐷 =
𝑙𝑜𝑔10 𝑁 + 𝑙𝑜𝑔10 (𝑁/(𝑁 + 0.4𝑁𝛿 )
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
12000
10000
y = 6E+07x-5,999
8000
6000
4000
2000
0
0 1 2 3 4 5 6 7 8 9
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:
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.
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:
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):
The second function is used to make new readings or to stop the whole program:
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
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”.
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
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:
CHAPTER 4
Conclusions
4.1 Summary
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.
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.
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.
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
http://www.dx.com/es/p/fc-22-1-harmful-gas-detector-sensor-module-for-
arduino-179225#.WSfqp1IrzR0
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.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 C
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.
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.1 Declaration
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.
C.2.2 Calibration
%Initialize
printLCD(lcd,'Hello!');
printLCD(lcd,'Choose program.');
pause(3);
clearLCD(lcd);
%Calibration
reading=0;
calibration=0;
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; %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
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
%%%%%%%%%%%%%%%%%%%%%%%
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);
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
%%%%%%%%%%%%%%%%%%%%%%%
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
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
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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