Arduino Leonardo Gas Sensor Example

Gas Sensor Example

This example shows you how to use a gas sensor.
In particular in this sketch is used a MQ3-Alcohol Gas Sensor but you can use an other gas sensor.
The MQ3 is an heater-driven sensor that it gives as output an analogic signal converted an value between 150 and 1023 depending on how long you let the sensor warm up.
In this example it is gotten the read value from sensor and it is compared with a threshold. If this value exceed the threshold it is played a buzzer, it is turned on a red led and it is printed a warning notice on Serial monitor.

You can download the datasheet of MQ3 sensor here .

Hardware Required

  • Arduino Board
  • Gas sensor Mq3
  • Wires
  • Breadboard
  • 1 Green led
  • 1 Red led
  • 1 piezo buzzer
  • 2 resistor 220Ω
  • 1 resistor 10KΩ


The sensor has six pins how it is shown in the figure.
It isn't important the side of sensor.
Connect one of the H pin to 5V and the other one to Ground (GND) of board.
Also connect Pin A to the 5V pin and the Pin B to analogic Pin "A0" and to the GND, through a resistor of 10kΩ, as shown in the picture down.
Connect the anode of the red LED (usually the longer pin) in series to a resistence of 220Ω and connect it to pin 8 of board and connect the cathode of led to GND of board.
Also connect, in the same way, the green led but taking care to connect the anode to pin 9.
Finally connect the positive pole of buzzer to pin 7 of board and the negative to the Ground.
Once realized the circuit, upload the code in your Arduino board and open the Serial monitor of the IDE.




First we define the variables used in the sketch and in particular we choose the threshold value.
In the setup function we initialize the Serial comunication, we define as output the variables red_led, green_led and buzzer and finally we set the leds at LOW level.
In the loop block we read the sensor values and and we print them on serial monitor. If this values exceed the threshold then we play the buzzer, we turn on the red led and we do to print a warning notice on Serial monitor. Else the buzzer doesn't play and the green led is turned on.

You can change the threshold, modifying the value.

The complete code and its detailed description are shown down.

/*Gas sensor example*/
int mq3 = A0; //connect Gas sensor to analogic pin A0
int buzzer = 7;//connect buzzer to digital pin 7
int red_led = 8;//connect red led to digital pin 8 
int green_led = 9;//connect green led to digital pin 9
int threshold =300; //change the threshold value for your use
void setup(){
   Serial.begin(9600); //initialize serial comunication at 9600 bps
   pinMode(red_led, OUTPUT);//define red_led as output
   pinMode(green_led, OUTPUT); //define green_led as output
   pinMode(buzzer, OUTPUT); //define buzzer as output
   digitalWrite(red_led, LOW);//red_led off
   digitalWrite(green_led, LOW);//green_led off

void loop()
    int mq3 = analogRead(mq3);//read sensor value
    Serial.println(mq3);//print on serial monitor the sensor value
    if(mq3>=threshold){//check if it is exceeded the threshold value
        digitalWrite(red_led,HIGH);//red_led on
        digitalWrite(green_led,LOW);//green_led off
        tone(buzzer,200);//play buzzer
        Serial.println(" Attention threhold exceeded!!!");//print on serial monitor the overcoming threshold notice
        digitalWrite(red_led,LOW);//red_led off
        digitalWrite(green_led,HIGH);//green_red on
        noTone(buzzer);no play buzzer 
    delay(100); //slow down the output

This is how the Gas Sensor Example is shown on the new Arduino Studio. Check it out!