Detects the level of methane “gas” in the room, and can sound an alarm if the level becomes “toxic”! 🙂

What will I learn?

You will learn how to read analog values from a connected sensor and perform some action based on the value read from the sensor. You may even learn about how to react based on digital values.

Prerequisites

A Computer with a configured Arduino IDE tested and working. If you don’t know how to get the basic set up working, check out my guide for Getting started (1/5)

Important notes

  • This project contains components that cannot handle 5V, so take care to use resistors (1 KOhm) with the LEDs.
  • Respect minus and plus, otherwise you may damage the components.
  • The MQ-4 sensor can get pretty hot, so take care if you’ve kept it running for a while!
  • The sensor must be calibrated for production use, so do not expect an 100% accurate result.
  • Start out by figuring out what components can handle 5V and what components can’t. If you connect a component that cant handle 5V you may destroy the component.

What you need

  • Breadboard
  • Dupont wires & jumper wires
  • CP2102
  • Arduino Pro Mini
  • MQ-4: Natural gas, Methane, Coal gas, etc. sensor (detects gas, 200-10.000 ppm)
  • 1 KOhm resistor
  • 1 Color LED

Connecting instructions

  • Arduino VCC → MQ-4 VCC
  • Arduino GND → MQ-4 GND
  • Arduino A0 → MQ-4 A0
  • Arduino D8 → Positive pin of LED
  • LED negative pin → 1KOhm resistor and then to GND

 

Fritzing

 

fart-o-sensor_bb

Code

/**
* Fart alarm - registers level of methane and sounds an alarm when
* reaching a specific level.
*
* Connections:
* - Arduino VCC -> MQ-4 VCC
* - Arduino GND -> MQ-4 GND
* - Arduino A0 -> MQ-4 A0
* - Arduino D8 -> Positive pin of LED
* - LED negative pin -> 1KOhm resistor and then to GND
*
*  Author: Jakob Maaloe
*  https://wackydeveloper.wordpress.com
*/

const int AOUTpin = 0; // AOUT pin of the methane sensor goes into analog pin A0 of the arduino
const int ledPin = 8; // The anode of the LED connects to digital pin D8 of the arduino

int limit = 100; // The custom limit that sounds the alarm
int value;

void setup() {
Serial.begin(115200); // Sets the baud rate for logging
pinMode(ledPin, OUTPUT); // Set the output pin of the arduino
}

void loop()
{
value = analogRead(AOUTpin); // Read the analaog value from the methane sensor's AOUT pin

Serial.print("Methane value: ");
Serial.print(value); // prints the methane value

Serial.print(", Limit: ");
Serial.println(limit); // prints the limit

if (value > limit) {
Serial.println("Alarm!");
digitalWrite(ledPin, HIGH); // If limit has been reached, turn on the LED

}
else {
digitalWrite(ledPin, LOW); // If threshold not reached, LED is off
}
delay(200);
}

Next steps

I wonder what the D0 pin is for on the sensor? Google and try it out… or try out the next project, the Alcohol sensor.

Advertisements