#define MQ2pin 8
int test2 = 0;
int sensorValue; //variable to store sensor value
void setup() {
Serial.begin(9600); // sets the serial port to 9600
pinMode(A0, INPUT);
Serial.println("MQ2 warming up");
delay(200); // allow the MQ2 to warm up
}
void loop() {
// sensorValue = digitalRead(MQ2pin); // read digital output pin
// Serial.print("Digital Output: ");
// Serial.print(sensorValue);
// ทดสอบการเขียนและอ่านค่าด้วยค่า analog
test2 = analogRead(A0);
Serial.print("analog read : ");
Serial.print(test2);
// Determine the status
if (sensorValue) {
Serial.println(" | Smoke: -");
} else {
Serial.println(" | Smoke: Detected!");
}
delay(2000); // wait 2s for next reading
}
// ตำแหน่งของการอ่านค่าใน A0 สามารถอ่านข้อมูล ได้ 10 bit 0-1023