Welcome, In this tutorial we will simulate how to use the gas sensor,
to detect the presence of a toxic gas, or smoke, in a given location.
we will process and display the results, using an LCD display and 2 indicator lights.
The MQ2 gas sensor is used for the detection of gas
leaks for equipment in the consumer and industrial markets.
This sensor  detects LPG, i-butane, propane, methane, alcohol, hydrogen and smoke.
He has great sensitivity and a quick response time. his sensitivity can also be adjusted by a potentiometer.
it has two outputs: analog and digital, for this project we use the digital output.
Open Proteus, new project, you can name it gas sensor,
find a place to save it, choose a default project, and follow the steps.
Open the component library, search for Arduino Uno, or any other ,
if you can't find it, you download it from the net, and add it to your library.
search for the Gas MQ-2 detector, you can download and add it to your library.
It is available on the website www.theengineeringprojects.com, the download link is in the description below.
choose a resistance of 560 ohms, that we will put it in series with the witness lights.
Bring a green LED.
Bring a red LED.
search for the LCD display 2 lines and 16 columns, it is more than enough for this application.
add a status indicator to indicate the presence of gas or smoke.
We position our Arduino in the middle, then we slide the gas sensor, and the status indicator.
We add a voltage source, we name it Vcc, And a mass, then we connect all that.
The mass with the mass of the Arduino, and the sensor. Like for the voltage sources,
connect the status indicator with the Test Pin input.
We connect the OUT pin of the sensor with the digital pin 9 of the Arduino.
It is a choice, you can choose any digital pin as long as you configure it as input.
place our LED , we start with the green in series with the resistance  of 560 ohms. Then we put the red LED.
If there is gas or smoke in the room, the red light will be turned on, and a message will appear. else,  the green LED is turned on.
connect the red bulb with the digital pin 6, and the green bulb with the digital pin 7.
Of course we don't forget the mass.
still have the LCD display to set up.  connect pins D4 to D7 of the display with the digital inputs / outputs of the Arduino 2 to 5.
You can modify this choice, I'll show you later how to initialize all this in the program.
connect the Pin E with 11, and RS with 12. Vss and RW with the mass. This wiring method is almost a convention, unless there are space constraints.
Open the Arduino compiler,
the first thing to do is to declare the LIQUID CRYSTAL library which contains the functions of the display.
Then, we call the LCD function which is used to initialize the digital inputs / outputs of the Arduino with the display,
The two first  parameters are the numbers of  the digital pin Arduino  , linked with the E and RS Pin of the LCD display.
the other 4 parameter, are the Pin of Arduino connected with the Pin, D2 to D7 on the LCD display.
All of these inputs / outputs are digital.
declare a Gas as an integer, wich represents the number of the Pin that will read the state of the sensor.
declare 2 integers: redled and greenled, which represent the digital output numbers of Pin connected with the red and green LED bulbs respectively.
configure the digital pin 9 as inputs, it will certainly have 2 states, that is HIGH which indicates the presence of a gas or smoke. Or LOW which indicates no Gas or Smoke.
If we read the value of digital input 9 is HIGH or equal to 1, then
Position the display cursor on the zero line and the zero column
And write the following message: Gas detected Yes.
And turn on the red light.
And of course turn off the Green light.
Otherwise,
if there is no gas or smoke.
Then position yourself on the line and the column 0,
And write: No gas detected,
and turn on the green light.
and turn off the red light, With the digital write function (digital output number, status)
wait 500 milliseconds, the time to read the results.
clear the display and resume the cycle again.
We're trying to simulate all of this. We  save the project .
I'm correcting some spelling mistakes.
if you want the file to be accepted by the Arduino, you need to export it in the form. HEX,
For that, go to sketch, then choose: export compiled binary, you will find it with your Arduino file.
Return to Proteus,
make sure the LEds bulbs have a digital configuration.
Double click on the Gas sensor, search its  HEX file, it is in the Gas Sensor Library for Proteus,
In the folder that I left  in the description below. It also contains the whole project.
Or download it from www.theengineeringprojects.com,
Then, Double click on the Arduino and you import the file. HEX that you have compiled.
start the simulation, switch the state of the sensor to verify that everything is working well.
See you next time for another Arduino tutorial.
