Charflow

MQ135 sensor program Arduino

Dec 17th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <MQ135.h>
  2. MQ135 gasSensor = MQ135(5);
  3. int maxppm = 1000; //Ppm max value allowed by “law”
  4. int comfortval = 600; //Ppm comfort val
  5. int fanOutput = 10; //Digital pin 10 with analog write capabilities
  6. int warningLED = 7; //Digital led pin
  7. int analogSensorPin = 5;
  8. int sensorValue;
  9. float ppm; //CO2 levels in PPM
  10. float rzero; //Only required for calibration
  11.  
  12. void setup() {
  13.   //delay(120000); //2min sensor initialize
  14.   Serial.begin(9600); //Start console
  15.   pinMode(fanOutput, OUTPUT); //Fan output
  16.   pinMode(warningLED, OUTPUT); //Fan output
  17.   rzero = gasSensor.getRZero(); //Only required for calibration
  18.   //Print RZero nicely
  19.   Serial.print("===== RZero: ");
  20.   Serial.print(rzero);
  21.   Serial.print(" =====");
  22.   Serial.println("");
  23. }
  24.  
  25. void loop() {
  26.   digitalWrite(warningLED, 0); //Set led voltage to zero to make sure there’s no misunderstandings
  27.   sensorValue = analogRead(analogSensorPin); //Analog sensor input
  28.   ppm = gasSensor.getPPM(); //Grap current ppm val for CO2 using the lib
  29.   Serial.println(ppm); //Print ppm val
  30.  
  31.   if (ppm > maxppm) //WARNING: Limit reached! Set red LED.
  32.   {
  33.     digitalWrite(warningLED, 1);
  34.   }
  35.  
  36.   if (ppm > comfortval) //Reaching high levels of CO2, set full power to fan.
  37.   {
  38.     analogWrite(fanOutput, 0);
  39.   }
  40.  
  41.   else //Half power (normal speed)
  42.   {
  43.     analogWrite(fanOutput, 127.5);
  44.   }
  45.   delay(1000); //Only check every second.
  46. }
Advertisement
Add Comment
Please, Sign In to add comment