I'm currently using an mq5 gas sensor in my kitchen with the MCP3208 ADC. my code shows a continuous loop of the voltage charge. in the mq5 as it detects gases in the air. It is supposed to print ("Gas leak") on line 9 when the voltage reaches the set threshold of 0.50 but it does not. Am i doing something wrong?

#main loop
def main():
     while True:
              smokelevel=readadc(smokesensor_apin, SPICLK, SPIMOSI, SPIMISO, SPICS)

              if GPIO.input(smokesensor_dpin)> 0.50 :
                       if GPIO.input(smokesensor_dpin) > 0.50 :
                           print("Gas leak")

                       print("Gas lealage")
                       print"Current Gas AD vaule = " +str("%.2f"%((smokelevel/1024.)*5))+" V"

if __name__ =='__main__':
     except KeyboardInterrupt:
  • i think that GPIO.input will return a digital value, ie a one or zero and am confused as to how you plan on comparing that to 0.50 Sep 10, 2017 at 18:27
  • ok yes, i understand the digital usage of the pins...what i actually need the code to do is tell the analog to digital converter to alert me when the voltage on the sensor exceeds a threshold. Sep 10, 2017 at 23:57
  • 1
    pretty sure you're going to need to look up the spec sheet for your adc. their are many tutorials online for this: learn.adafruit.com/raspberry-pi-analog-to-digital-converters/… Sep 10, 2017 at 23:59

2 Answers 2


Code snippets are rarely conclusive. We have to guess at the missing parts.

All the Pi's GPIO are digital. They return 0 or 1, which is what I guess your GPIO.input() function is doing.

  • Would it help if I post the full code here ? Sep 20, 2017 at 19:05

If you want to interface MQ sensors with raspbery pi you will need a analog to digital converter, on you can buy a breakout board whihc has MQ sensor and ADC on the same board like this one You can use this python script to read MQ5 gas sensor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.