mq135.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """Micropython library for dealing with MQ135 gas sensor
  2. Based on Arduino Library developed by G.Krocker (Mad Frog Labs)
  3. and the corrections from balk77 and ViliusKraujutis
  4. More info:
  5. https://hackaday.io/project/3475-sniffing-trinket/log/12363-mq135-arduino-library
  6. https://github.com/ViliusKraujutis/MQ135
  7. https://github.com/balk77/MQ135
  8. """
  9. import math
  10. import time
  11. from machine import ADC
  12. class MQ135(object):
  13. """ Class for dealing with MQ13 Gas Sensors """
  14. # The load resistance on the board
  15. RLOAD = 10.0
  16. # Calibration resistance at atmospheric CO2 level
  17. RZERO = 76.63
  18. # Parameters for calculating ppm of CO2 from sensor resistance
  19. PARA = 116.6020682
  20. PARB = 2.769034857
  21. # Parameters to model temperature and humidity dependence
  22. CORA = 0.00035
  23. CORB = 0.02718
  24. CORC = 1.39538
  25. CORD = 0.0018
  26. CORE = -0.003333333
  27. CORF = -0.001923077
  28. CORG = 1.130128205
  29. # Atmospheric CO2 level for calibration purposes
  30. ATMOCO2 = 397.13
  31. def __init__(self, pin):
  32. self.pin = pin
  33. def get_correction_factor(self, temperature, humidity):
  34. """Calculates the correction factor for ambient air temperature and relative humidity
  35. Based on the linearization of the temperature dependency curve
  36. under and above 20 degrees Celsius, asuming a linear dependency on humidity,
  37. provided by Balk77 https://github.com/GeorgK/MQ135/pull/6/files
  38. """
  39. if temperature < 20:
  40. return self.CORA * temperature * temperature - self.CORB * temperature + self.CORC - (humidity - 33.) * self.CORD
  41. return self.CORE * temperature + self.CORF * humidity + self.CORG
  42. def get_resistance(self):
  43. """Returns the resistance of the sensor in kOhms // -1 if not value got in pin"""
  44. adc = ADC(self.pin)
  45. value = adc.read()
  46. if value == 0:
  47. return -1
  48. return (1023./value - 1.) * self.RLOAD
  49. def get_corrected_resistance(self, temperature, humidity):
  50. """Gets the resistance of the sensor corrected for temperature/humidity"""
  51. return self.get_resistance()/ self.get_correction_factor(temperature, humidity)
  52. def get_ppm(self):
  53. """Returns the ppm of CO2 sensed (assuming only CO2 in the air)"""
  54. try:
  55. return self.PARA * math.pow((self.get_resistance()/ self.RZERO), -self.PARB)
  56. except:
  57. return 0
  58. def get_corrected_ppm(self, temperature, humidity):
  59. """Returns the ppm of CO2 sensed (assuming only CO2 in the air)
  60. corrected for temperature/humidity"""
  61. try:
  62. return self.PARA * math.pow((self.get_corrected_resistance(temperature, humidity)/ self.RZERO), -self.PARB)
  63. except:
  64. return 0
  65. def get_rzero(self):
  66. """Returns the resistance RZero of the sensor (in kOhms) for calibratioin purposes"""
  67. return self.get_resistance() * math.pow((self.ATMOCO2/self.PARA), (1./self.PARB))
  68. def get_corrected_rzero(self, temperature, humidity):
  69. """Returns the resistance RZero of the sensor (in kOhms) for calibration purposes
  70. corrected for temperature/humidity"""
  71. return self.get_corrected_resistance(temperature, humidity) * math.pow((self.ATMOCO2/self.PARA), (1./self.PARB))
  72. def mq135lib_example():
  73. """MQ135 lib example"""
  74. # setup
  75. temperature = 21.0
  76. humidity = 25.0
  77. mq135 = MQ135(0) # analog PIN 0
  78. # loop
  79. while True:
  80. rzero = mq135.get_rzero()
  81. corrected_rzero = mq135.get_corrected_rzero(temperature, humidity)
  82. resistance = mq135.get_resistance()
  83. ppm = mq135.get_ppm()
  84. corrected_ppm = mq135.get_corrected_ppm(temperature, humidity)
  85. print("MQ135 RZero: " + str(rzero) +"\t Corrected RZero: "+ str(corrected_rzero)+
  86. "\t Resistance: "+ str(resistance) +"\t PPM: "+str(ppm)+
  87. "\t Corrected PPM: "+str(corrected_ppm)+"ppm")
  88. time.sleep(0.3)
  89. if __name__ == "__main__":
  90. mq135lib_example()