Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MQ135.h>
- MQ135 gasSensor = MQ135(5);
- int maxppm = 1000; //Ppm max value allowed by “law”
- int comfortval = 600; //Ppm comfort val
- int fanOutput = 10; //Digital pin 10 with analog write capabilities
- int warningLED = 7; //Digital led pin
- int analogSensorPin = 5;
- int sensorValue;
- float ppm; //CO2 levels in PPM
- float rzero; //Only required for calibration
- void setup() {
- //delay(120000); //2min sensor initialize
- Serial.begin(9600); //Start console
- pinMode(fanOutput, OUTPUT); //Fan output
- pinMode(warningLED, OUTPUT); //Fan output
- rzero = gasSensor.getRZero(); //Only required for calibration
- //Print RZero nicely
- Serial.print("===== RZero: ");
- Serial.print(rzero);
- Serial.print(" =====");
- Serial.println("");
- }
- void loop() {
- digitalWrite(warningLED, 0); //Set led voltage to zero to make sure there’s no misunderstandings
- sensorValue = analogRead(analogSensorPin); //Analog sensor input
- ppm = gasSensor.getPPM(); //Grap current ppm val for CO2 using the lib
- Serial.println(ppm); //Print ppm val
- if (ppm > maxppm) //WARNING: Limit reached! Set red LED.
- {
- digitalWrite(warningLED, 1);
- }
- if (ppm > comfortval) //Reaching high levels of CO2, set full power to fan.
- {
- analogWrite(fanOutput, 0);
- }
- else //Half power (normal speed)
- {
- analogWrite(fanOutput, 127.5);
- }
- delay(1000); //Only check every second.
- }
Advertisement
Add Comment
Please, Sign In to add comment