Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Watch now!Tune in to Hackster Holidays, Ep. 4 now!
Shahariar
Published © GPL3+

Gas Leakage Monitor & Ventilation System for Confined Space

A gas leak monitoring device with adjustable sensitivity for detecting natural gas with voice prompt warning & ventilation for fire safety

BeginnerFull instructions provided6 hours2,141

Things used in this project

Hardware components

BBC micro:bit board
BBC micro:bit board
×1
Gravity: Analog CH4 Gas Sensor (MQ4) For Arduino
DFRobot Gravity: Analog CH4 Gas Sensor (MQ4) For Arduino
×1
Gravity: Voice Recorder Module
DFRobot Gravity: Voice Recorder Module
×1
Speaker: 3W, 4 ohms
Speaker: 3W, 4 ohms
×1
Gravity:Analog Rotation Potentiometer Sensor V1 For Arduino
DFRobot Gravity:Analog Rotation Potentiometer Sensor V1 For Arduino
×1
Gravity:Digital Push Button (Yellow)
DFRobot Gravity:Digital Push Button (Yellow)
×1
Grove - 2-Coil Latching Relay
Seeed Studio Grove - 2-Coil Latching Relay
×1
SparkFun TL431 Voltage Referen
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
AC Ventilation Fan
×1
RGY LED Bar Graphe
×1

Software apps and online services

GNAT Community
AdaCore GNAT Community

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematic

Code

main

ADA
main.adb
------------------------------------------------------------------------------
--                                                                          --
--                       Copyright (C) 2018, AdaCore                        --
--                                                                          --
--  Redistribution and use in source and binary forms, with or without      --
--  modification, are permitted provided that the following conditions are  --
--  met:                                                                    --
--     1. Redistributions of source code must retain the above copyright    --
--        notice, this list of conditions and the following disclaimer.     --
--     2. Redistributions in binary form must reproduce the above copyright --
--        notice, this list of conditions and the following disclaimer in   --
--        the documentation and/or other materials provided with the        --
--        distribution.                                                     --
--     3. Neither the name of the copyright holder nor the names of its     --
--        contributors may be used to endorse or promote products derived   --
--        from this software without specific prior written permission.     --
--                                                                          --
--   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    --
--   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      --
--   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  --
--   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   --
--   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
--   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       --
--   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  --
--   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  --
--   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    --
--   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  --
--   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   --
--                                                                          --
------------------------------------------------------------------------------

with MicroBit.IOs;use MicroBit.IOs;
with MicroBit.Time;
with MicroBit.Buttons; use MicroBit.Buttons;



procedure Main is

   --
   
   Detected : Boolean := False;
   Sensor_Min : constant Integer := 220;    -- gas sensor ADC val when no gas
   Range_Step : constant Integer := 15;     -- detection step in a given range
   Sensitivity : MicroBit.IOs.Analog_Value; -- potentiometer ADC value
   Gas_Sensor : MicroBit.IOs.Analog_Value;  -- gas sensor ADC value
   Vref : MicroBit.IOs.Analog_Value;        -- 2.5V external refernec for ADC
   Threshold : Integer;                     -- gas sensor sensing threshold
   Level : Integer;                         -- user selectable sensivity level

   -- Audio MSG enable pin

   Aud : constant MicroBit.IOs.Pin_Id := 15;

   -- latching relay control pins

   Latch : constant MicroBit.IOs.Pin_Id := 13;
   Unlatch : constant MicroBit.IOs.Pin_Id := 14;

   -- LEDs on bar graph Driving I/O pins
   R7 :constant MicroBit.IOs.Pin_Id := 7;
   R6 :constant MicroBit.IOs.Pin_Id := 10;
   Y5 :constant MicroBit.IOs.Pin_Id := 6;
   Y4 :constant MicroBit.IOs.Pin_Id := 0;
   G3 :constant MicroBit.IOs.Pin_Id := 8;
   G2 :constant MicroBit.IOs.Pin_Id := 20;
   G1 :constant MicroBit.IOs.Pin_Id := 19;

   -- LED bargraph driver
   procedure Set_Bargraph(I:Integer);
   procedure Set_Bargraph(I:Integer) is
   begin
      if I >= 7 then
         MicroBit.IOs.Set(R7,False);MicroBit.IOs.Set(R6,False);
         MicroBit.IOs.Set(Y5,False);MicroBit.IOs.Set(Y4,False);
         MicroBit.IOs.Set(G3,False);MicroBit.IOs.Set(G2,False);
         MicroBit.IOs.Set(G1,False);
      elsif I = 6 then
         MicroBit.IOs.Set(R7,True);MicroBit.IOs.Set(R6,False);
         MicroBit.IOs.Set(Y5,False);MicroBit.IOs.Set(Y4,False);
         MicroBit.IOs.Set(G3,False);MicroBit.IOs.Set(G2,False);
         MicroBit.IOs.Set(G1,False);
      elsif I = 5 then
         MicroBit.IOs.Set(R7,True);MicroBit.IOs.Set(R6,True);
         MicroBit.IOs.Set(Y5,False);MicroBit.IOs.Set(Y4,False);
         MicroBit.IOs.Set(G3,False);MicroBit.IOs.Set(G2,False);
         MicroBit.IOs.Set(G1,False);
      elsif I = 4 then
         MicroBit.IOs.Set(R7,True);MicroBit.IOs.Set(R6,True);
         MicroBit.IOs.Set(Y5,True);MicroBit.IOs.Set(Y4,False);
         MicroBit.IOs.Set(G3,False);MicroBit.IOs.Set(G2,False);
         MicroBit.IOs.Set(G1,False);
      elsif I = 3 then
         MicroBit.IOs.Set(R7,True);MicroBit.IOs.Set(R6,True);
         MicroBit.IOs.Set(Y5,True);MicroBit.IOs.Set(Y4,True);
         MicroBit.IOs.Set(G3,False);MicroBit.IOs.Set(G2,False);
         MicroBit.IOs.Set(G1,False);
      elsif I = 2 then
         MicroBit.IOs.Set(R7,True);MicroBit.IOs.Set(R6,True);
         MicroBit.IOs.Set(Y5,True);MicroBit.IOs.Set(Y4,True);
         MicroBit.IOs.Set(G3,True);MicroBit.IOs.Set(G2,False);
         MicroBit.IOs.Set(G1,False);
      elsif I = 1 then
         MicroBit.IOs.Set(R7,True);MicroBit.IOs.Set(R6,True);
         MicroBit.IOs.Set(Y5,True);MicroBit.IOs.Set(Y4,True);
         MicroBit.IOs.Set(G3,True);MicroBit.IOs.Set(G2,True);
         MicroBit.IOs.Set(G1,False);
      elsif I <= 0 then
         MicroBit.IOs.Set(R7,True);MicroBit.IOs.Set(R6,True);
         MicroBit.IOs.Set(Y5,True);MicroBit.IOs.Set(Y4,True);
         MicroBit.IOs.Set(G3,True);MicroBit.IOs.Set(G2,True);
         MicroBit.IOs.Set(G1,True);
      end if;
      MicroBit.Time.Delay_Ms (100);
   end Set_Bargraph;

   -- Exhaust fan control
   procedure Fan_Control(I:Integer);
   procedure Fan_Control(I:Integer) is
   begin
      
      if I = 1 then
         MicroBit.IOs.Set(Latch,False);  MicroBit.IOs.Set(Unlatch,False);
         MicroBit.IOs.Set(Latch,True); MicroBit.Time.Delay_Ms (200);
         MicroBit.IOs.Set(Latch,False);
      elsif I = 0 then
         MicroBit.IOs.Set(Latch,False);  MicroBit.IOs.Set(Unlatch,False);
         MicroBit.IOs.Set(Unlatch,True); MicroBit.Time.Delay_Ms (200);
         MicroBit.IOs.Set(Unlatch,False);
      end if;
    
   end Fan_Control;


   -- main begin   
begin
   -- keep fan off during startup
   Fan_Control(0);
   
   -- test bar graph display   
   for I in 0 .. 7 loop
      Set_Bargraph(I);
      MicroBit.Time.Delay_Ms (100);
   end loop;
   -- turn off bar graph
   Set_Bargraph(0);
   
   --  main loop 
   loop
      --  take user input for detection sensitivity   
      while MicroBit.Buttons.State (Button_B) = Released loop
         Sensitivity := MicroBit.IOs.Analog (2);
         Vref := MicroBit.IOs.Analog (3); -- 2.5 V reference, reads about 880
         Level := Integer(Vref/Sensitivity);
         Set_Bargraph(Level);
      end loop;
      
      --  acknowledge user input been taken with blink
      for I in 0 .. 3 loop
         Set_Bargraph(7);
         MicroBit.Time.Delay_Ms (100);
         Set_Bargraph(0);
         MicroBit.Time.Delay_Ms (100);
      end loop;

      -- gas sensing loop
      while True loop
         -- frequently check for gas leakage
         Gas_Sensor := MicroBit.IOs.Analog(1);
         Threshold := (Integer(Gas_Sensor)-Sensor_Min)/((8-Level)*Range_Step);
         Set_Bargraph(Threshold);
         
         -- if gas leakage detected   
         if Threshold >= 7 then
            Detected := True;
            -- turn on vent fan
            Fan_Control(1);
            
            -- play voice warning repeatedly until user button press acknowledged
            while MicroBit.Buttons.State (Button_B) = Released loop
               MicroBit.IOs.Set(Aud,True);
               MicroBit.IOs.Set(R7,False);MicroBit.IOs.Set(R6,False);
               MicroBit.Time.Delay_Ms (150);
               MicroBit.IOs.Set(R7,True);MicroBit.IOs.Set(R6,True);
               MicroBit.IOs.Set(Aud,False);
               MicroBit.Time.Delay_Ms (150);   
            end loop;
            
         end if;
         
          -- turn off vent fan only if leaked gas is vented out
          -- keep vent fan running if there is gas, even if user pressed button
         if Threshold <= 1 and Detected then
            Fan_Control(0);   Detected := False;
         end if;

      end loop;
      
   end loop;
   
end Main;


-- This project is based on examples/drivers from the following link:
-- https://github.com/AdaCore/Ada_Drivers_Library/tree/master/boards/MicroBit
-- Fabien Chouteau's contribution of ADA driver library on Github
-- DISCLAIMER: License agreement as per contest rules  --

Credits

Shahariar

Shahariar

74 projects • 266 followers
"What Kills a 'Great life' is a 'Good Life', which is Living a Life Inside While Loop"
Thanks to Fabien Chouteau.

Comments