[HELP] MQ2 gas sensor and fan programming

Please help me

For my project, the wroking process is

MQ2 -> Fan

After detect the gas the fan should spinning

This is actually switch program, Pin 2 for switch and pin 13 for LED. But can I use the MQ2 instead of switch and led light use as fan to spin?

int inPin = 2; // the number of the input pin, actually for switch int outPin = 13; // the number of the output pin, actually for fan

int state = HIGH; // the current state of the output pin int reading; // the current reading from the input pin int previous = LOW; // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. // the last time the output pin was toggled long debounce = 0; // the debounce time, increase if the output flickers

void setup() { pinMode(inPin, INPUT); pinMode(outPin, OUTPUT); }

void loop() { reading = digitalRead(inPin);

// if the input just went from LOW and HIGH and we've waited long enough // to ignore any noise on the circuit, toggle the output pin and remember // the time { if (state == HIGH) state = LOW; else state = HIGH;


digitalWrite(outPin, state);


Do you have a bare sensor ? then please be careful how to connect it. Or do you have a module, perhaps even with a potentiometer ?

This page shows some information about the sensors: http://playground.arduino.cc/Main/MQGasSensors

You need an analog pin to read the analog value. After the preheat time, you can determine at what value the fan should start.