Skip to Content
This is the Beta version of our new Learning Paths approach. Please email feedback.

Gas Sensor

gas_sensor.ino
/* Project: Smart Home Component: MQ Series Gas Sensor Sketch Description: This sketch has functions to read data from a gas sensor that detects the presence of various gases. Author: STEMVentor Educonsulting This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use. */ // LIBRARIES // PIN DEFINTIONS // See board_parameters.h // We will be skipping the analog read (due to unavailability of pins) // GLOBAL CONSTANTS // GLOBAL VARIABLES // INITIALIZE OBJECTS // LOCAL FUNCTIONS void gasSensorSetup(){ pinMode(GAS_SENSOR_DIGITAL_PIN, INPUT); // You don't need to set pin mode for analog pins. if(OPERATING_MODE == 'D'){ Serial.println("The Gas sensor is ready!"); } } // This can read both the digital and analog values. uint8_t readGasSensorValues(){ // Read the digital value. // The digital value goes HIGH based on the threshold set by manually adjusting // the potentiometer on the sensor manual. uint8_t gas_sensor_digital_value = digitalRead(GAS_SENSOR_DIGITAL_PIN); return gas_sensor_digital_value; // We will be skipping the analog read (due to unavailability of pins). // If required uncomment the code below. // The analog value returns an actual voltage value between 0 and 1024. // You can programmatically decide what is the threshold for triggering some action. // uint8_t gas_sensor_analog_value = analogRead(GAS_SENSOR_ANALOG_PIN); // if(OPERATING_MODE == 'D'){ // Serial.println("Analog Value: " + gas_sensor_analog_value); // } // Convert the analog value to a % value using the map function. // uint8_t gas_sensor_percent_value = map(gas_sensor_analog_value, 0, 1024, 0, 100); // if(OPERATING_MODE == 'D'){ // Serial.println("Percent Value: " + gas_sensor_percent_value); // } }