LPG is present in almost all households today, whether in cylinders or through pipes, but what fraction of these households have devices to detect LPG leaks ? Given that so many people lose their lives because of this, it is surprising that little has been done.
It's actually very simple to build your own LPG Leak Detector and here you will learn how to do just that. The LPG Leak Detector circuit here detects the LPG concentration nearby and displays the same on an LCD. It will also compare it to a limit and beep if it exceeds 75% of the limit. The user can enter the desired limit using the provided keypad and also calibrate the sensor as needed.
ATmega328p development stick
The project is built around Knewron's Development Stick ATmega328p, which is an excellent and compact unit. Consists of integrated support for a standard 16×2 HD44780 compatible LCD operated in 4-bit mode. It has the 32-pin TQFP version of the ATmega328p with all I/O split via headers; It actually has two sets of each pin, which is very useful during design and testing.
Figure 1: Typical ATMega328P development stick image
For this project we will interface a standard LCD module, 4x4 keyboard, buzzer and a gas sensor that will use 1 ADC pin, and after interfacing everything, we will have used most of the pins available on the tiny MCU. The gas sensor will be interfaced with channel 0 of the ADC, i.e. PC0 pin, while the buzzer will be connected to PC1 pin. The 4×4 keyboard will be interfaced with port D, using all PD0-PD7 pins. Lastly, we just need to mount an LCD to the already populated header pins on the stick (PORT B); It's very easy, as the potentiometer needed to vary the display contrast is already on the board.
Keyboard
The chosen keyboard is practically a standard layout used in small embedded systems. It consists of 4 rows and 4 columns of buttons configured in the form of a matrix and is therefore called a 4×4 matrix. There are several methods for reading these types of keyboards, some involve using interrupts to initiate key sweeps, while others require multiple read sweeps to detect whether multiple keys are pressed; but for this application we don't need to worry about pressing multiple keys; other safeguards to verify the validity of keystrokes have been implemented; an interrupt scheme is also not needed as our controller is not heavily loaded.
Figure 2: 4X4 Keyboard Circuit Diagram
The figure above shows the real matrix arrangement of the keyboard where we have 4 rows and 4 columns connected. The theory is that if we initially set the columns to outputs while making the row inputs with pull up resistors in place, we can individually set each column to logic 0 (GND), while keeping the other three to logic 1 (5V); By reading the bottom nibble i.e. the rows we can determine if a key was pressed, if a specific input is low then the key corresponding to that row was pressed.
It is worth mentioning that the key priorities are fixed in the scanning (reading) order, i.e. if both keys 1 and 6 are pressed the MCU will only read key 1 from the first column and will ignore the other since the routine will have returned. a value corresponding to key 1. This, however, is not bad, as there is no defined way to differentiate between simultaneous key presses; the exact time of pressing can be taken into account, but the fact that two keys were pressed together is an indication of an error from the user's point of view.
Stopwatch
It is well known that changes in gases are relatively slow compared to other phenomena, so it makes no sense to read the sensor at fast rates, but this causes a problem since our MCU works at much faster rates. This is exactly where a timer comes into play. We can use the ATmega328p's built-in timers and track longer time intervals, in our case 100ms. Therefore, we sample the sensor every 100 ms and obtain 5 of these readings, calculate the average of these 5 and use the resulting one for our calculation.
The first thing we need to understand is that the timer works in clock cycles; just like the main MCU; and has a set count beyond which it overflows. For the 8-bit timer it is 255, while it is 65535 for the 16-bit timer (Timer 1) that we will use. The timer clock is a factor of the MCU main clock and can be adjusted accordingly; this is controlled using the prescalar value stored in the special function register. The available prescalar options are divided by 1, 8, 64, 256 and 1024. Since the MCU is clocked at 8 MHz (using internal RC), we can divide the timer clock by 256 to get a tick of 1/(8000000 /256) = 32 microseconds. Now to get a time interval of 100ms we need clicks of 500ms/32 microseconds i.e. (100e-3)/(32e-6) which is 3125.
Since the timer counts from 0 to 65535 per tick of the clock and overflows when it transitions from 65535 to 0, thus generating an overflow interrupt, we need to load the timer count with (65535 – 3125), so that it counts 3125 clicks, giving us a time interval of 100ms. We also need to enable timer overflow interrupt.
The interrupt is automatically generated every 100ms and we can perform our routines in the interrupt service routine (function called when the interrupt is generated). For our application, we just set a 100ms flag and disabled the timer interrupt in the service routine; the flag is cleared, the count is reloaded, and the interrupt is enabled when the 100ms routine is called in the main loop.
Components and schematic details
Gas Sensors
The gas sensor used here is readily available and has several compatible units capable of detecting various gases. Gas sensors are commonly known as MQ series sensors; we will use MQ-6 LPG sensor now. Some other useful sensors available that are very similar in operation include:
MQ-7 – Carbon Monoxide
MQ-3 – Alcohol
MQ-4 – Methane
MQ-8 – Hydrogen
There are many more sensors; It is also important to note that each sensor is also capable of detecting other gases, however, it is designed for a specific gas listed and is therefore most sensitive to that specific gas, as is evident from the response curves provided in the datasheet. .
The sensor works by using a heater to heat the air present around the chemical sensor, this heat acts as a catalyst for chemical reactions releasing electrons and ions according to the gas to be detected, the released electrons flow as current and therefore develop and generate voltage across the load resistor. The voltage developed varies between 0-5V and can therefore be detected using an ADC. It is worth reading the datasheet and obtaining the load resistance values as well as the heater voltage requirements. The MQ-6 has a recommended load resistance value of 20Kohms and a range of 10K-47Kohms. The heater also requires a constant voltage of 5V.
Some sensors specify a burn time (24-48 hours) for which the sensor must remain on for the readings obtained to be reliable, but this can be avoided in our case as we are interested in relative rather than absolute values . It has been found that when you give the sensor a few seconds, it stabilizes at a stable (low) value to start detecting. The MQ-6 GLP sensor has a detection range of 200-1000ppm.
ADC Section
The ADC unit in the MCU works by taking the ADC reference voltage and dividing it into smaller pieces based on the ADC's resolution. ADC resolution is nothing but the number of bits used to represent the output. The ATmega328p has a 10-bit ADC, which means the output can range from 0 to 1023. Therefore, the MCU divides the reference voltage into 1024 pieces. The development board has the ADC reference voltage tied to 5V, taking this as a reference the ADC divides it into 1024 chunks of about 5V/1024 = 4.8mV each. So if the ADC gives a value of 10, it means the voltage at the ADC pin is 10 * 4.8mV = 48mV, similarly for a full scale output of 1023, the voltage at the ADC pin is 1023 * 4, 8mV, which is about 4910mV (the error is due to the rounding of the bit value as well as the ADC precision of +/- 0.5 LSB – 1 LSB).
For this application, we are only interested in getting the raw ADC values from the sensor, so we will use this to calculate intermediate parameters that will be used to calculate the gas concentration.
Display section
A standard 16x2 character LCD module is used in 4-bit mode for display purposes. The interface of an LCD is quite simple and self-explanatory from the source code itself.
Block and circuit diagram
Figure 3: Block diagram of AVR-based LPG leak detector ATMega328P
Figure 2 shows the project's block diagram, while Figure 3 details more circuitry. The development stick constantly checks the keyboard for valid inputs while also obtaining values from the gas sensor; It then calculates the gas concentration and displays it on the LCD.
The circuit is designed around the ATmega328p development stick. The stick's VCC signal is 5V and is used to power the LCD. Most of the connections are self-explanatory, however note that the LCD contrast potentiometer and the transistor to control the LCD backlight are already present on the development stick.
The gas sensor has 6 pins; however, the pin set (A1, HA, A2) and pin set (B1, HB, B2) are interchangeable. In our scheme we apply 5V to heater pins HA, HB and supply pins A1, A2 with 5V and hence the output is obtained at the junction of pins B1, B2. We use a load resistor between this output and ground to form the necessary voltage divider as per the datasheet.
Because the buzzer can draw more current than the I/O pin can supply, it is safer to use a transistor in key mode to activate the buzzer. The transistor used is an NPN transistor, so when a positive voltage (I/O pin for logic high) is applied to the base, it goes into saturation and turns on the buzzer, allowing current to flow from the collector to the emitter. By applying logic low to the base of the transistor, we can turn off the buzzer.
The BC548 is a commonly available general-purpose NPN transistor. The 1K resistor at the base of the transistor is a current limiting resistor that prevents excessive current consumption; however, you can safely use any amount between 330E and 1K.
The general operation is illustrated in the flowchart shown in Figure.
Figure 4: AVR code flowchart for LPG leak detection
Sensor Calibration
The user has the option of pressing the ' C ' button on the keyboard to start the calibration. The MCU will take 5 sensor readings spaced 100 ms apart and each time it calculates the sensor resistance R0 it will average the five readings and store this in the EEPROM. Before calibration, the MCU will use a default value of R0 = 10.
EEPROM Storage
To store the values in the EEPROM we use the standard AVR EEPROM library. Calibration data is stored at address 1 and to ensure data validity, test bytes are placed at locations 0x00 and 0x03. Additionally, the threshold value is stored at address 6, with test bytes at addresses 5 and 8. Each time at startup; the MCU checks whether these test bytes are present; if so, reads the calibration value and limit and uses them for calculation; otherwise, it uses the default value for operation. The default limit is set to 250 ppm.
Pad Sequence Key
To start calibration, the user must press the ' C ' key. The sensor will automatically calibrate. To enter a limit, the sequence is as follows:
1. Press '*' key.
two. Enter 4-digit limit lower than 9999 (max sensor ppm); the typical value may be around 250.
3. Press the '#' key to finish.
Gas concentration reading
Let's see how we obtain sensor readings and can then convert them into actual concentration values in parts per million (ppm). The sensor can be seen (in its theoretical model) as a variable resistor, dependent on the gas concentration in its vicinity. Now, knowing the sensor resistance, we can correlate this information and obtain concentration levels. This seems easy, doesn't it?
Fig. 5: Simplified Circuit Diagram of the Gas Concentration Reader
Let's clarify one principle before we go any further. Consider a voltage divider circuit; Shown above; composed of two resistors. It allows obtaining an output voltage lower than the input, simply because the voltage is divided between the two resistors and as the current that flows through the two resistances in series is the same, the output voltage depends on the resistance values of the components. .
In case of sensor; let V C be the voltage applied to the sensor, that is, 5V, and let Vout be the voltage drop across the load resistance.
Now, by the voltage divider principle, we have
V OUT =V C (RL / (RL + RS))
…where RS = sensor resistance, RL = load resistance
This can be expressed as
RS = ((V C –V OUT ) *RL) /V OUT
Now, since we are dealing with ADC counts, we can substitute V C = 5V = 5V * 1023/1024 (the difference is due to the ADC conversion error of +/- 0.5LSB – 1 LSB), and V OFF = 5* (RAW_ADC/1024). Simplifying we have,
RS=RL* (1023 – RAW_ADC)/RAW_ADC
And this is the equation we are interested in using in our main program.
So just by substituting the RAW_ADC values in the equation above, we can obtain the sensor resistance at that concentration level, dividing this value by R0 – the sensor resistance in clean air, we can obtain RS/R0; a proportion that is the Y axis of the logarithmic graph in the datasheet.
Application
We can also go a step further by finding the curve equation and then substituting the RS/R0 ratio to directly produce the ppm.
The MCU compares the current concentration with the set limit and if the current value exceeds 75% of the limit, then activates the buzzer which remains on until the concentration drops below 75% again.
Figure 6: Character LCD representational image with pin descriptions
The same logic can be applied to work with any other similar sensor in the MQ series.
Application
Having done all this, it makes sense to use this device for real work; here is how you can do that…
REMEMBER TO DO THIS CAREFULLY AND AT YOUR OWN RISK
Use an LPG/isobutane source and check the production variation for different amounts of gas released. The safest source is a lighter. You can use a gas stove, but observe safety and adult supervision is mandatory.
two. Gently press the lighter button to avoid igniting it and place the sensor next to it and measure the power.
3. Repeat the procedure again, keeping the lighter a few centimeters away from the sensor.
4. Use both readings to get a good threshold for determining if you have a gas leak.
5. Set this as your limit and now place the sensor safely in your kitchen, next to the stove. You will be alerted by the LEDs if any leaks are detected.
6. To take it a step further; you can add cell phone/GSM module connectivity so that it will notify you if any leak is detected in your absence.
Although the gas stove is a good reference source, due to safety concerns, it is not recommended to use it as a primary source for setting the limit. However, you can place the sensor very close to the burner without it lighting, then you can check the change in readings, however, you must be careful not to keep the burner lit for more than 3-4 seconds, make sure also that the kitchen is well ventilated and that there are no sources of sparks nearby during the exercise.
Fig. 7: AVR-based LPG gas detector prototype
Job Setup
Fig. 8: Image showing different components of the LPG Detection Kit
Circuit diagrams
Circuit Diagram-AVR-ATMega328P-Based-LPG-Detector-Leak Alarm |