Example Code for Arduino-Threshold alarm function

In this routine, users will learn how to set a threshold alarm for the gas sensor. When the gas concentration exceeds the set threshold, the ALA pin will output a high level, and a warning message will be printed to the serial monitor.

Hardware Preparation

  • DFRuino UNO R3 x1
  • DFR0784 Smart Gas Sensor Terminal x1
  • Gas probe x1
  • Jumper wires

Software Preparation

The initial power-on requires more than 5 minutes of preheating. It is recommended to preheat more than 24 hours if it has not been used for a long time.
After switching the communication mode or changing the I2C address, the system needs to be powered off and on again.

Wiring Diagram

Other Preparation Work

  • Connect the module to the Arduino according to the connection diagram above. Of course, you can also use it with Gravity I/O Expansion Board to build the project prototype more conveniently and quickly.
  • Set the DIP switch SEL on the sensor to 0, and use I2C communication by default.
  • The default I2C address is 0x74. If you need to modify the I2C address,You can configure the hardware I2C address through the DIP switch on the module, or run the code to modify the address group to modify the address. The corresponding relationship between the DIP switch and the I2C address parameter is as follows:
    • ADDRESS_0: 0x74, A0=0, A1=0
    • ADDRESS_1: 0x75, A0=0, A1=1
    • ADDRESS_2: 0x76, A0=1, A1=0
    • ADDRESS_3: 0x77, A0=1, A1=1
  • Download and install the DFRobot_GasSensor Library. (About how to install the library?)
  • Open Arduino IDE and upload the following code to Arduino UNO.
  • Open the serial port monitor of Arduino IDE, adjust the baud rate to 115200, and observe the serial port print result.

Sample Code

/*!
  * @file  setThresholdAlarm.ino
  * @brief Set the threshold alarm of the sensor
  * @n Experiment method: Connect the sensor communication pin to the main control and burn codes into it. 
  * @n Communication mode selection, dial switch SEL:0: IIC, 1: UART
*/
#include "DFRobot_MultiGasSensor.h"

//Enabled by default, use IIC communication at this time. Use UART communication when disabled
#define I2C_COMMUNICATION

#ifdef  I2C_COMMUNICATION
#define I2C_ADDRESS    0x77
  DFRobot_GAS_I2C gas(&Wire ,I2C_ADDRESS);
#else
#if (!defined ARDUINO_ESP32_DEV) && (!defined __SAMD21G18A__)
/**
  UNO:pin_2-----RX
      pin_3-----TX
*/
  SoftwareSerial mySerial(2, 3);
  DFRobot_GAS_SoftWareUart gas(&mySerial);
#else
/**
  ESP32:IO16-----RX
        IO17-----TX
*/
  DFRobot_GAS_HardWareUart gas(&Serial2); //ESP32HardwareSerial
#endif
#endif

#define ALA_pin 4

void setup() {

  Serial.begin(115200);
  
  while(!gas.begin())
  {
    Serial.println("NO Deivces !");
    delay(1000);
  }

  while (!gas.changeAcquireMode(gas.PASSIVITY))
  {
    delay(1000);
  }
  Serial.println("change acquire mode success!");

  while (!gas.setThresholdAlarm(gas.ON, 2, gas.HIGH_THRESHOLD_ALA ,gas.queryGasType()))
  {
    Serial.println("Failed to open alarm!");
    delay(1000);
  }
  pinMode(ALA_pin,INPUT);
}

void loop() {

  Serial.print(gas.queryGasType());
  Serial.print(":");
  Serial.println(gas.readGasConcentrationPPM());
  if (digitalRead(ALA_pin) == 1)
  {
    Serial.println("warning!!!");
  }
  else
  {
    Serial.println("nolmal!!!");
  }
  delay(200);
}

Result

-**After uploading the code successfully, open the serial monitor and you can observe the alarm message. **

-ALA outputs low level by default when no alarm is triggered. Modify the HIGH_THRESHOLD_ALA parameter in the gas.setThresholdAlarm function to LOW_THRESHOLD_ALA, then ALA outputs high level when no alarm is triggered

Was this article helpful?

TOP