Arduino 101: Using a Gas sensor module

in #utopian-io8 years ago (edited)

What Will I Learn?

  • You will learn how to use the Gas (MQ2) sensor.
  • You will learn the basic functions of the gas sensor.
  • You will learn how to integrate the gas sensor with the arduino uno board.

Requirements

Hardware

  • Arduino Uno

image.png

Image source

  • Gas sensor (MQ2)

image.png

Image Source

  • LED (2 pcs)

image.png

Image source

  • Resistors (220 ohms)

image.png

Image source

  • Breadboard

image.png

Image source

  • Jumper wires

image.png

Image source

  • Computer

image.png

Image source

  • USB cable type A to B

image.png

Image source

Software

  • Arduino software / Arduino IDE

Knowledge

  • Basic electronics and programming knowledge

Difficulty

  • Basic

Project description

  • This project uses a gas(MQ-2) sensor that is controlled by an arduino uno board. The LED will be indicating the status of the gas sensor, the green led will light up on normal situation where there is no gas leakage in the surrounding, once the sensor detects the gas leakage the red LED will light up signifying that there is a leakage of gas in the surrounding.

Components description

Arduino Uno - type of arduino board that is regularly used by specialist and hobbyist because of its robust design. It is packed with a microcontroller board that is based on ATMega328P with 14 advanced I/O pins (6 are PWM outputs), 6 analog inputs, 16 Mhz quartz crystal, a power jack, a USB port, an ICSP header and a reset button.

Gas sensor (MQ-2) module - is a very useful device that detects many types of gas (H2, LPG, CH4, CO, Alcohol, Smoke or Propane). The resistance of this sensor varies depending on the type of the gas. This sensor has a built-in potentiometer where you can adjust the sensor's sensitivity.

image.png

Image source

Tutorial Contents

Using a Gas sensor module

Step 1: Gather the Parts

  • You can purchase the components in your nearby electronics shop or you can order the parts online.

image.png

Step 2: Construct the circuit

Connect the sources

  • Connect the 5V pin of the arduino board into the top/bottom slot of the breadboard. The connections here are connected horizontally, so it is ideal as the power grid and for common ground.
  • Connect the GND pin of the arduino board into the top/bottom slot of the breadboard but make sure not to short them our to avoid damage to the board.

image.png

Connect the sensor

  • Connect the VCC pin of the gas sensor to the positive terminal(+) in the breadboard.
  • Connect the GND pin of the gas sensor to the negative terminal(-) in the breadboard.
  • Connect the A0 (analog) pin of the gas sensor to the analog pin A5 of the arduino uno board.

image.png

Connect the LEDs

  • Connect the anode(+) of the green LED to pin number 11 of the arduino uno board.
  • Connect the cathode(-) of the green LED to the resistor.
  • Connect the 220 ohms resistor to the common ground and to the cathode of the LED, this will limit the current that will pass through the LED to protect it from over supply of current that could damage the LED.
  • Connect the anode (+) of the red LED to pin number 12 of the arduino uno board.
  • Connect the cathode (-) of the red LED to the resistor.
  • Connect the 220 ohms resistor to the common ground and to the cathode of the LED.

image.png

Step 3: Programming

  • Connect the arduino Uno board into the computer by using the USB type a to b cable.

image.png

Image source

  • Once connection has been established, open the arduino IDE and go to Tools > Board: > then select Arduino/Genuino Uno.

image.png

  • Copy the code below and paste it into your sketch in the arduino IDE.
//defines the led and the sensor
int red = 12;     
int green = 11;
int gasA0 = A5;
int threshold = 400; //threshold value

void setup() {
  pinMode(red, OUTPUT);     //sets the red led as output
  pinMode(green, OUTPUT);   //sets the green led as output
  pinMode(gasA0, INPUT);    //sets the gas sensor as input
}

void loop() {
  int analogSensor = analogRead(gasA0);   //reads the data from the sensor
  // checks if it has reached the threshold value
  if (analogSensor > threshold)
  {
    digitalWrite(red, HIGH);
    digitalWrite(green, LOW);
  }
  else    //if analog < threshold, normal condition
  {
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
  }
  delay(100); //delay of 100 microseconds
}

What this code does is simple, first it defines the leds and the sensor pins. Then it sets up the threshold level to 400, and sets the leds as the output and the sensor to be the input. Once the sensor detects gas above the value of 400 the arduino uno will immediately turn the red led ON to indicate that there is a gas leakage. If the sensor detects no gas from the surrounding, the green LED will keep on lighting to indicate that everything is normal.

  • After you have typed in the code into your sketch in the arduino IDE, press the Verify button to save and compile the sketch. This will also check for any errors in the program.

image.png

  • Once no error is found, click the Upload button to start the installation.

image.png

Step 4: Testing

  • Once the program has finished, unplug the arduino board from the computer and hook it up with your battery pack.
  • Turn the arduino uno on, after it boots up and the surrounding is gas-free the green LED should light up steadily.
  • Now, spray some butane gas or whatever gas you have to the sensor, this should be picked up by the sensor and the red LED would light up to indicate that gas is above the threshold level.
  • Note: Be very careful with the gas when testing as this can easily burn you and your surrounding.

Curriculum

Check out my other Arduino tutorials here:



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thank you very much :)

Hey @forkonti, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Hey @ted7 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.033
BTC 116555.50
ETH 4489.25
SBD 0.86