Advertisement
microrobotics

Pololu Carbon Monoxide & Flammable Gas Sensor MQ-9

Mar 30th, 2023
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*The Pololu Carbon Monoxide & Flammable Gas Sensor MQ-9 is an analog gas sensor that detects the presence of carbon monoxide (CO) and flammable gases such as propane, methane, and butane. Here's an example code in Arduino that reads the gas concentration from the sensor:
  2.  
  3. Note: This code assumes that the Pololu Carbon Monoxide & Flammable Gas Sensor MQ-9 is connected to pin A0 of the Arduino board. If you're using a different pin, you'll need to modify the SENSOR_PIN definition accordingly. Also, the equations used to calculate the gas concentrations were determined experimentally, so you may need to adjust them for your specific sensor. Finally, note that the sensor is designed to detect specific gases and may not detect all flammable gases or CO concentrations in the area.
  4. */
  5.  
  6. const int SENSOR_PIN = A0;  // the pin connected to the sensor
  7. float voltage;  // the voltage reading from the sensor
  8. float co_concentration;  // the carbon monoxide concentration calculated from the voltage
  9. float flammable_concentration;  // the flammable gas concentration calculated from the voltage
  10.  
  11. void setup() {
  12.   Serial.begin(9600);
  13.  
  14.   pinMode(SENSOR_PIN, INPUT);
  15. }
  16.  
  17. void loop() {
  18.   // read the voltage from the sensor
  19.   voltage = analogRead(SENSOR_PIN) * (5.0 / 1023.0);
  20.  
  21.   // calculate the carbon monoxide concentration in parts per million (ppm)
  22.   co_concentration = pow(10, ((1.695 * voltage) - 1.188));
  23.  
  24.   // calculate the flammable gas concentration in parts per million (ppm)
  25.   flammable_concentration = pow(10, ((1.695 * voltage) - 1.188)) * 2.5;
  26.  
  27.   // print the gas concentrations
  28.   Serial.print("Carbon Monoxide Concentration: ");
  29.   Serial.print(co_concentration);
  30.   Serial.println(" ppm");
  31.   Serial.print("Flammable Gas Concentration: ");
  32.   Serial.print(flammable_concentration);
  33.   Serial.println(" ppm");
  34.  
  35.   // delay before reading from the sensor again
  36.   delay(1000);
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement