You are on page 1of 71

UNIVERSITY OF NAIROBI

FACULTY OF ENGINEERING
DEPARTMENT OF ELECTRICAL AND INFORMATION ENGINEEERING

MICROCONTROLLER BASED COOKING GAS


LEAKAGE DETECTOR
PROJECT INDEX: 004

SUBMITTED BY

NJUE GEORGE FUNDI


F17/1419/2011
SUPERVISOR: PROF. ELIJAH MWANGI
EXAMINER: SEYYID AHMED

PROJECT REPORT SUBMITTED IN PARTIAL FULFILLMENT OF THE REQUIREMENT


FOR THE AWARD OF THE DEGREE OF BACHELOROF SCIENCE IN ELECTRICAL
AND ELECTRONIC ENGINEERING OF THE UNIVERSITY OF NAIROBI 2016

SUBMITTED ON: 5/17/2011

i
DECLARATION OF ORIGINALITY

NAME: NJUE GEORGE FUNDI

REG NUMBER: F17/1419/2011

COLLEGE: ARCHITECTURE AND ENGINEERING

SCHOOL: ENGINEERING

DEPARTMENT: ELECRICAL AND INFORMATION ENGINEERING

COURSE: BACHELOR OF SCIENCE IN ELECTRICAL AND


ELECTRONICS ENGINEERING

TITLE OF WORK MICROCONTROLLER BASED COOKING GAS LEAKAGE


DETECTOR

I understand what plagiarism is and I am aware of the university policy on this regard.
I declare that this final year project report is my original work and has not been
submittedelsewhere for examination, award of a degree or publication. Where other people’s
work or my own work has been used, this has properly been acknowledged and referenced in
accordance with the University of Nairobi’s requirements.
I have not sought or used the services of any professional agencies to produce this work.
I have not allowed, and shall not allow anyone to copy my work with the intention of passing it
off as his/her own work.
I understand that any false claim in respect of this work shall result in disciplinary action, in
accordance with University anti-plagiarism policy.

Signature:
……………………………………………………………………………………
Date:
…………………………………………………………………………

ii
CERTIFICATION

This report has been submitted to the Department of Electrical and Information Engineering in
the University of Nairobi with my approval as supervisor.

…………..………………………………
Prof. ELIJAH MWANGI

Date:………………………

iii
DEDICATION

To my dear mother for her unconditional love and support throughout my life

iv
ACKNOWLEDGEMENT

I thank the Almighty God for always keeping me safe and seeing me through my academic life
I thank my supervisorProf Elijah Mwangi for his valuable insights into my project and his
commitment in helping me finish my project. Thank you so much.
I recognize the effort by my lecturers and everyone else at the Department of Electronic and
Information Engineering to see to it that am completing my course
I really appreciate the SWA fraternity for hosting me for the 5 years that have been at the
University of Nairobi.
I thank the Class Representative, Mr. Samuel Khaemba for selflessly serving us well and my
classmates for being so supportive.
I thank my dear friends James Muchiri and Agnes Githinji for their support for the time I have
been here.

v
TABLE OF CONTENTS

Contents

DECLARATION OF ORIGINALITY .............................................................................................. ii

CERTIFICATION ....................................................................................................................... iii

DEDICATION ............................................................................................................................. iv

ACKNOWLEDGEMENT ......................................................................................................................... v

TABLE OF CONTENTS ........................................................................................................................ vi

LIST OF FIGURES ........................................................................................................................................ x

LIST OF TABLES ......................................................................................................................................... xi

ABSTRACT .......................................................................................................................................... xii

LIST OF ABBREVIATION.....................................................................................................................xiii

Chapter 1: INTRODUCTION ...................................................................................................... 1

1.1 Background.................................................................................................................. 1

1.2 Problem Statement ...................................................................................................... 1

1.3 Project Objective .......................................................................................................... 2

1.3.1 Overall Objective ......................................................................................................... 2

1.3.2 Specific Objectives ...................................................................................................... 2

1.4 Project Justification ...................................................................................................... 3

1.5 Scope of Work ................................................................................................................... 3

Chapter 2: LITERATURE REVIEW ............................................................................................. 4

2.1 Introduction ....................................................................................................................... 4

2.2 Operating Principle of MQ-6 Sensor ................................................................................... 6

2.3 The PIC16F690 Microcontroller ......................................................................................... 9

2.3.1 Introduction ................................................................................................................. 9

2.3.2 The PIC16F690 MCU Architecture............................................................................... 9

vi
2.4 Hitachi’s HD44780 LCD Display ........................................................................................10

2.4 Buzzer ..............................................................................................................................13

2.5 Power Supply Unit ............................................................................................................14

Chapter 3: Design Methodology.................................................................................................15

3.1 Hardware Design .............................................................................................................15

DESCRIPTION OF THE BLOCK DIAGRAM ........................................................................................ 15

3.1.1 MQ-6 Gas Sensor ......................................................................................................15

3.1.2 PIC16F690 MCU .......................................................................................................16

3.1.3 LCD Display...............................................................................................................18

3.1.3 Buzzer .......................................................................................................................19

3.1.4 LEDs .................................................................................................................................... 19

3.1.5 Button ........................................................................................................................19

3.1.6 Power Supply. ............................................................................................................19

3.1.7 Schematic Diagram for the Detector ...........................................................................20

3.2 Software Design ...............................................................................................................21

vii
3.2.1 Flowchart Diagram

...................................................................................................................................................... 21

3.2.2 Programming the PIC16F690 ..................................................................................................... 22

Chapter 4: RESULTS.................................................................................................................................... 31

viii
4.1 Proteus Simulated Results and Practical Results ..............................................................31

4.1.1 Procedures: Simulated results ....................................................................................31

4.1.2 Procedures: Practical result ........................................................................................31

4.2 Analysis of the Results ......................................................................................................33

4.3 Discussion........................................................................................................................34

Chapter 5: CONCLUSION AND RECOMMENDATIONS ..................................................................... 37

5.1 Conclusion .......................................................................................................................37

5.2 Recommendations ...........................................................................................................37

REFERENCES ..................................................................................................................................... 39

APPENDIX ........................................................................................................................................... 40

Appendix A: MQ-6 Details ......................................................................................................40

Appendix B: PIC16F690 Details .............................................................................................41

Appendix C: LCD Details ........................................................................................................43

Appendix D ............................................................................................................................45

BILL OF QUANTITIES ...................................................................................................................... 46

THE PROGRAM CODE .................................................................................................................... 47

ix
LIST OF FIGURES

FIGURE 2.1 MQ-6 GAS SENSOR ....................................................................................................................................................................................... 6


FIGURE 2.2 MQ-6 SENSOR DRIVE CIRCUIT ..................................................................................................................................................................... 7
FIGURE 2.3 MQ-6 SENSOR SENSITIVITY CURVE ............................................................................................................................................................ 8
FIGURE 2.4: PIC16F690 PIN DIAGRAM....................................................................................................................................................................... 10
FIGURE 2.5 HITACHI'S HD44780 LCD PIN OUT.......................................................................................................................................................... 11
FIGURE 2.6 5V POWER SUPPLY USING 7805 VOLTAGE REGULATOR ...................................................................................................................... 14
FIGURE 3.1. THE GAS LEAKAGE DETECTOR BLOCK DIAGRAM .................................................................................................................................... 15
FIGURE 3.2 SENSOR CONNECTION TO THE MCU .......................................................................................................................... 16
FIGURE 3.3 CONNECTION OF VARIOUS COMPONENTS TO THE PIC16F690 MICROCONTROLLER ........................................................................ 17
FIGURE 3.4 GAS LEAKAGE DETECTOR............................................................................................................................................................................ 18
FIGURE 3.5 LCD CONNECTION TO THE MCU .............................................................................................................................. 18
FIGURE 3.6 INTERFACING OF THE BUZZER TO THE MCU ................................................................................................................ 19
FIGURE 3.8 SCHEMATIC DIAGRAM FOR GAS LEAKAGE DETECTOR ............................................................................................................................ 20
FIGURE 3.9 FLOWCHART FOR THE PROGRAM .............................................................................................................................................................. 21
FIGURE 3.10 MESSAGE DISPLAY ON THE LCD WHEN THERE IS GAS LEAKAGE .......................................................................................................... 29
FIGURE 3.11 MESSAGE DISPLAY ON THE LCD WHEN THERE IS NO GAS LEAKAGE .................................................................................................... 30
FIGURE B.1 PIC16F690 BLOCK DIAGRAM ............................................................................................................................. 41

x
LIST OF TABLES

TABLE 2.1 QAPASS HD44780 LCD PIN OUT DESCRIPTION .................................................................................................................... 11


TABLE 3.1 MICROCONTROLLER PINS ALLOCATION TO THE PERIPHERALS ............................................................................................... 16
TABLE 3.2 VOLTAGE GAS CONCENTRATION RELATIONSHIP ......................................................................................................................... 27
TABLE 3.3 SHOWS THE MULTIPLYING FACTOR FOR EACH RANGE ................................................................................................... 27
TABLE 3.4 EXAMPLE OF CONVERSION OF VOLTAGE TO GAS CONCENTRATION IN PPM ............................................................ 28
TABLE 4.1 THE SIMULATED RESULTS USING PROTEUS SIMULATION SOFTWARE AND THE PRACTICAL RESULTS USING MQ-6 SENSOR .........32
TABLE 4.2 ANALYSIS OF RESULTS ................................................................................................................................................................... 33
TABLE A.1 TECHNICAL PARAMETERS FOR MQ-6 SENSOR ......................................................................................................................... 40
TABLE B.1 A SUMMARY OF PIC16F690 PINS .................................................................................................................................. 43
TABLE C.1 LCD COMMANDS ............................................................................................................................................................... 43
TABLE D.1 PIC16F690 INSTRUCTION SET ....................................................................................................................................... 45

xi
ABSTRACT

Liquefied Petroleum Gas (LPG) is a main source of fuel especially in urban areasbecause it is
clean compared to firewood and charcoal. There is always a danger of the gas leakage as a result
of negligence or failure on the regulating valve on the gas cylinder which pose a great danger
due to highly flammable nature of the gas. Cases of gas related fire has been on the rise and this
can be avoided using a gas leakage detection system and thus theneed for development of a
microcontroller based cooking gas detector. The use of microcontroller enables development of a
high accurate and fast response detection system.The detector incorporate MQ-6 sensor (with gas
detection range of 300-10000ppm) as the LPG gas sensor, PIC16F690 microcontroller as the
control unit, LCD for displaying gas concentration, a buzzer as an alarm and a number of LEDs
to indicate the gas leakage status. The microcontroller senses the presence of a gas when the
voltages signal from the MQ-6 sensor goes beyond a certain level and gives an audiovisual
alarm. The microcontroller is programmed using PIC assembly language and all the peripherals
connected to it through it pins. When the system is powered on the microcontroller lit a green
LED to show the absence of a gas leakage. LPG gas is released and the sensor voltage signal
monitored using a digital multimeter. Below 2.0V, the green LED is kept lit and when the
voltage is more or equal to 2.0V, the microcontroller blinks a red LED and set off an alarm to
show the presence of a gas. The detector has a button with which the alarm can be
acknowledged. The sensor as a high resistance in clean air. In the presence of LPG gas, the
sensor conductivity increases and the characteristic of the sensor is that at 2.0V output from the
sensor, the gas concentration is 300ppm, thus the trigger level is 2.0V. Therefore, the
microcontroller based gas leakage detector based on PIC16F690 microcontroller and MQ-6
sensor is able to detect gas leakage concentration from 300ppm and give an audiovisual signal.

xii
LIST OF ABBREVIATION

LPG Liquid Petroleum Gas


LNG Liquefied Natural Gas
MCU Microcontroller
PPM Parts per Million
PIC PIC16F690
SFRs Special Function Registers.
GPRs General Purpose Registers.
BCD Binary Coded Decimal

xiii
Chapter 1: INTRODUCTION

1.1 Background

Gas is a leading source of energy used for cooking and heating in our homes. It provide an
economical and clean source of energy compared to other source of energy like firewood. It is
also used as a source of energy in industry and fueling our cars. The two major type of gas used
are Natural Gas and Liquefied Petroleum Gas(LPG) both of which are hydrocarbons gas.
Natural Gas is basicallymethanegas ( ) while LPG gas is a mixture of butane ( ) and
propane gas ( ) or purely butane or propane gas. Natural gas is extracted from underground
while LPG gas is a by-product of Natural Gas and crude oil processing. These gases have a
characteristic of being highly flammable and can cause suffocation in high concentration. It is
because of this, the usage of gases should be done with great care and safety standards are put in
place to ensure everyone using the gas is safe. Gas used for cooking is supplied in gas cylinders
which have a regulating valve. After using the gas you are supposed to turn off the gas.The
supplier of the gas should make sure the valve are working well and not leaking the gas.
Observation of the safety standards would avoid the dangers posed by the gas. However, systems
made by human being are bound to fail at one point due to wearing out, accident or by intention.
Also by accident, we might forget to turn off the gas. This poses an immediate danger to life and
property due to the flammable and intoxicating nature of the gas. As an engineer, it is a
lifesaving task to design a LPG gas detector capable of raising an alarm and showing the
concentration of the gas leakage. Thus we are going to design a microcontroller based cooking
gas detector.

1.2 Problem Statement

To design and implement a microcontroller based cooking gas leakage detector capable of giving
an audiovisual alarm. There will always be a risk of gas leakage whenever we are using the gas.
If the leakage of the gas goes undetected it leads to undesirable consequences. Fire related to gas
leakage have not only destroyed millions worth of properties but also have cost life.A fire related
to gas leakage destroyed a nursery school in Kisii [4]. I have witnessed one case of a neighbor’s
house being destroyed by fire caused by gas leakage. Gas leakage alsopollute the environment by

1
adding to the greenhouse effect. In the light of this, a LPG gas leakage detector can help so much
in alleviating this problem.
LPG gas is odorless and a small amounts of a pungent gas such as ethanethial is added to help
people smell a gas leakage. However this is not reliable as you may not smell the gas hence the
need for a gas leakage detector. One such kind of a sensor is a microcontroller based gas leakage
detector. The detector is capable of raising an alarm, showing the concentration of the gas
leakage and has gas leakage status LEDs. A red LED when there is a gas leakage and a green
LED when there is no gas leakage.
To design such a detector, a gas sensor (MQ-6 LPG gas sensor), a microcontroller (PIC16F690),
a LCD Display, a buzzer and a number of LEDs ar required. The MQ-6 sensor uses Tin Dioxide
as the sensing material and as is highly sensitive to Propane and Butane gas and less sensitive to
other gases like carbon Monoxide and smoke. The microcontrolleris used to process the signal
from the sensor and set off the alarm, the LCD and the LEDs depending on the output voltage
from the sensor. The LCD is used to show the concentration of the gas leakage in PPM
The success of designing the LPG gas leakage detector will help to efficiently detect the leakage
of LPG gas and avoid risk of fire and pollution, saving life and property.

1.3 Project Objective

1.3.1 Overall Objective


To design a microcontroller based cooking gas leakage detector capable of giving an audio-
visual alarm.

1.3.2 Specific Objectives


To design a low cost and an effective PIC16F690 MCU based LPG gas detector unit to detect the
leakage of the LPG gas and sound an alarm through a buzzer.
To incorporate a LCD display to show the concentration of the gas leakage
To incorporate LEDs to show the status of the gas leakage. A Red LED when there is gas
leakage and a Green LED when there is no leakage.
To incorporate an alarm acknowledging button.

2
1.4 Project Justification

There is always a risk of gas leakage whenever we are using gas which poses a great danger to
both human live and property. Therefore designing a gas leakage detector and making it
available for a cheap price would help in detecting possible gas leakage and avoiding the
possibility of destruction of property and lose of lives

1.5 Scope of Work

LPG gas is the gas mainly used for cooking. The gas leakage detector being designed is limited
to detecting LPG gas only. It can be used at LPG gas sales point, homes, restaurant and any other
place where a LPG gas is being used for heating or cooking. Since the device uses a MQ-6
sensor it cannot be used to detect Natural gas or any other methane base gas. Also, it cannot be
used as smoke detector in case of fire.

3
Chapter 2: LITERATURE REVIEW

2.1 Introduction

LPG gas is the main gas fuel used for cooking and other heating appliances. This is because it is
a stable, high energy content, relatively low Sulphur, clean burning fuel which can be transported
economically as a liquid[1].It is a by-product of crude oil and Natural Gas processing[2].
LPG is primarily composed of propane, butane, isobutane or a mixtures of these gases[2]. It is
stored in gas cylinders as liquid under relatively low pressure. It has low boiling point and it will
vaporize immediately it is released into the air. It is relatively heavier compared to air. When it
leaks, it will tend to flow close to the ground and settle in low lying places in the premises if
there is no adequate air ventilation. LPG gas is highly flammable gas with Lower Explosive
Limits of about 1.4% that is about 14,000PPM[3]. This is the property of LPG that makes us
worried when using the gas[2].
No matter the safety standards that are put in place in usage of the gas to ensure there is no gas
leakage, there is always the danger of a gas leakage. Human being are prone to error. LPG gas is
odorless and colorless and it would be impossible to detect the presence of a gas leakage. An
odourant is normally added to the gas to help detect the gas in case of a leakage[2] but this is not
enough as it would require person to be physically present to detect the gas leakage and by the
time the gas has built up enough to be detected by smell it will have reached dangerous
concentration level. Therefore, it is a great risk to relay on our sense of smell to determine
whether there is a gas leakage or not.
This has been a great concern for people over time and people have come up with gas leakage
detector to solve this problem.
The gas leakage detectors that have been developed have been built around already existing
different gas technology[4]. The different type of sensor are listed below[5].
 Electrochemical sensors-mostly used for toxic gases such carbon monoxide
 Metal Oxide Semiconductor sensors- used for both toxic and combustible
 Catalytic sensor-used for combustiblegases such hydrocarbon gases
 Infrared Sensors- used for combustible gases

4
The choice of the sensor depends on the type of gas that is to be detected among other factors
such as the stability, sensitivity, selectivity, price and durability of the sensor. For hydrocarbon
gases such as LPG gas, metal oxide semiconductor sensors are preferred over the rest of sensor
because they are relatively cheap and last for a long time being stable, sensitive to low gas
concentrations (300PPM) and detect a wide range of gas concentration (300-10000) PPM and
resistant to poisoning[4]. However, its performance is affected by exposure to high corrosive
gases (such as hydrogen chloride), organic silicon steam, halogen pollution and water[6].
Metal oxide semiconductor detect gases by means of their surface interaction with the target gas
which alters the conductivity of the semiconductor. The output voltage signal is converted into
gas concentration. Tin oxide and Tungsten Oxide are kind of metal oxide used as the sensing
material in metal oxide semiconductor sensor[4].
TGS sensor by Figaro[7] and MQ-6 sensor[6] are metal oxide semiconductor based sensors that
can be used for detecting LPG gas. The MQ-6 sensor offers a [6]
 High Sensitivity to LPG gas
 A Detection Range: 300 - 10,000 PPM
 Fast Response Time: <10s
 Simple drive circuit
 Heater Voltage: 5.0V
 Long lifespan
 Low cost
A gas leakage detection system capable of raising an alarm has been developed using the sensor
and a comparator. The sensor output voltage is compared with a reference voltage from a
potentiometer and if the voltage signal is higher the reference voltage the comparator output a
signal which can be used to drive a circuit setting off an alarm and lit a number of LEDs. The
potentiometer is used to set the trigger level for the alarm[8].
There are some commercial LPG gas detector in the market that detect gas leakage and raise a
warning in form of an alarm and LED indicators. However, these commercialized gas detector
come at a high price and a not readily available on Kenya market[9], [10].
Therefore, there is a need for designing a gas leakage detector that can be readily available in the
local market at an affordable price. The detector is based on a microcontroller with a high
resolution Analog to Digital Converter (10 bits)[11]. The detector in additional to raising an

5
alarm and having LED indicators for status of the gas leakage it is capable of accurately
displaying the concentration of the gas on a LCD display. It also incorporate a button that can be
used for acknowledging the alarm. The microcontroller is programmed with an algorithm for
converting the voltage signal from the sensor to the respective gas concentration according to the
sensor datasheet. The datasheet outline the relationship between the sensor voltage and the gas
concentration.Thus, gas concentration can be displayed with high accuracy. The microcontroller
runs at high frequency, thus the detector has a faster response time to changes in concentration of
the gas.
The specific sensor to be used for this detector is MQ-6 sensor which uses Tin Oxide as the
sensing material. The microcontroller being used is PIC16F690 microcontroller by Microchip.
The LCD being used is Hitachi HD44780 LCD. The detector will use a Buzzer as the alarm. All
this components are powered using a 5V supply. The 5V is supplied using available 5V voltage
regulators ICs and a 9V battery.

2.2 Operating Principle of MQ-6 Sensor

The Figure 2.1 shows a photo of MQ-6 sensor. In clean air, the sensor has a high resistance and
in presence of a gas the sensor conductivity increases. The sensor has a simple drive circuit
shown in Figure 2.2.The sensor is driven from a 5V supply. A voltage (heating voltage) is
applied between Pin 2 and 5 with a resistance of 26 ± 3Ω to heat the sensor to the working
temperature[6].When Tin Oxide is pre-heat in presence of oxygen, oxygen is adsorbed on the
crystal surface with negative charges. The donor electrons on the crystal are transferred to the
adsorbed oxygen thus leaving positive charges in a space charge layer. This create a surface
potential which acts as potential barrier against electrons flow hence the high resistance of the
sensor in clean air[7].In presence of reducing gas such as LPG, the gas molecules are adsorbed
on the material surface reducing the surface density of the negatively charged Oxygen ions thus
increasing concentrations of electrons and the conductivity of the sensor[7].[4].

VCC pin

Figure 2.1 MQ-6 Gas Sensor


Ground pin

Digital Output

Analog Output
The Sensor Sensitive Part.

Figure 2.2 MQ-6 sensor Drive Circuit

Therefore, as the gas concentration increasetheconductivity of the sensor will increase and so
does the sensor output voltage. However, this relationship is not linear and Figure 2.3 describe
this relationship[6]. This sensor resistance is between pins 6&4 and 1&3 and a loop voltage is
applied between the series of the sensor resistance and the load resistance . The sensor output
voltage is the voltage across the load resistance. The load resistance is used to change the
sensitivity of the sensor. A very high resistance, reduce the sensor sensitivity and small changes

7
in the concentration of the gas would not be noticed. MQ-6 sensor has a load resistance of
4.7K[6].

Figure 2.3 MQ-6 Sensor Sensitivity Curve

The sensor detect the gas concentration from a range of 300-10,000PPM. The sensitivity curve
of the sensor form the basis for setting the alarm trigger level and the amount of gas
concentration for a given voltage. From the curve, the sensor has an output voltage of 2.0V at
300 PPM and thus the trigger level is 2.0V. When the input voltage to the MCU is equal to or
more than 2.0V, the MCU starts the audiovisual alarm. Although the relation between gas
concentration and sensor voltage is not linear, there are ranges of sensor output voltage with
constant gradient i.e. between 2.0V and 2.5V, for every increase of 20 PPM in gas concentration
there is an increase of 0.1V. Thus gas concentration for any other voltage is through
extrapolation from the known values of sensor voltage and gas concentration from Figure 2.3.
NB: MQ-6 sensor technical specifications are outline in the Appendix A, Table A.1

8
2.3 The PIC16F690 Microcontroller

2.3.1 Introduction
A microcontroller (MCU) is a microprocessor with memory, input and output (I/O) pins and
other additional peripheral features such as Timers/Counters on one chip[12] depending on the
type of the microcontroller. The PIC16F690 MCU is mid-range microcontroller of the PIC
family of microcontrollers produced by Microchip Technology Incorporation. It is a 8-bit, flash
memory based, 20 pins CMOS MCU with nanoWatt technology[11]. It has a RISC CPU with 35
instructions[11]. The additional peripherals on the PIC16F690 MCU, include; ADC Module,
Analog Comparator Module, Timers/Counters, In-circuit Serial Programming, Enhanced
Capture, Compare, PWM+ module and Synchronous Serial Port (SSP). It has Low power
features and other special features such as; Precision Internal Oscillator (with a maximum speed
of 8MHz), Power Saving Sleep mode, Wide Range of Operating Voltage (2.5V-5.5V), Power-
On Reset, Flash memory with 100,000 writes among other features[11].The full description of
the features of PIC16F690 is shown in Section B.1 in Appendix B

2.3.2 The PIC16F690 MCU Architecture


The MCU uses Harvard Architecture, data and program are accessed via separate data bus. The
internal hardware of the MCU is shown in Figure B.1 in Appendix B. The MCU can be divided
into two part; the program execution section and the register processing section[12]. The
program execution section consists of the program memory, instruction register and the control
logic. PIC16F690 has a 13 bits program counter thus 8K * 14 of program memory but only the
first 4K of memory is implemented[12]. PIC16F690 uses 14 bits instruction. Thus the code
written should not exceed 4K.
The register processing section consists of the static RAM and the Arithmetic and Logic Unit
(ALU). The ALU uses a register called the Working Register which is used to temporary store
data during execution[13]. All other register used by the CPU are stored in the RAM which is
arranged in 4 Banks each of 128 register each 8 bits. There two type of registers; Special
Function Register (SFR) and General Purpose Register (GPR). The SFR are used for controlling
processor operations and showing the status of the processor. These SFR are mapped in the first

9
32 locations of each banks. Such SFR are STATUS, INTCON, OPTION_REG, TRIS and the
PORT registers. The GPR space holds the user defined variable. [12].
The PIC uses the port registers (PORTA, PORTB and PORTC) to get data from the peripherals
attached to it and to output data to the peripherals attached to it[12]. The PIC has 18 general
purpose I/O pins but one pin is an input pin only. The pin out of PIC16F690 is shown in Figure
2.4 [11]

Figure 2.4: PIC16F690 Pin Diagram

The pin functionality as a General purpose I/O depends on whether a peripheral feature on that
pin has been enabled[11]. A pin configured as input for the comparator module cannot be used a
digital input pin. The complete description of the functionalities of each pin is in Table B.1 in
Appendix B[11].

2.4 Hitachi’s HD44780 LCD Display

The Hitachi HD44780 controlled 16*2 LCD will be used for display of the gas concentration.
This LCD displays alphanumeric characters (letters, numbers and symbols) which can be used to
convey adequate information concerning the gas concentration status. The fact that it is built
around the Hitachi HD44780 controller makes the LCD a smart device[14].The LCD has
controls lines and data lines which makes it possible to send information for displaying on the
LCD by putting the controls lines and data lines high or low[15]. Therefore, the LCD can be
interfaced to the microcontroller pins and information be sent to the LCD by controlling the

10
microcontroller. Figure 2.5 shows the LCD pin out and Table 2.1 the description of the function
of each pin[15].

Figure 2.5 Hitachi's HD44780 LCD pin out

Table 2.1 QAPASS HD44780 LCD Pin out Description

Pin No Name Description


1 VSS
Power Supply(GND)
2 VDD
Power Supply
3 VEE
Contrast adjust
4 RS
0 = Instruction input
1 = Data input

11
5 R/W
0 = Write to LCD module
1 = Read from LCD module
6 EN
Enable signal
7 D0
Data bus line 0 (LSB)
8 D1
Data bus line 1
9 D2
Data bus line 2
10 D3
Data bus line 3
11 D4
Data bus line 4
12 D5
Data bus line 5
13 D6
Data bus line 6
14 D7
Data bus line 7(MSB)
15 LED +
LCD Backlight (Anode)
16 LED -
LCD Backlight (Cathode)

The control lines are RS, R/W and EN and data lines (D0-D7) whose functions are described in
Table 2.1
The 16*2 display means that only 2 lines of 16 characters can fit on the LCD display screen[16].
The Hitachi’s HD44780 LCD has an integrated 80 characters display data buffer (DDRAM-
Data Display RAM) and an integrated character generator (CGROM-Character Generator
ROM). It has also CGRAM- Character Generator RAM for user defined characters. The LCD is

12
accessed through the instruction register or the data register. When sending commands such
turning the display off, the command is sent to the instruction register through the data lines
while when sending data, the data is passed to the data register through the data lines. The two
are differentiated with the RS control line. When RS=0 and R/W=0, the controller sees the data
placed on the datalines as a command and when RS=1 as the data to be displayed on the LCD.
When data or command value is placed on the data lines it sent to the LCD when an Enable pulse
signal is sent the LCD. An Enable pulse signal is sent when EN pin is pulled HIGH and then
LOW with a small delay of about 500ns between. However, a microcontroller running at an
instruction cycle of 1MHz will provide the necessary delay between putting EN HIGH and
LOW[15].
The characters are displayed by sending their ASCII’s value to a DDRAM address location
through the data register and the CGROM generates the specified character to be displayed at
that location[15].
The LCD is used in 8 bit mode and all the 8 data lines (D0-D7) are connected to PORTC pins.
The LCD can be used in 4 bit mode where only D4-D7 pins are used to transfer data and
commands. Although this safes the MCU pins for other peripherals, data can only be sent in
nibbles thus requiring more lines of code and takes more time to executes compared to the 8 bit
mode where data is sent in bytes[15].
NB: Table C.1 shows the list of commonly used commands with the LCD.

2.4 Buzzer

The buzzer used is Piezoelectric Active Buzzer. It uses the inverse relationship of
piezoelectricity. When an alternating current is applied to piezoelectric material such as
Piezoceramic, they stretch and compress depending on the frequency of the signal producing a
sound. The active Buzzer has a built in oscillator circuit and when applied with DC voltage will
produce a consistent sound.[17]

13
2.5 Power Supply Unit

The sensor, Buzzer and the MCU are require a 5V D.C voltage for powering up. Such a power
supply is design using a 5V voltage regulator.The 5V power supply can be achieved using the
circuit shown in Figure 2.6

U2
7805

1 3
VI VO 5V

GND
B1

2
12V C1
1uF C2
1uF

Figure 2.6 5V Power Supply using 7805 Voltage Regulator

The circuit uses a 7805 voltage regulator which output 5V. Capacitor C1 filters out the noise
from source voltage which is 9V battery by shorting the AC signal in the source voltage into the
ground and allowing the DC signal only to pass. Capacitor C2 is used to filters out any AC signal
in the output DC voltage[18].

14
Chapter 3: Design Methodology

3.1 Hardware Design

The specifications of the cooking gas leakage detector are being to detect a LPG gas leakage and
give an audiovisual warning. The detection system also has an alarm acknowledgement button.
The device is powered from a 5V supply from a 9V battery. Therefore, the following
components are required to make the gas detector; MQ-6 Sensor, PIC16F690 MCU, LCD
Display, Buzzer, RED LED, GREEN LED,BLUE LED, a Button and current limiting resistors.

The block diagram in Figure 3.1 show the interconnection of the components to make the
Device.

LCD Display

PIC16F690 MCU
Sensor BUZZER

BUTTON LEDs

Figure 3.1. The gas leakage detector Block Diagram.

DESCRIPTION OF THE BLOCK DIAGRAM


3.1.1 MQ-6 GasSensor
The sensor is used to detect when there is a gas leakage and the amount of the gas concentration
by giving out a voltage output depending on the gas leakage concentration

15
Interfacing the sensor to the MCU
The sensor gives an analog voltage input to the MCU. The sensor is connected to analog channel
1 of the ADC on pin RA0 as shown in the Figure 3.2. A potentiometer is used to simulate the
analog voltage from the sensor. The analog signal is converted to a gas concentration value with
reference to the curve in Figure 2.3

RV3
U2
19 16
50%

RA0/AN0/C1IN+/ICSPDAT/ULPWU RC0/AN4/C2IN+
18 15
RA1/AN1/C12IN0-/VREF/ICSPCLK RC1/AN5/C12IN1-
17 14
RA2/AN2/T0CKI/INT/C1OUT RC2/AN6/C12IN2-/P1D
4 7
RA3/MCLR/VPP RC3/AN7/C12IN3-/P1C
1k 3 6
RA4/AN3/T1G/OSC2/CLKOUT RC4/C2OUT/P1B
2 5
RA5/T1CKI/OSC1/CLKIN RC5/CCP1/P1A
8
RC6/AN8/SS
13 9
RB4/AN10/SDI/SDA RC7/AN9/SDO
12
RB5/AN11/RX/DT
11
RB6/SCK/SCL
10
RB7/TX/CK
PIC16F690
Figure 3.2 Sensor connection to the MCU

3.1.2 PIC16F690 MCU


The microcontroller is the control unit. All the peripherals, Sensor, alarm, LEDs, LCD and
button are connected to it through the interfacing pins. It monitors the input signal from the
sensor. When the signal get above the trigger level, theMCU set off the alarm, blinks a red LED
and display the concentration of the gas leakage on the LCD. When the signal is below the
trigger level, a green LED is lit. When the device is powered up the MCU light a blue LED to
show Power ON status.
PIC16F690 Pin Allocations
The various peripherals are interfaced to the microcontroller through the following pins as
described in Table 3.1

Table 3.1 Microcontroller Pins Allocation to the Peripherals

Component I/O Functionality


Sensor RA0 Analog Input

16
Button RA5 Digital Input
Buzzer RA1 Digital Output
Red LED RA2 Digital Output
Green LED RA4 Digital Output
Blue LED RB7 Digital Output
LCD (D0-D7)
Data Lines RC0-RC7 Digital Output
LCD RS pin RB4 Digital Output
LCD EN pin RB6 Digital Output

Figure 3.3 shows the interface of the various components to the PIC16F690 microcontroller on a
breadboard and Figure 3.4 shows fabricated circuit.

MQ-6 Sensor Buzzer Leakage Indicator LED PIC16F690 HD44780 LCD

Buzzer

Figure 3.3Connection of various components to the PIC16F690 microcontroller.

Button NO Leakage LED Power LED Resistor

17
Figure 3.4 Gas leakage detector

3.1.3 LCD Display


The LCD shows the numerical value of the gas concentration in Parts per Million. The LCD is
interfaced to the microcontroller as shown in Figure 3.5

RV3
LCD2
40%

LM016L
VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E

1k
11
10

12
13
14
1
2
3

4
5
6

7
8
9

U2
19 16
RA0/AN0/C1IN+/ICSPDAT/ULPWU RC0/AN4/C2IN+
18 15
RA1/AN1/C12IN0-/VREF/ICSPCLK RC1/AN5/C12IN1-
17 14
RA2/AN2/T0CKI/INT/C1OUT RC2/AN6/C12IN2-/P1D 7
4 RC3/AN7/C12IN3-/P1C
RA3/MCLR/VPP 6
3 RC4/C2OUT/P1B
RA4/AN3/T1G/OSC2/CLKOUT 5
2 RC5/CCP1/P1A
RA5/T1CKI/OSC1/CLKIN 8
RC6/AN8/SS
13 9
RB4/AN10/SDI/SDA RC7/AN9/SDO
12
RB5/AN11/RX/DT
11
RB6/SCK/SCL
10
RB7/TX/CK
PIC16F690

Figure 3.5 LCD Connection to the MCU

18
The RW pin is permanently grounded because we will be only sending data and command to the
LCD. The data lines D0-D7 are connected to PORTC while the control lines RS and EN to RB4
and RB6 respectively.
3.1.3 Buzzer
It gives an audio alarm when there is gas leakage. The buzzer is interfaced to the MCU through
pin RA1 as shown in Figure 3.5

BUZ2

BUZZER

U2
19 16
RA0/AN0/C1IN+/ICSPDAT/ULPWU RC0/AN4/C2IN+
18 15
RA1/AN1/C12IN0-/VREF/ICSPCLK RC1/AN5/C12IN1-
17 14
RA2/AN2/T0CKI/INT/C1OUT RC2/AN6/C12IN2-/P1D
4 7
RA3/MCLR/VPP RC3/AN7/C12IN3-/P1C
3 6
RA4/AN3/T1G/OSC2/CLKOUT RC4/C2OUT/P1B
2 5
RA5/T1CKI/OSC1/CLKIN RC5/CCP1/P1A
8
RC6/AN8/SS
13 9
RB4/AN10/SDI/SDA RC7/AN9/SDO
12
RB5/AN11/RX/DT
11
RB6/SCK/SCL
10
RB7/TX/CK
PIC16F690

Figure 3.6 Interfacing of the Buzzer to the MCU

3.1.4 LEDs
Gives a visual alarm when there is gas leakage. Blinking of a red LED show there is a gas
leakage and the absence of the gas leakage is shown by lighting of a green LED. The blue LED
shown Power ON status of the device.
The LED are interfaced to the microcontroller through a current limiting resistor.

3.1.5 Button
It used to acknowledge the alarm and it is connected to the microcontroller through pin RA4.

3.1.6 Power Supply.


The power supply used for powering the detector is shown in Figure 2.6

19
3.1.7 Schematic Diagram for the Detector
Figure 3.8 shows the simulation of the gas leakage detector using Proteus Simulation Software.

LCD1
BUZ1 LM016L

RV2
BUZZER
RV1
59%

VDD
VSS

VEE

RW
RS
21%

D0
D1
D2
D3
D4
D5
D6
D7
1k

6E

11
1
2
3

4
5

7
8
9
10

12
13
14
1k

D2
LED-RED
U1
19 RA0/AN0/C1IN+/ICSPDAT/ULPWU RC0/AN4/C2IN+ 16
R4 18 15
RA1/AN1/C12IN0-/VREF/ICSPCLK RC1/AN5/C12IN1-
17 14
RA2/AN2/T0CKI/INT/C1OUT RC2/AN6/C12IN2-/P1D
330 4 7
RA3/MCLR/VPP RC3/AN7/C12IN3-/P1C
3 6
RA4/AN3/T1G/OSC2/CLKOUT RC4/C2OUT/P1B
2 5
RA5/T1CKI/OSC1/CLKIN RC5/CCP1/P1A
8
RC6/AN8/SS
13 9
RB4/AN10/SDI/SDA RC7/AN9/SDO
R1 R5 12
RB5/AN11/RX/DT
330 330 11
RB6/SCK/SCL
10
RB7/TX/CK

R2 PIC16F690
330

D3 D1
LED-GREEN LED-BLUE

Figure 3.8Schematic Diagram for Gas leakage Detector

20
3.2 Software Design

3.2.1 Flowchart Diagram

Figure 3.9 Flowchart for the program

21
3.2.2 Programming the PIC16F690
The PIC16F690 is programmed in Assembly language. The PIC16F690 is RISC chip with 35
instruction only. The instructions are 14 bits and consist of the opcode (operation code part) and
operand (the data being operated on which can be a literal or a register with address of the data).
The assembly code consists of Labels, mnemonic (instruction symbol) and the data being
operated on. The instruction set is divided into 3 categories
 Byte-oriented operation
 Bit-oriented operation
 Literal and control operation
The complete set of instruction set for PIC is shown in Table D.1 in Appendix D.
The program code is written using an MPLAB IDE which include a Code Editor, Simulator,
Debugger and an Assembler.

Steps in Programming the PIC.


Including header files
A header file is included that defines the PIC16F690 processor specific variables
#include<p16f690.inc>
Set the configuration word bits which enable or disable certain special features of the processor.
The configuration Word was configured such that, the internal oscillator – Enabled, Watch Dog
Timer - Disabled, Brown Out Reset – ON and Power Up Timer
config 0x32C4
CONFIG _FOSC_INTRCIO & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF &
_CPD_OFF & _BOREN_NSLEEP & _IESO_OFF & _FCMEN_OFF

Define the variables


Define the variable that will be used in the program.

The org directive


The ORG directive is used to tell the processor to start from a certain memory location
ORG 0x00
GOTO starts

22
ORG 0x04
;interrupt service routine
RETFIE
The processor start the program execution from the 1st memory location. When an interrupt
occurs the processor start the program execution from 0x04. PIC16F690 has a number of
interrupt such a Timers Overflows, IOC, ADC Conversion Completion. When an interrupt
occurs in PIC16F690, the processor start the program from 0x04 so if you are using interrupt in
the program, the ISR should be written from this location.RETFIE directive is used to return
from a interrupt

Initializing the oscillator


The main code start from starts Label. The main code starts by configuring the oscillator and the
internal oscillator is selected as the clock source at a frequency of 4MHz.

Initializing the ports


The ports pins are initialized as input or output pins and as analog or digital pinsusing the TRISx
and ANSEL and ANSELH registers
To configure a pin as an input pin the corresponding bit of TRISx is set and to configure the pin
as an output pin the corresponding bit of TRISx is cleared. i.e. RA0 is the sensor input and RA1
is the output to the alarm. These are configured as follows
BANKSEL TRISA
BSF TRISA,RA0
BCF TRISA, RA1
The BANKSEL directive is used to select a bank in the RAM memory. PIC16F690 RAM
memory is divided into 4 banks each with 128 8-bits registers. The SFR registers are in the first
32 locations of each bank. When using a certain register in PIC16F690, you must be certain you
are in the bank where that register is contained in the RAM memory. The banks are selected
using the RP1 and RP0 bits of the STATUS register or using the BANKSEL directive.
TheANSEL and ANSELH registers are used to configure analog capable pins as analog or digital
pins. When the corresponding bit of ANSEL or ANSELH is SET, the pin is an analog pin and
when cleared the pin is a digital pin. PIC16F690 has a 12 ADC channels. AN0 channel is used
for the sensor input which is on pin RA0 and this is the only pin configured as an analog pin.This
is configured as follows

23
BANKSEL ANSEL
MOVLW b'00000001' ;RA0 is an analog pin for the sensor
MOVWF ANSEL
BANKSEL ANSELH
CLRF ANSELH ; all other pins are digital I/O pins

Initializing the LCD display.


For the HD44780 LCD to display character, it is required to be initialized in a specific way by
sending a series of commands. The LCD can be initializedfor 8-bits interface or 4-bits interface
mode.The LCD has been initialized for 8 bits interface. This has been done by sending a
‘function set’ command for 8-bits interface mode for a number of times and a ‘real function set’
command for 8-bits interface, 2 display lines and 5*7 dots. Send the ‘display off and cursor off’
command, ‘clear the display’ command and the ‘entry mode’ command. The ‘entry mode’
command set the cursor position to auto increment with display of each character and the
character not to shift with the cursor position. Send the ‘display on’ command such that the
character can be displayed on the LCD.

Initialize the ADC


PIC16F690 has a 10 bits ADC module with 12 channels. The ADC module has two ADC control
registers; ADCON0 and ADCON1 that are used to: select the channel, the voltage reference, the
format of the result and the ADC conversion clock source. The ADCON1 is specifically used to
select the ADC conversion clock source using the ADCS bits. The ADCS bits <2:0> =b’100’
select the ADC conversion clock source to be Fosc/4. Fosc is the internal clock frequency of
4MHz.
The ADCON0 is set such as to select channel AN0, as the ADC reference voltage and left
justification of the result. The 10-bits (ADRES<9:0>) ADC result is stored into two 8 bit
registers, ADRESH and ADRESL. Left justify means, the 8 most significant bits ADRES<9:2>
are stored in the higher byte ADRESH and the 2 least significant bits ADRES<1:0> are stored in
bit7 and bit6 of the lower byte ADRESL.

Sample Acquisition and Conversion


BSF ADCON0,ADON ; put the ADC ON
CALL sample_time ; a delay of 7.67us for sample acquisition
BSF ADCON0,1 ; start the conversion

24
BTFSC ADCON0,1 ; Is conversion done?
GOTO $-1
After initializing the ADC, the ADC is put ON and wait the required sample acquisition period
of 7.67us. The ADC module use successive approximation. The Go/DONE bit is set to start the
conversion. When the conversion is complete, GO/DONE is cleared. The completion of the ADC
conversion is checked by polling the GO/DONE bit. When the conversion is completed,
GO/DONE=1, the ADRESH and ADRESL register are updated with the result of the conversion.

NB:Therefore, whether there is a gas leakage or not and the amount of the gas concentration, is
determined by reading the ADRESH andADRESL registers.

The leakage_status subroutine


This subroutine decides whether there is gas leakage or not and the amount of the gas
concentration. The ADC conversion results are moved to the registers RESULTHI and
RESULTLO; RESULTHI = ADRESH and RESULTLO = ADRESL. A gas leakage is when the
input signal is ≥ 2.0V. If 10-bits ADC result is ≥ 2.0V the alarm systems should be put ON. The
10-bits ADC has2 = 1024representation levels.0V correspond to 0 and 5V correspond to 1023.
The ADC module has a resolution of
5
= 4.89 10
1023
Each voltage level is converted to its digital value in binary form using the following formula
1023
=
5
i.e. 5.0V = b’1111111111’ = d’1023’ and 2.0V= b’0110011001’ = d’409’. Thus, with the format
of the result being left justified ADRESH (=RESULTHI) = b’1111 1111’ =d’255’ and
RESULTLO = B’1100 0000’=d’192’ for 5.0V and RESULTHI = 0110 0110’=d’102’ and
RESULTLO=b’0100 0000’=d’64’ for 2.0V. For analysis purpose the result on RESULTLO has
been ignored, the 2 least significant bit read as Zeros. This introduce a maximum probable error
of (2 = 2 − 1 = 3) 5 = 1.5 10 ≈ 0 in the read voltage level. If RESULTHI value of 102 is
considered to represent 2.0V and ignore RESULTLO result, the error introduced on the read
voltage level is 5 = 4.9 10 ≈ 0.

25
RESULTHI values ranges from 0 at 0V to 255 at 5V. To decide whether to put ON the alarm
systems, the RESULTHI register is checked if it above a value d’101’ using the following code.
BANKSEL RESULTHI
MOVF RESULTHI,W
SUBLW d'101' ;K-W, sensor voltage >=2.0V. At 2.0V RESULTHI=102
BANKSEL STATUS
BTFSC STATUS,C ;check status of C flag, C=0(W>K) and C=1(W<=K)
GOTO ALARMOFF ;if C is set, input voltage<2.0V, goto ALARM OFF
GOTO ALARMON ;if C is clear, voltage>=2.0V goto ALARMON
If RESULTHI value is more than 101, branch to ALARMON and turn on the alarm and the red
LED else branch to ALARMOFF and turn on the green LED as detailed on the following code
GOTO ALARMOFF ; if C is set, input voltage<2.0V, goto ALARM OFF
GOTO ALARMON ; if C is clear, voltage>=2.0V goto ALARMON
ALARMON
BTFSS PORTA, RA5 ; check status of button.
GOTO loop2 ; if button is not pressed (RA5=low), goto loop2
BTFSC PORTA, RA5
BCF ack, 0 ; if the button is pressed (RA5=HIGH), clear ack
loop2
BTFSS ack, 0 ; check status of button press variable (ack)
GOTO alarm_ack ; if ack=clear (ALARM OFF) else ALARM ON
NOT_ACK
BANKSEL PORTA
BCF PORTA, RA1 ; ALARM ON
BSF PORTA, RA2 ; RED LED ON
BCF PORTA, RA4 ; GREEN LED OFF
BSF leakage, 0 ; leakage bit0 is set when there is leakage
RETURN
alarm_ack
BANKSEL PORTA
BSF PORTA, RA1 ; ALARM OFF
BSF PORTA, RA2 ; RED LED ON
BCF PORTA, RA4 ; GREEN LED OFF
BSF leakage, 0 ; leakage bit0 is set when there is leakage
RETURN
ALARMOFF
BANKSEL PORTA
BCF PORTA, RA5 ; RESET the button PIN (RA5) to no press status
BSF ack, 0 ; RESET the alarm ack variable
BANKSEL PORTA
BSF PORTA, RA4 ; GREEN LED ON
BSF PORTA, RA1 ; ALARM OFF
BCF PORTA, RA2 ; RED LED OFF
BCF leakage, 0 ; leakage bit0 is cleared when there is No leakage

26
RETURN

GasConcPPM subroutine
This subroutine determine how much is the gas concentration in PPM. The relation between
voltage and gas concentration is not linear. From figure 2.3 (MQ-6 Sensitivity Curve) there are
intervals of voltages where there is linear relationship between voltage and gas concentration, a
constant gradient.In Figure 2.3, within intervals of 0.5V, there is a constant gradient and given a
voltage, the gas concentration can be interpolated.Table 3.2 shows these intervals,

Table 3.2 voltage gas concentration relationship

Sensor ADC Binary Equi Binary Equi of Decimal Decimal Gas Conc
output RESULT of ADC ADC Equi of Equi of (PPM) from
voltage 10-bits ADRESH ADRESL ADRESH ADRESL Figure A.3
0-5V 0-1023

0 0 0000 0000 0000 0000 0 0 SAFE

0.5 102 0001 1001 1000 0000 25 128 SAFE

1.0 204 0011 0011 0000 0000 51 0 SAFE

1.5 306 0100 1100 1000 0000 76 128 SAFE

2.0 409 0110 0110 0100 0000 102 64 300

2.5 511 0111 1111 1100 0000 127 192 400

3.0 613 1001 1001 0100 0000 153 64 1400

3.5 716 1011 0011 0000 0000 179 0 2500

4.0 818 1100 1100 1000 0000 204 128 4160

4.5 920 1110 0110 0000 0000 230 0 7400

5 1023 1111 1111 1100 0000 255 192 10 000

Table 3.3 shows the multiplying factor for each range

Range 0-2.0V 2.0-2.5V 2.5-3.0V 3.0-3.5V 3.5-4.0V 4.0-4.5V 4.5-5.0V


Multiply Factor 0 4 38 42 66 125 104

27
=
Therefore the gas concentration for any voltage is equal to
+ ∗
The base value is the lower value of the gas concentration in a given range and offset is equal to
the ADRESH value for a given voltage minus the ADRESH value for lower value of voltage in
that range. Table 3.4 show example of voltage conversion into corresponding gas concentration.
Table 3.4 example of conversion of voltage to gas concentration in PPM

Sensor ADC Binary Equi Binary Equi of Decimal Decimal Gas Conc
output RESULT of ADC ADC Equi of Equi of (PPM)
voltage 10-bits ADRESH ADRESL ADRESH ADRESL
0-5V 0-1023
2.65 535 1000 0111 1000 0000 135 128 704

3.35 685 1010 1011 0100 0000 171 64 2170

Gas concentration for 2.65 is equal to


400 + 135 − 127 ∗ 38 = 704
Gas concentration for 3.35 is equal to
1400 + 171 − 153 ∗ 42 = 2156

GasConcPPM implement the above relationship to convert any voltage to it corresponding gas
concentration in PPM.

BINARY_to_BCD subroutine
The concentration value is held in a variable ‘conclevel’ in binary form. To display this on the
LCD it has to be converted to Binary Coded Decimal (BCD). This has been achieved by
subtracting a 1000 from conclevel and counting how many times 1000 is subtracted from
conclevel before the value in conclevel gets below 1000. The same has been done for 100s, 10s
and 1s. The results are stored in variable ‘thousands’, ‘hundreds’, ‘tens’ and ‘ones’.

28
Msgdisplay subroutine
This subroutine display the appropriate message when there is a gas leakage or when there is no
gas leakage. When there is a gas leakage, bit0 of a variable ‘leakage’ is set and in absence of gas
leakage the bit is cleared. In this subroutine, if bit0 is set, then the following message is
displayed on the LCD.
Line 1 of the LCD: Gas Leakage
Line 2 of the LCD: CONC: ‘gas concentration’ PPM
i.e.
VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E

11
10

12
13
14
1
2
3

4
5
6

7
8
9

Figure 3.10 message display on the LCD when there is gas leakage

Else, the following message is displayed


Line 1 of the LCD: No Leakage
Line 2 of the LCD: CONC: SAFE PPM
i.e.

29
VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E

11
10

12
13
14
1
2
3

4
5
6

7
8
9
Figure 3.11 message display on the LCD when there is no gas leakage

Acknowledging the alarm


When the alarm goes off, it can be acknowledged using a button. When a button is pressed bit0
of a variable ‘ack’ is CLEARED and the bit is SET in the ALARMOFFsubroutine. When the
code branch to ALARMON subroutine when there is a gas leakage, the button is checked if it is
pressed or not by polling bit0 of ‘ack’. If the bit is set the NOT_ACK subroutine is executed
which put alarm and the red LED ON else the alarm_ack subroutine is executed which put the
alarm OFF but keeps the red LED ON. Bit0 of ‘ack’ variable is RESET to high when there is no
leakage in the ALARMOFF subroutine.

Blinking the LED.


After displaying the message on the LCD, a delay of 0.5s is called and then the LED is put OFF
for 0.5s. When the code loops back, the LED is put back ON and the LED will be blink.

The full program code is listed on the Appendix.

30
Chapter 4: RESULTS

4.1 Proteus Simulated Results and Practical Results

4.1.1 Procedures: Simulated results


Simulation results were obtained using the simulated circuit using proteus by running the
simulation. The potentiometer simulating the sensor was adjusted from 0% to 100%. At 100%
the potentiometer output voltage is 5V. The potentiometer is connected to 5V. The response of
the alarm and the LEDs was noted. The value of the gas concentration was read on the LCD and
recorded in Table 4.1.
Observation
When the program is ran, the Power ON LED and the Green LED lit. The red LED and the alarm
are off.As the potentiometer is increased, at 40% (0.4*5V=2V) the green LED turns off and the
red LED starts to blink and the alarm goes off. When the button was pressed, the alarm turns off
but the red LED keeps blinking.
Table 4.1 shows the concentration level of the gas in PPM at various voltages level.

4.1.2 Procedures: Practical result


Practical results were obtained using the breadboard implemented gas leakage detector. The
circuit was powered on and LPG gas from a lighter released. The response of the alarm and the
LEDs was observed. The value of the gas concentration was read on the LCD and recorded in
Table 4.1

Observation
When the detector was powered on the Power ON LED is lit and the green LED is also lit. When
the gas is released, the green LED goes off and the red LED blinks and the alarm goes off when

31
the sensor output voltage is more than or equal to 2.0V. Below 2.0V, the alarm is off and the
green LED is lit while the red LED is off. The sensor output voltage is monitored using a DDM
or by displaying the value of ADRESH on the LCD. When the button is pressed while the alarm
is ON the alarm turns OFF.
The value of the gas concentration was read and recorded in Table 4.
Table 4.1 the simulated results using Proteus simulation software and the Practical results using MQ-6 sensor

Sensor ADC Binary Binary Decimal Decimal Expecte


output RESUL Equi of Equi of Equi of Equi of d Gas Simulate
voltage T ADC ADC ADRES Conc d Practical
0-5V 10-bits ADRES L (PPM) Results Results
0-1023 ADRESH ADRESL H PPM
0 0 0000 0000 0000 0000 0 0 SAFE SAFE

0.5 102 0001 1001 1000 0000 25 128 SAFE SAFE

1.0 204 0011 0011 0000 0000 51 0 SAFE SAFE FE

1.5 306 0100 1100 1000 0000 76 128 SAFE SAFE SAFE

2.0 409 0110 0110 0100 0000 102 64 300 300 300
2.3 470 0111 0101 1000 0000 117 128 360 360 360
2.4 491 0111 1010 1100 0000 122 192 380 380 380
2.5 511 0111 1111 1100 0000 127 192 400 400 400
2.65 535 1000 0111 1000 0000 135 128 700 704 704
2.9 593 1001 0100 0100 0000 148 64 1200 1198 1198
3.0 613 1001 1001 0100 0000 153 64 1400 1400 1400
3.05 624 1001 1100 0000 0000 156 0 1510 1526 1526
3.35 685 1010 1011 0100 0000 171 64 2170 2156 2156
3.5 716 1011 0011 0000 0000 179 0 2500 2500 2500
3.75 767 1011 1111 1100 0000 191 192 3330 3292 3292
3.9 797 1100 0011 0100 0000 199 64 3828 3820 3820
4.0 818 1100 1100 1000 0000 204 128 4160 4160 4160
4.15 849 1101 0100 0100 0000 212 64 5132 5160
4.45 910 1110 0011 1000 0000 227 128 7076 7035

32
4.5 920 1110 0110 0000 0000 230 0 7400 7400
4.55 930 1110 1000 1000 0000 232 128 7660 7608
4.65 951 1110 1101 1100 0000 237 192 8180 8128
5 1023 1111 1111 1100 0000 255 192 10 000 10 000

NB: The expected results have been arrived at by extrapolating using the voltages level and the
knowngas concentration as read from Figure 2.3

4.2 Analysis of the Results

The analysis is done by comparing the expected results with the simulated and practical results
as shown in Table 4.2

Table 4.2 Analysis of Results

Sensor Expecte Error in Error in


output d Gas Simulate Practical Simulated Error Practical Error

voltage Conc d Results results % Results %

0-5V (PPM) Results


PPM
0 SAFE SAFE SAFE

0.5 SAFE SAFE SAFE

1.0 SAFE SAFE SAFE

1.5 SAFE SAFE SAFE

2.0 300 300 300 0 0% 0 0%


2.3 360 360 360 0 0% 0 0%
2.4 380 380 380 0 0% 0 0%
2.5 400 400 400 0 0% 0 0%
2.65 700 704 704 4 0.6 % 4 0.6 %
2.9 1200 1198 1198 2 0.2 % 2 0.2 %
3.0 1400 1400 1400 0 0% 0 0%
3.05 1510 1526 1526 16 1.1 % 16 1.2 %
3.35 2170 2156 2156 14 0.6 % 14 0.6 %

33
3.5 2500 2500 2500 0 0% 0 0%
3.75 3330 3292 3292 8 0.2 % 8 0.2 %
3.9 3828 3820 3820 8 0.2 % 8 0.2 %
4.0 4160 4160 4160 0 0% 0 0%
4.15 5132 5160 28 0.5 %
4.45 7076 7035 41 0.6 %
4.5 7400 7400 0 0%
4.55 7660 7608 52 0.7 %
4.65 8180 8128 52 0.6 %
5 10 000 10 000 0 0%

4.3 Discussion

The objective of the project was to design and implement a microcontroller based cooking gas
leakage detector capable of detecting gas leakage and giving an audiovisual warning. The
detector has been designed around PIC16F690 microcontroller and MQ-6 sensor and the
implemented design is shown in Figure 3.3 and 3.4. According to the design, the detector is
supposed to show there is gas leakage when the sensor voltage is >=2.0V. This is with respect to
Figure 2.3 which shows the relationship between the sensor voltage and the gas concentration.
When the simulated circuit for the detector is ran, with the potentiometer being 0% (0V) the
green LED lit showing there is no gas leakage. This is the same case when the practical circuit is
powered ON and no gas is released. The LCD display the message ‘SAFE’ as shown in figure
3.11. In absence of the gas, the sensor resistance is high such that the output voltage is almost
zero. The microcontroller is programmed such that as long as the voltage from the sensor is
below 2.0V it show display the ‘SAFE’ message and lit the green LED to show that there is no
gas leakage.

34
When there is a gas leakage, the sensor voltage should increase due to the increase in the sensor
conductivity. When the potentiometer is at 40% the green led immediately goes off and the red
LED starts blinking and the alarm goes off. The LCD starts showing gas concentration starting
from 300ppm. The same thing happens when the gas is released constantly. The sensor output
voltage increase gradually and when it reaches 2.0V, the alarm systems goes off. The sensor
voltage increases almost to 5V with the LCD showing the gas concentration levels in ppm. This
is because the gas molecules gets adsorbed on the Tin oxide surface layer and its oxidizing
effectleads to increased concentration of electrons which leads to increased conductivity.
Table 4.1 shows the expected gas concentration for various voltagelevels. The expected
concentration is dictated by the relationship shown in Figure 2.3. The gas concentration for any
voltage from the sensor is arrived at through interpolation. Although the relationship is not
linear, there are intervals of, known gas concentration and sensor voltage that can be used to
convert any other value of sensor voltage to a corresponding gas concentration.
This algorithm has been programmed in the microcontroller and this is what makes the detector
to be very accurate and precise in displaying the gas concentration level.From Table 4.1 it is
evident that there is a very slight error between the expected results and the practical results. The
practical results are same as the simulation results. The difference in practical results from
expected results is due to the assuming the two least significant bits of the 10 bits ADC results to
be always zero such that the ADC result can be approximated to be the 8 most significant bits of
the 10 bits ADC results. The use of a microcontroller helps us to write a conversion algorithm
which makes the detector to be very accurate in displaying the gas concentration level.

35
36
Chapter 5: CONCLUSION AND RECOMMENDATIONS

5.1 Conclusion

The objective of the project was to design and implement a cooking gas detector capable of
giving an audiovisual warning when there is a gas leakage.The detector has been designed that
uses a PIC16F690 microcontrollerand a MQ-6 gas sensor. The detector shows a green LED to
show that there is no gas leakage. When there is a gas leakage the detector flashes a red and
sound an alarm. The detector has an alarm acknowledgement button that can be used to put off
the alarm when necessary. The detector used a LCD to show the concentration of the gas
leakage. The sensor is capable of showing a gas concentration from 300ppm to 10,000ppm.
The use of a microcontroller makes the detector to have high accuracy in displaying the gas
concentration according to the relationship that exist between the sensor voltage and gas
concentration.
The detector is low cost. The components that goes into making the detector does not exceed
1600KShs. If this is done with mass production then the detector can go for a price of 1500ksh
which would very easily affordable and competitive in the market. The objective of designing a
highly accurate low cost a microcontroller based cooking gas leakage detector has been well
achieved.

5.2 Recommendations

Despite how this project might look good it is still wanting.The use of an alarm, LED and LCD
as the alarm system is not enough. This require a person presence in the vicinity to be notified
when there is gas leakage. This detection system can be improved by including a GSM module
for sending sms alert to a mobile phone. This way, wherever someone is, he can be notified when
there a gas leakage. To improve the safety of people and property, the sms alert can be sent to the
relevant authority like the fire brigade department to deal with the gas leakage issue if the gas
leakage as reached very high concentration.
The inclusion of fire detector would greatly improve the performance of this gas detector system.
There has been a gas leakage, has there been a fire that has started? The fire detector can help to
inform whether things have gone worse for quick safety actions to be taken. To take this gas

37
detection system to new perfection, the gas can be supplied with smart regulator which can be
turned off remotely or by sending a signal. Thus when detector has detected gas leakage even
before it sound the alarm and send the alerts, the microcontroller should send a signal to switch
off the gas. To effectively implement all this good design features a higher level microcontroller
would be useful. PIC16F690 microcontroller is a midrange microcontroller by Microchip.
Microchip have PIC18 series which are higher level microcontroller with more pins and features
such as priority interrupts which would help to easily incorporate the improvement to this gas
leakage detection system.

38
REFERENCES

[1] E. T. Goerge, Fuels and Lubricants handbook: technology, properties, performance and
testing, 2nd Edition. West Conshohocken, Pa : ASTM International, 2003.
[2] E. Hahn, “LPG Gas Blog,” LPG Gas Blog, 2016. .
[3] “Lower-(LEL)-&-Upper-(UEL)-Explosive-Limits-.pdf.” .
[4] X. Liu, S. Cheng, H. Liu, S. Hu, D. Zhang, and H. Ning, “A Survey on Gas Sensing
Technology,” Sensors, vol. 12, no. 12, pp. 9635–9665, Jul. 2012.
[5] Great Britain and Health and Safety Commission, Dangerous substances and explosive
atmospheres: Dangerous Substances and Explosive Atmospheres Regulations 2002. Sudbury
Suffolk [England]: HSE Books, 2003.
[6] Zhengzhou Winsen Electronics Technology Co., Ltd “Flammable Gas Sensor (MQ-6
MODEL) MANUAL,” no. VERSION 1.3, p. 7, May 2014.
[7] C. Sansone, S. Manfredi, E. Di Tucci, S. De Vito, G. Fattoruso, and F. Tortorella, “A novel
approach for detecting alerts in urban pollution monitoring with low cost sensors,” in
Measurements and Networking Proceedings (M&N), 2013 IEEE International Workshop on,
2013, pp. 89–93.
[8] U. B. REDDY, “DETECTION OF LPG GAS LEAKAGE SENSOR AND ALERT
SYSTEM.”
[9] “Zenith systems limited.” .
[10] “Droplex Industrial System Ltd.” .
[11] Microchip Inc., “PIC16F631/677/685/687/689/690 Data Sheet,” no. DS41262C, p. 294,
2006.
[12] M. Bates and M. Bates, PIC microcontrollers: an introduction to microelectronics, 3rd
ed. Amsterdam ; Boston: Elsevier, Newnes, 2011.
[13] Michael A. Covington, PIC Assembly Language for the Complete Beginner. The
University of Georgia Athens, Georgia 30602-7415: Gernsback Publications, Inc., 1999.
[14] “How to Use Character LCD Module,” 01-Dec-2010. .
[15] Risckey’s World, “LCD Interfacing Tutorial:,” 2014. .
[16] Electronics Engineering Herald, “LCD interface to a microcontroller covering basics,”
LCD interface to a microcontroller covering basics, 2006. .
[17] Arpit Jain, “Insight - How Piezo Buzzer works,” 2012. .
[18] Learning about Electronics, “How to Connect a Voltage Regulator in the circuit,” How to
Connect a Voltage Regulator in the circuit. .

39
APPENDIX

Appendix A: MQ-6 Details

Table A.1 Technical Parameters for MQ-6 Sensor

40
Appendix B: PIC16F690 Details

Figure B.1 PIC16F690 BLOCK DIAGRAM

41
Section B.1 PIC16F690 features

42
Table B.1 A Summary of PIC16F690 Pins

Appendix C: LCD Details

Table C.1 LCD Commands

No Instruction Hex value Decimal

1 Function Set: 8-bit, 1 Line, 5x7 Dots 0x30 48

2 Function Set: 8-bit, 2 Line, 5x7 Dots 0x38 56

3 Function Set: 4-bit, 1 Line, 5x7 Dots 0x20 32

43
4 Function Set: 4-bit, 2 Line, 5x7 Dots 0x28 40

5 Entry Mode 0x06 6

6 Display off Cursor off


(clearing display without clearing DDRAM content)
0x08 8

7 Display on Cursor on 0x0E 14

8 Display on Cursor off 0x0C 12

9 Display on Cursor blinking 0x0F 15

10 Shift entire display left 0x18 24

11 Shift entire display right 0x1C 30

12 Move cursor left by one character 0x10 16

13 Move cursor right by one character 0x14 20

14 Clear Display (also clear DDRAM content) 0x01 1

15 Set DDRAM address or coursor position on display 0x80 + address* 128 +


address*

16 Set CGRAM address or set pointer to CGRAM 0x40 + address** 64 + address**


Location

44
Appendix D

Table D.1 PIC16F690 Instruction Set

45
BILL OF QUANTITIES

Component Quantity @ (KShs) Cost


MQ-6 Sensor 1 400 400
PIC16F690 MCU 1 400 400
LCD 16*2 1 400 400
Buzzer 1 30 30
Button 1 5 5
LED 3 3 9
9V Battery 1 100 100
Voltage Regulator-7805 1 50 20
Capacitor 2 5 10
Resistor-330 ohms 4 5 20
PCB Board 1 200 200
Total Cost 1594

46
THE PROGRAM CODE

;**********************************************************************
; This file contains a code designed to detect LPG Gas using *
; PIC16F690.ile contains the basic code *
;************************************************************************
; Filename: LPG gas Detector.asm
; Date: April 1st 2016
; File Version: VERSION 1.0
;
; Author: NJUE GEORGE FUNDI F17/1419/2011
; Company: UNIVERSITY OF NAIROBI

list p=16f690 ;defines the processor


#include<p16f690.inc> ;defines processor specific variables

; config 0x32C4
CONFIG _FOSC_INTRCIO & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF &
_CP_OFF & _CPD_OFF & _BOREN_NSLEEP & _IESO_OFF & _FCMEN_OFF

errorlevel -302

#define RS RB4
#define EN RB6
CBLOCK 20h
RESULTHI
RESULTLO
thousands
hundreds
tens
ones
value0
value1
value2
value3
counts
leakage
ack
counts100us
counts1ms
counts10ms
counts100ms
counts500ms
counts1s
ENDC

udata_shr
conclevel res 2
dummy1 res 1
OFFSET res 1
multiplicand res 1
basevalue res 2

onethousand res 2
onehundred res 2

47
org 0x00
goto start
org 0x04
goto start

start
;select internal oscillator as the clock source at 4MHz
BANKSEL OSCCON
MOVLW b'01100101'
MOVWF OSCCON
;initializing the ports
BANKSEL ANSEL
MOVLW b'00000001' ;RA0 is an analog pin for the sensor
MOVWF ANSEL
BANKSEL ANSELH
CLRF ANSELH ;all other pins are digital I/O pins
BANKSEL PORTA
CLRF PORTA
CLRF PORTB
CLRF PORTC
BANKSEL TRISA
MOVLW b'001000001' ;RA0=sensorInput,
;RA5=buttonInput,RA1=alarmOutput
MOVWF TRISA ;RA2=REDLED OUTPUT, RA4=GREENLED OUTPUT
BANKSEL TRISB
MOVLW 0x00
MOVWF TRISB ;RB7=PowerLED, (RB4=RS RB6=EN-LCD control
pins)
MOVWF TRISC ;PORTC for the LCD data lines, D0-D7

;light the Power LED


BANKSEL PORTB
BSF PORTB, RB7

call init_LCD ;call the subroutine to initialize the LCD.


call init_ADC ;call a subroutine to initialize the ADC
main
call sampleAcq ;ADC ON, wait conversion, update registers
call leakage_status ;Determine there is gas leakage or not
call voltagelevel ;
call GasConcPPM ;Convert sensvoltage level to gas conc in PPM
call msgdisplay ;display gas leakage status and conc level

GOTO main ;check again for gas leakage status

msgdisplay
MOVLW 0x80 ;place the cursor to the head of row one
CALL LCD_COMMAND
BTFSS leakage,0 ;if no leakage goto NOLEAKAGE message else
GOTO NOLEAKAGE ;display gas leakage message
GOTO gasleakage
gasleakage
MOVLW "G"
CALL LCD_SENDATA
MOVLW "a"
CALL LCD_SENDATA

48
MOVLW "s"
CALL LCD_SENDATA
GOTO LEAKAGEE
NOLEAKAGE
MOVLW " "
CALL LCD_SENDATA
MOVLW "N"
CALL LCD_SENDATA
MOVLW "O"
CALL LCD_SENDATA
LEAKAGEE
MOVLW " "
CALL LCD_SENDATA
MOVLW "L"
CALL LCD_SENDATA
MOVLW "e"
CALL LCD_SENDATA
MOVLW "a"
CALL LCD_SENDATA
MOVLW "k"
CALL LCD_SENDATA
MOVLW "a"
CALL LCD_SENDATA
MOVLW "g"
CALL LCD_SENDATA
MOVLW "e"
CALL LCD_SENDATA

movlw " "


CALL LCD_SENDATA
MOVF value1,w
CALL LCD_SENDATA
MOVF value2,w
CALL LCD_SENDATA
MOVF value3,w
CALL LCD_SENDATA

MOVLW 0xC0 ;place the cursor to the head of row two


CALL LCD_COMMAND
;Display gas concentration level
MOVLW "C"
CALL LCD_SENDATA
MOVLW "0"
CALL LCD_SENDATA
MOVLW "N"
CALL LCD_SENDATA
MOVLW "C"
CALL LCD_SENDATA
MOVLW ":"
CALL LCD_SENDATA
MOVLW " "
CALL LCD_SENDATA
;gas concentration value
MOVF thousands,w
CALL LCD_SENDATA
MOVF hundreds,w
CALL LCD_SENDATA

49
movf tens,w
CALL LCD_SENDATA
MOVF ones,w
CALL LCD_SENDATA
MOVLW " "
CALL LCD_SENDATA
MOVLW "P"
CALL LCD_SENDATA
MOVLW "P"
CALL LCD_SENDATA
MOVLW "M"
CALL LCD_SENDATA

;a delay to be able to see the results for each conc value


call Delay500ms
banksel PORTA
BCF PORTA,RA2 ;RED LED OFF
BSF PORTA,RA1 ;ALARM OFF
call Delay500ms
RETURN

init_LCD
;LCD initialization subroutine
call Delay100ms
MOVLW 0x30 ;function set, 8 interface
CALL LCD_COMMAND
MOVLW 0x30 ;function set 8 interface
CALL LCD_COMMAND
MOVLW 0x30 ;function set 8 interface
CALL LCD_COMMAND
MOVLW 0x38 ;real function set(8bit, 2lines, 5*7 dots)
CALL LCD_COMMAND
MOVLW 0x08 ;display off cursor off command
CALL LCD_COMMAND
MOVLW 0x01 ;clear the display command
CALL LCD_COMMAND
MOVLW 0x06 ;entry mode command,
CALL LCD_COMMAND
MOVLW 0x0C ;display on and cursor off
CALL LCD_COMMAND
return

LCD_COMMAND
;subroutine for sending command to the LCD
BANKSEL PORTC
MOVWF PORTC ;W reg has the command or data value
BCF PORTB,RS ;RS=0, writing command
BSF PORTB,EN;Enable&disableLCD to send an enable pulse
BCF PORTB,EN
call Delay1ms ;Delay for LCD to finish instruction excution
RETURN

LCD_SENDATA
;subroutine for sending data to the LCD
BANKSEL PORTC
MOVWF PORTC ;Data is passed to the Data lines

50
BSF PORTB,RS ;RS=1, sending data to the LCD
BSF PORTB,EN ;Send LCD enable signal
BCF PORTB,EN
call Delay1ms
RETURN
init_ADC
BANKSEL ADCON1
MOVLW b'00010000' ;clock source for ADC is derived from
MOVWF ADCON1 ;dedicated internal oscillator=500kHz
MOVLW 0x00
MOVWF ADCON0 ;LEFT justify, Ref Volt=VDD, ADC channel=AN0
RETURN
sampleAcq
BSF ADCON0,ADON ;put the ADC ON
CALL sample_time ;A delay of 7.67us for sample acquisation
BSF ADCON0,1 ;start the conversion
BTFSC ADCON0,1 ;Is conversion done?
GOTO $-1 ;No, test again
RETURN
leakage_status
BANKSEL ADRESH
MOVF ADRESH,W ;Read upper 2 bits
BANKSEL RESULTHI
MOVWF RESULTHI ;store RESULTHI register
BANKSEL ADRESL
MOVF ADRESL,W ;Read lower 8 bits
MOVWF RESULTLO ;Store in RESULTLO

BANKSEL RESULTHI
MOVF RESULTHI,W
SUBLW d'101' ;K-W, senso voltage >=2.0V. At 2.0V
BANKSEL STATUS ;RESULTHI=102
BTFSC STATUS,C ;check status of C flag, C=0(W>K),C=1(W<=K)
GOTO ALARMOFF ;if C is set, voltage<2.0V, goto ALARM OFF
GOTO ALARMON ;if C is clear, voltage>=2.0V goto ALARMON
ALARMON
BTFSS PORTA,RA5 ;check status of button.
GOTO loop2 ;if button is not pressed(RA5=low), goto
loop2
BTFSC PORTA,RA5
BCF ack,0 ;if the button is pressed(RA5=HIGH), clear
ack
loop2
BTFSS ack,0 ;check status of button press variable (ack)
GOTO alarm_ack ;if ack=clear (ALARM OFF) else ALARM ON
ALARMM
banksel PORTA
BCF PORTA,RA1 ;ALARM ON
BSF PORTA,RA2 ;RED LED ON
BCF PORTA,RA4 ;GREEN LED OFF
BSF leakage,0 ;leakage bit0 is set when there is leakage
return
alarm_ack
BANKSEL PORTA
BSF PORTA,RA1 ;ALARM OFF
BSF PORTA,RA2 ;RED LED ON
BCF PORTA,RA4 ;GREEN LED OFF

51
BSF leakage,0 ;leakage bit0 is set when there is leakage
RETURN
ALARMOFF
BANKSEL PORTA
BCF PORTA,RA5 ;RESET the button PIN(RA5) to no press status
BSF ack,0 ;RESET the alarm ack variable
BANKSEL PORTA
BSF PORTA,RA4 ;GREEN LED ON
BSF PORTA,RA1 ;ALARM OFF
BCF PORTA,RA2 ;RED LED OFF
BCF leakage,0 ;leakage bit0 is cleared when there is
Noleakage
RETURN
voltagelevel
clrf value1
clrf value2
clrf value3
clrf conclevel
clrf conclevel+1
movf RESULTHI,W
MOVWF conclevel
call BINARY_to_BCD
movf thousands,w
movwf value0
movf hundreds,w
movwf value1
movf tens,w
movwf value2
movf ones,w
movwf value3
return
;GasConcPPM determine the gas concentration level
GasConcPPM
clrf basevalue ;RESET this variable
clrf basevalue+1
clrf OFFSET

BANKSEL RESULTHI
MOVF RESULTHI,W
SUBLW d'101' ;sensor output voltage-1.9V
BTFSC STATUS,C ;check status of C flag; C=0(W>K), C=1(W<=K)
GOTO ConcSafe ;if voltage <2.0
GOTO voltageRange1 ;if voltage >=2.0V
ConcSafe
MOVLW b'01010011' ;display a "SAFE" message
MOVWF thousands ;ASCII VALUE OF "S"=01010011 A=01000001
MOVLW b'01000001' ;F=01000110 E=01000101
MOVWF hundreds
MOVLW b'01000110'
MOVWF tens
MOVLW b'01000101'
MOVWF ones
return
voltageRange1 ;if 2.0<= V <2.5
BANKSEL RESULTHI
MOVF RESULTHI,W
SUBLW d'126'

52
BTFSS STATUS,C
goto voltageRange2 ;if voltage >2.5, goto voltageRange2
;if 2.0<= V <2.5 calc conclevel
MOVLW 0x2C ;conclevel=basevalue(300)+
;offset(ADCvalue-102)*4
MOVWF conclevel ;load basevalue to 16 bit conclevel register
MOVLW 0x01
MOVWF conclevel+1 ;basevalue=300 stored in 16bit conclevel reg
MOVLW d'102'
SUBWF RESULTHI,W
MOVWF OFFSET ;OFFSET=RESULTHI-102, OFFSET*4 is by RIGHT
BCF STATUS,C ;shifting through C in status reg. clear C
RLF OFFSET,f ;multiply offset by 2 offset=offset*2
RLF OFFSET,w ;offset=offset*4
addwf conclevel,f ;add OFFSET to the basevalue=300
BTFSC STATUS,C
incf conclevel+1

call BINARY_to_BCD ;convert the conc level to BCD for display


return
voltageRange2 ;if 2.5<=V<3.0
BANKSEL RESULTHI
MOVF RESULTHI,W
SUBLW d'152'
BANKSEL STATUS
BTFSS STATUS,C
goto voltageRange3 ;if voltage >3.0, goto voltageRange3
;if 2.5<=V<3.0
movlw 0x90 ;CONCLEVEL = basevalue(400) + OFFSET*38
movwf basevalue ;and load 400 to basevalue
movlw 0x01
movwf basevalue+1 ;basevalue=400
MOVLW d'127'
SUBWF RESULTHI,W ;offset = ADCVALUE-minimum value IN that range
MOVWF OFFSET
MOVlw d'38'
MOVWF multiplicand ;multiplicand = 38

call CalcConcvalue ;calc conclevel and convert it into BCD.


return
voltageRange3 ;if 3.0<=V<3.5
BANKSEL RESULTHI
MOVF RESULTHI,W
SUBLW d'178'
BANKSEL STATUS
BTFSS STATUS,C
goto voltageRange4 ;if voltage >3.5
;if 3.0<= V <3.5
movlw 0x78 ;CONCLEVEL = basevalue(1400) + OFFSET*42
movwf basevalue
movlw 0x05
movwf basevalue+1 ;basevalue = 1400
MOVLW d'153'
SUBWF RESULTHI,W ;offset = ADCVALUE-MINIMUM VALUE IN that range
MOVWF OFFSET
MOVlw d'42'

53
MOVWF multiplicand ;multiplicand = 42

call CalcConcvalue ;calc conclevel and convert it into BCD


return
voltageRange4 ;if 3.5<=V<4.0
BANKSEL RESULTHI
MOVF RESULTHI,W
SUBLW d'203'
BANKSEL STATUS
BTFSS STATUS,C
goto voltageRange5 ;if voltage >4.0
;if voltage 3.5<=V<4.0
movlw 0xC4 ;CONCLEVEL = BASEVALUE +OFFSET*66
movwf basevalue
movlw 0x09
movwf basevalue+1 ;basevalue = 2500
MOVLW d'179'
SUBWF RESULTHI,W ;offset = ADCVALUE-MINIMUM VALUE IN that range
MOVWF OFFSET
MOVlw d'66'
MOVWF multiplicand ;multiplicand = 66

call CalcConcvalue ;calc conclevel and convert it into BCD


return
voltageRange5 ;if 4.0<=V<4.5
BANKSEL RESULTHI
MOVF RESULTHI,W
SUBLW d'229'
BANKSEL STATUS
BTFSS STATUS,C
goto voltageRange6 ;if voltage >4.5
;if voltage 4.0<=V<4.5
movlw 0x40 ;CONCLEVEL = basevalue + OFFSET*125
movwf basevalue
movlw 0x10
movwf basevalue+1 ;basevalue = 4160
MOVLW d'204'
SUBWF RESULTHI,W ;offset = ADCVALUE-MINIMUM VALUE IN that range
MOVWF OFFSET
MOVlw d'125'
MOVWF multiplicand ;multiplicand = 125

call CalcConcvalue
return
;if V>4.5V CONCLEVEL = basevalue + OFFSET*104
voltageRange6
movlw 0xE8
movwf basevalue
movlw 0x1C
movwf basevalue+1 ;baselevel = 7400
MOVLW d'230'
SUBWF RESULTHI,W ;offset = ADCVALUE-MINIMUM VALUE IN that range
MOVWF OFFSET
MOVlw d'104'
MOVWF multiplicand ;multiplicand = 104

call CalcConcvalue ;calc conclevel and convert it into BCD

54
return

;calc conclevel = basevalue + offset*multiply factor in that range.


CalcConcvalue
clrf conclevel
clrf conclevel+1
movlw d'8'
movwf counts ;store 8bit of the register tobe shifted through
movf multiplicand,W ; W reg = d'38'
BCF STATUS,C ;clear the carry bit
loop1
RRF OFFSET,f ;right shift the multiplier OFFSET register
BTFSC STATUS,C ;if the lower bit of OFFSET is set add
;multiplicand to conclevel+1
addwf conclevel+1,f ;conlevel+1=conclevel+1 + 38
RRF conclevel+1,f
RRF conclevel,f
DECFSZ counts
goto loop1
;add the basevalue to the conclevel
movf basevalue,w ;W=lower byte of 16 bit reg basevalue
addwf conclevel,f ;conclevel=conclevel+basevalue
movf basevalue+1,w ;W reg = MSB of basevalue(basevalue+1)
BTFSC STATUS,C ;check if conclevel+basevalue resulted into a C
incfsz basevalue+1,W;if yes,increment higher byte and store in W
addwf conclevel+1,f ;conclevel+1=conclevel+1 + baselevel+1

call BINARY_to_BCD ;convert the binary value of conclevel to BCD.


return
;BINARY_to_BCD convert binary value of conclevel into BCD
BINARY_to_BCD
clrf thousands
clrf hundreds
clrf tens
clrf ones
movlw 0xE8
movwf onethousand
movlw 0x03
movwf onethousand+1 ;onethousand = 1000
movlw 0x64
movwf onehundred
movlw 0x00
movwf onehundred+1 ;onehundred = 100
get1000s
movf onethousand,w ;subtract 1000 from conclevel and counts the
subwf conclevel,w ;number of times you substract before
movwf dummy1 ;conclevel gets less than 1000
movf onethousand+1,w
btfss STATUS,C
incfsz onethousand+1,w
subwf conclevel+1,w
btfss STATUS,C ;if conclevel <1000 goto get100s
goto get100s
;update the result of previous sub to conclevel&conclevel+1
movwf conclevel+1
movf dummy1,w
movwf conclevel

55
;increment the counter for thousands
incf thousands,f
goto get1000s
get100s
movf onehundred,w
subwf conclevel,w
movwf dummy1
movf onehundred+1,w
btfss STATUS,C
incfsz onehundred+1,w
subwf conclevel+1,w
btfss STATUS,C ;if conlevel <100 goto get10sand1s
goto get10sand1s
;update the result of previous sub to conclevel&conclevel+1
movwf conclevel+1
movf dummy1,w
movwf conclevel
;increment the counter for hundred
incf hundreds,f
goto get100s

get10sand1s
movlw d'10'
subwf conclevel,w ;substract 10 from conclevel
btfss STATUS,C ;if conclevel is < 10 the C will be cleared
goto endBCD
movwf conclevel
incf tens,f
goto get10sand1s
endBCD
movf conclevel,w
movwf ones
movlw 0x30 ;converting the thousandth, hundredth, tenth and
addwf ones,f ;ones value to thei ASCII value by adding d'48'
addwf tens,f ;for display on the LCD
addwf hundreds,f
addwf thousands,f
return
sample_time
NOP ;7.67us for sample acquisations.each instruction
NOP ;is excuted in 0.5us-7.67us is approx 16 instruc
NOP
NOP
NOP
NOP
NOP
RETURN
;DELAY SUBROUTINES (delay routines is written based on 4Mhz int oscillator
Delay100us
banksel counts100us
movlw H'19'
movwf counts100us
R100us
decfsz counts100us
goto R100us
return
;call 100us 10 times

56
Delay1ms
banksel counts1ms
movlw 0x0A ;10
movwf counts1ms
R1ms
call Delay100us
decfsz counts1ms
goto R1ms
return
;call 100 times of 100 us Delay
Delay10ms
banksel counts10ms
movlw H'64' ;100
movwf counts10ms
R10ms
call Delay100us
decfsz counts10ms
goto R10ms
return
;call 10 times of 10ms Delay
Delay100ms
banksel counts100ms
movlw H'0A' ;10
movwf counts100ms
R100ms
call Delay10ms
decfsz counts100ms
goto R100ms
return
;call 50 of 10ms
Delay500ms
banksel counts500ms
movlw d'50' ;10
movwf counts500ms
R500ms
call Delay10ms
decfsz counts500ms
goto R500ms
return
END

57
58

You might also like