//GAS sensor output pin to Arduino analog A0 pin #define R 2 #define Y 4 #define MQ3 A0 int gaslevel; // UltraSonic Sensor interfacing to Arduino . int buzzer = 9; int triggerPin = 7; //triggering on pin 7 int echoPin = 8; //echo on pin 8 void setup() { Serial.begin(9600); //we'll start serial comunication, so we can see the distance on the serial monitor Serial.println("Tech Ponder's UltraSonic Sensor Tutorial"); pinMode(triggerPin, OUTPUT); //defining pins pinMode(echoPin, INPUT); pinMode(buzzer, OUTPUT); digitalWrite(buzzer,LOW); pinMode(MQ3,INPUT); pinMode(R,OUTPUT); pinMode(Y,OUTPUT); } void loop() { int duration, distance; //Adding duration and distance digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED) delay(10); digitalWrite(triggerPin, LOW); duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number delay(1000); Serial.print(distance); //printing the numbers Serial.print("cm"); //and the unit Serial.println(" "); //just printing to a new line if (distance < 35) { digitalWrite(buzzer,HIGH); Serial.println("Buzzer On"); } digitalWrite(buzzer,LOW); gaslevel=(analogRead(MQ3)); gaslevel=map(gaslevel,0,1023,0,255); if(gaslevel > 100 && gaslevel <= 300){//gaslevel is greater than 100 and less than 300 digitalWrite(R,LOW);//RED led is off _delay_ms(500);//delay digitalWrite(Y,HIGH);//YELLOW led is on _delay_ms(500); } else if(gaslevel > 300 && gaslevel <= 600){//gaslevel is greater than 300 and less than 600 digitalWrite(Y,LOW);//YELLOW led is off _delay_ms(500); digitalWrite(R,HIGH);//RED led is on } else { digitalWrite(R,LOW);//red led is off digitalWrite(Y,LOW);//YELLOW led is off } Serial.println(gaslevel);//print values on serial monitor _delay_ms(100); }