You are on page 1of 68

ABSTRACT

Air leak detector is a project to intimate the alarm signal when there are any leakages in the

tank. The field of mechanical engineering has a theme word called “CHANGE” as its

backbone. The new technological advancements and the needs of people have made us

think about this project.

In a process industry pressure is an important parameter. Particularly pneumatic

pressure is a very important parameter. By using the pneumatic pressure controller, we can

save the power consumed by the industries too. Normally in the industries, there are high

pressure is somewhat low (say around 30 to 40 mbar) the required pressure is depends upon

the process presently we are working with the machine. If there are any chances of air

leakages from the tank due to the high pressure, at that time in our project is used to

intimate the leakages.

1
CHAPTER 1
INTRODUCTION

1.1 Background

Air 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 air
used are Natural Air and Liquefied Petroleum Air() both of which are hydrocarbons air.
Natural Air is basicallymethaneair ( ) while air is a mixture of butane ( ) and
propane air ( ) or purely butane or propane air. Natural air is extracted from underground
while air is a by-product of Natural Air and crude oil processing. These aires have a
characteristic of being highly flammable and can cause suffocation in high concentration. It
is because of this, the usage of aires should be done with great care and safety standards are put
in place to ensure everyone using the air is safe. Air used for cooking is supplied in air
cylinders which have a regulating valve. After using the air you are supposed to turn off
the air.The supplier of the air should make sure the valve are working well and not leaking the
air. Observation of the safety standards would avoid the dangers posed by the air. 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 air. This poses an immediate danger
to life and property due to the flammable and intoxicating nature of the air. As an
engineer, it is a lifesaving task to design a air detector capable of raising an alarm and
showing the concentration of the air leakage. Thus we are going to design a microcontroller
based cooking air detector.

1.2 Problem Statement

This is an era of automation where it is broadly defined as replacement of manual

effort by mechanical power in all degrees of automation. The operation remains an

essential part of the system although with changing demands on physical input as the

degree of mechanization is increased.


2
Degrees of automation are of two types, viz.

Full automation.

Semi automation.

In semi automation a combination of manual effort and mechanical power is required

whereas in full automation human participation is very negligible.

1.4 Project Justification

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

1.5 Scope of Work

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

3
Chapter 2: LITERATURE REVIEW

2.1 Introduction

air is the main air 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 Air processing[2].
is primarily composed of propane, butane, isobutane or a mixtures of these aires[2]. It is stored
in air 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. air is highly flammable air with Lower Explosive Limits
of about 1.4% that is about 14,000PPM[3]. This is the property of that makes us worried when
using the air[2].
No matter the safety standards that are put in place in usage of the air to ensure there is no air
leakage, there is always the danger of a air leakage. Human being are prone to error. air is
odorless and colorless and it would be impossible to detect the presence of a air leakage. An
odourant is normally added to the air to help detect the air in case of a leakage[2] but this is not
enough as it would require person to be physically present to detect the air leakage and by the
time the air 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 air leakage or not.
This has been a great concern for people over time and people have come up with air leakage
detector to solve this problem.
The air leakage detectors that have been developed have been built around already existing
different air technology[4]. The different type of sensor are listed below[5].
 Electrochemical sensors-mostly used for toxic aires such carbon monoxide
 Metal Oxide Semiconductor sensors- used for both toxic and combustible
 Catalytic sensor-used for combustibleaires such hydrocarbon aires
 Infrared Sensors- used for combustible aires

4
The choice of the sensor depends on the type of air that is to be detected among other factors
such as the stability, sensitivity, selectivity, price and durability of the sensor. For hydrocarbon
aires such as air, 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 air
concentrations (300PPM) and detect a wide range of air concentration (300-10000) PPM and
resistant to poisoning[4]. However, its performance is affected by exposure to high corrosive
aires (such as hydrogen chloride), organic silicon steam, halogen pollution and water[6].
Metal oxide semiconductor detect aires by means of their surface interaction with the target air
which alters the conductivity of the semiconductor. The output voltage signal is converted into
air 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 air. The MQ-6 sensor offers a [6]
 High Sensitivity to air
 A Detection Range: 300 - 10,000 PPM
 Fast Response Time: <10s
 Simple drive circuit
 Heater Voltage: 5.0V
 Long lifespan
 Low cost
A air 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 air detector in the market that detect air leakage and raise a
warning in form of an alarm and LED indicators. However, these commercialized air detector
come at a high price and a not readily available on Kenya market[9], [10].
Therefore, there is a need for designing a air 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

5
an

6
alarm and having LED indicators for status of the air leakage it is capable of accurately
displaying the concentration of the air 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 air concentration according to the
sensor datasheet. The datasheet outline the relationship between the sensor voltage and the air
concentration.Thus, air 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 air.
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 air 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 air such as , the air 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 Air Sensor

6
Ground pin

Digital Output

Analog Output
The Sensor Sensitive Part.

Figure 2.2 MQ-6 sensor Drive Circuit

Therefore, as the air 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 air 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 air 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 air
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 air
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 air
concentration there is an increase of 0.1V. Thus air concentration for any other
voltage is through extrapolation from the known values of sensor voltage and air
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 air concentration.
This LCD displays alphanumeric characters (letters, numbers and symbols) which can be used
to convey adequate information concerning the air 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

12
ROM). It has also CGRAM- Character Generator RAM for user defined characters. The LCD is

13
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]

14
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

15
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].

16
Chapter 3: Design Methodology

3.1 Hardware Design

The specifications of the cooking air leakage detector are being to detect a air 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 air 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 air leakage detector Block Diagram.

DESCRIPTION OF THE BLOCK DIAGRAM


3.1.1 MQ-6 AirSensor
The sensor is used to detect when there is a air leakage and the amount of the air concentration
by giving out a voltage output depending on the air leakage concentration
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 air concentration value
with
reference to the curve in Figure 2.3
RV3
U2
19 16
50% RA0/AN0/C1IN+/ICSPDAT/ULPW RC0/AN4/C2IN+
U
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 air 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
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


Figure 3.4 Air leakage detector

3.1.3LCD Display
The LCD shows the numerical value of the air 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
1
2
3

4
5
6

11
10

12
13
14
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
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.5 LCD Connection to the MCU


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 air leakage. The buzzer is interfaced to the MCU through
pin RA1 as shown in Figure 3.5

BUZ2

BUZZER

U2
19 RA0/AN0/C1IN+/ICSPDAT/ULPWU 16
18 15
RC0/AN4/C2IN+ RA1/AN1/C12IN0-/VREF/ICSPCLK
17 14
RC1/AN5/C12IN1- RA2/AN2/T0CKI/INT/C1OUT
4 7
RC2/AN6/C12IN2-/P1D RA3/MCLR/VPP
3 6
RC3/AN7/C12IN3-/P1C
2 RA4/AN3/T1G/OSC2/CLKOUT RC4/C2OUT/P1B 5
RA5/T1CKI/OSC1/CLKIN RC5/CCP1/P1A 8
13 RC6/AN8/SS 9
12 RB4/AN10/SDI/SDA RC7/AN9/SDO
11
RB5/AN11/RX/DT
10 RB6/SCK/SCL
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 air leakage. Blinking of a red LED show there is a air
leakage and the absence of the air 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
3.1.7 Schematic Diagram for the Detector
Figure 3.8 shows the simulation of the air leakage detector using Proteus Simulation Software.
BUZ1 LCD1
LM016L

RV2
BUZZER
RV1
59%

VDD
VSS

VEE

RW
RS
21%

E
1k

4
5
6
1
2
3
1k

D2
LED-RED
U1
19 16
RA0/AN0/C1IN+/ICSPDAT/ULPW RC0/AN4/C2IN+
U
18 15
R4 17
RA1/AN1/C12IN0-/VREF/ICSPCLK RC1/AN5/C12IN1-
14
RA2/A N2/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

D0
D1
D2
D3
D4
D5
D6
D7
RB4/AN10/SDI/SDA RC7/AN9/SDO

330 330 11
RB6/SCK/SCL

11
10

12
13
14
7
8
9
10
RB7/TX/CK
R2 PIC16F690
330

D3 D1
LED- N LED-BLUE
GREE

Figure 3.8Schematic Diagram for Air leakage Detector


3.2 Software Design

3.2.1 Flowchart Diagram

Figure 3.9 Flowchart for the program


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
ORG 0x04
;interrupt service routine
RETFIE
st
The processor start the program execution from the 1 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
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
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 air leakage or not and the amount of the air concentration, is
determined by reading the ADRESH andADRESL registers.

The leakage_status subroutine


This subroutine decides whether there is air leakage or not and the amount of the air
concentration. The ADC conversion results are moved to the registers RESULTHI and
RESULTLO; RESULTHI = ADRESH and RESULTLO = ADRESL. A air 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.


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
RETURN

AirConcPPM subroutine
This subroutine determine how much is the air concentration in PPM. The relation between
voltage and air 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 air concentration, a
constant gradient.In Figure 2.3, within intervals of 0.5V, there is a constant gradient and given a
voltage, the air concentration can be interpolated.Table 3.2 shows these intervals,

Table 3.2 voltage air concentration relationship

Sensor ADC Binary Equi Binary Equi of Decimal Decimal Air 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
=
Therefore the air concentration for any voltage is equal to
+ ∗
The base value is the lower value of the air 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 air
concentration.
Table 3.4 example of conversion of voltage to air concentration in PPM

Sensor ADC Binary Equi Binary Equi of Decimal Decimal Air 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

Air concentration for 2.65 is equal to


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

AirConcPPM implement the above relationship to convert any voltage to it corresponding air
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’.
Msgdisplay subroutine
This subroutine display the appropriate message when there is a air leakage or when there is no
air leakage. When there is a air leakage, bit0 of a variable ‘leakage’ is set and in absence of air
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: Air Leakage
Line 2 of the LCD: CONC: ‘air concentration’ PPM
i.e.
VSS VDD VEE

12

14
10
11

13
5

9
2
3

8
7
4

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

Else, the following message is


displayed
Line 1 of the LCD: No Leakage
Line 2 of the LCD: CONC: SAFE PPM
i.e.
VSS VDD VEE

8
9
10
11
12
13
14
5
6
3
2

7
Figure 3.11 message display on the LCD when there is no air 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 air 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.


CHAPTER V
BLOCK DIAGRAM

SENSOR RELAY

MICROCONTROLLER UNIT

ALARM

LCD DISPLAY
CHAPTER VI

WORKING PRINCIPLE

The pressure sensor senses the pressure leakage of the air tank. This sensing

signal is given to the microcontroller unit. When there is no leakage in the tank, there is

no signal given to the microcontroller unit.

In our 12 volt power supply is used. The power supply output is given to the

control unit. Control unit having three relays, they are connected to the alarm unit.

Initially the pressure is set with the help of the variable resistance. The pressure leakages

of the tank is sensed by the pressure sensor and this control signal is given to the

microcontroller unit
CHAPTER VII

APPLICATIONS, ADVANTAGES AND DISADVANTAGES

APPLICATIONS

 IN INDUSTRIES

ADVANTAGES

 Simple in construction

 This system is noiseless in operation

 It is portable, so it can be transferred easily from one place to other place

 Maintenance cost is low

 The LCD display monitors the current status.

DISADVANTAGES

 Initial cost is high

APPLICATION

Home

Industries
Meeting hall

Seminar hall
chapter 5: CONCLUSION AND RECOMMENDATIONS

5.1 Conclusion

The objective of the project was to design and implement a cooking air detector capable of
giving an audiovisual warning when there is a air leakage.The detector has been designed that
uses a PIC16F690 microcontrollerand a MQ-6 air sensor. The detector shows a green LED to
show that there is no air leakage. When there is a air leakage the detector flashes a red and
sound an alarm. The detector has an alarm acknowledgement button that can be used to put off
37
the alarm when necessary. The detector used a LCD to show the concentration of the air
leakage. The sensor is capable of showing a air concentration from 300ppm to 10,000ppm.
The use of a microcontroller makes the detector to have high accuracy in displaying the air
concentration according to the relationship that exist between the sensor voltage and air
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 air 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 air 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 air 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 air
leakage issue if the air leakage as reached very high concentration.
The inclusion of fire detector would greatly improve the performance of this air detector system.
There has been a air 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 air

38
detection system to new perfection, the air can be supplied with smart regulator which can be
turned off remotely or by sending a signal. Thus when detector has detected air leakage even
before it sound the alarm and send the alerts, the microcontroller should send a signal to switch
off the air. 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 air leakage detection system.

39
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, “ Air Blog,” Air 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 Air 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 Air 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 AIR 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. .

40
PROGRAM CODE

;**********************************************************************
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

CALL LCD_SENDATA

48
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 air leakage or not
call voltagelevel ;
call AirConcPPM ;Convert sensvoltage level to air conc in PPM
call msgdisplay ;display air leakage status and conc level

GOTO main ;check again for air 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 air leakage message
GOTO airleakage
airleakage
MOVLW "G"
CALL LCD_SENDATA
MOVLW "a"

CALL LCD_SENDATA

49
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 air 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
;air concentration value
MOVF thousands,w
CALL LCD_SENDATA
MOVF hundreds,w
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

50
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

51
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
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
;AirConcPPM determine the air concentration level
AirConcPPM
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'
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'
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


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
;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
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
58

You might also like