Product Introduction
/* MQ-4 Methane Sensor Circuit with Arduino */
const int AOUTpin = A0; //the AOUT pin of the methane sensor goes into analog pin A0 of the arduino
const int DOUTpin = 7; //the DOUT pin of the methane sensor goes into digital pin D8 of the arduino
const int buzzerPin = 8; //the anode of the LED connects to digital pin of the arduino
int gasLimit = 80;
int limit;
int value;
void setup()
{
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);//sets the baud rate
pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino
pinMode(buzzerPin, OUTPUT);//sets the pin as an output of the arduino
}
void loop()
{
value = analogRead(AOUTpin); //reads the analaog value from the methane sensor's AOUT pin
limit = digitalRead(DOUTpin); //reads the digital value from the methane sensor's DOUT pin
Serial.print("Methane value: ");
Serial.println(value);//prints the methane value
Serial.print("gasLimit: ");
Serial.println(gasLimit);//prints the limit reached as either LOW or HIGH (above or underneath)
Serial.print("limit: ");
Serial.println(limit);
delay(100);
if (value > gasLimit)
{
beep(255);
}
}
void beep(unsigned char delayms)
{
Serial.println("beep in voidloop");
analogWrite(buzzerPin, 255); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms); // wait for a delayms ms
analogWrite(buzzerPin, 0); // 0 turns it off
delay(delayms); // wait for a delayms ms
}