Example Code for Arduino UNO - Air Quality Monitor with LCD

Last revision 2026/01/15

This example is to detect formaldehyde, PM2.5, temperature and humidity in the environment using Arduino UNO and display the data on Gravity LCD1602 RGB Backlight Module.

Hardware Preparation

  • Arduino UNO or DFRobot Leonardo & Xbee R3 (require changing Serial to Serial1) x 1
  • Gravity IO Expansion Shield V7.1 x 1
  • Gravity LCD1602 RGB Backlight Module x 1
  • Air Quality Monitor x 1
  • Micro USB Cable x1
  • M-M/F-M/F-F Jumper wires

Software Preparation

Wiring Diagram

Other Preparation Work

Please download the LCD library: DFRobot RGB LCD.

Sample Code

/*!
 * @file  SEN0233.ino
 * @brief Air Quality Monitor (PM 2.5, HCHO, Temperature & Humidity)
 * @n Get the module here: https://www.dfrobot.com/product-1612.html
 * @n This example is to detect formaldehyde, PM2.5, temperature and humidity in the environment.
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license  The MIT License (MIT)
 * @author  [lijun]([email protected])
 * @version  V1.0
 * @date  2017-07-28
 */

#include <Wire.h>
#include <SoftwareSerial.h>
#include "DFRobot_RGBLCD1602.h"

char col;
unsigned int PMSa = 0, FMHDSa = 0, TPSa = 0, HDSa = 0, PMSb = 0, FMHDSb = 0, TPSb = 0,
             HDSb = 0, PMS = 0, TPS = 0,  HDS = 0, CR1 = 0, CR2 = 0, FMHDS = 0;
unsigned char bufferRTT[32] = {}; //serial receive data
char tempStr[15];

SoftwareSerial mySerial(10, 11); // RX, TX
/*
Change the RGBaddr value based on the hardware version
-----------------------------------------
       Moudule        | Version| RGBAddr|
-----------------------------------------
  LCD1602 Module      |  V1.0  | 0x60   |
-----------------------------------------
  LCD1602 Module      |  V1.1  | 0x6B   |
-----------------------------------------
  LCD1602 RGB Module  |  V1.0  | 0x60   |
-----------------------------------------
  LCD1602 RGB Module  |  V1.1  | 0x2D   |
-----------------------------------------
*/
DFRobot_RGBLCD1602 lcd(/*RGBAddr*/0x60 ,/*lcdCols*/16,/*lcdRows*/2);  //16 characters and 2 lines of show

void setup()
{
  Serial.begin(115200);
  mySerial.begin(9600);
  lcd.init();//Initialize LCD1602 RGB display
  lcd.setRGB(0, 255, 0); //Set the initial backlight color of the display
  lcd.setCursor(0, 0 ); //Set displaying from (0, 0)
  lcd.print("T:");//Display T:
  lcd.setCursor(9, 0 );
  lcd.print("H:");
  lcd.setCursor(0, 1 );
  lcd.print("PM2.5:");
  lcd.setCursor(9, 1 );
  lcd.print("ug/m3");
}

void loop()
{
  while (mySerial.available() > 0) //Check whether there is any serial data
  {
    for (int i = 0; i < 32; i++) //Read serial data
    {
      col = mySerial.read();
      bufferRTT[i] = (char)col;
      delay(2);
    }

    mySerial.flush();
    CR1 = (bufferRTT[30] << 8) + bufferRTT[31];
    CR2 = 0;
    for (int i = 0; i < 30; i++)
      CR2 += bufferRTT[i];
    if (CR1 == CR2)               //Check
    {
      PMSa = bufferRTT[12];      //Read PM2.5 high 8-bit data
      PMSb = bufferRTT[13];      //Read PM2.5 low 8-bit data
      PMS = (PMSa << 8) + PMSb;   //PM2.5 data
      TPSa = bufferRTT[24];      //Read temperature high 8-bit data
      TPSb = bufferRTT[25];      //Read temperature low 8-bit data
      TPS = (TPSa << 8) + TPSb;   //Temperature data
      HDSa = bufferRTT[26];      //Read humidity high 8-bit data
      HDSb = bufferRTT[27];      //Read humidity low 8-bit data
      HDS = (HDSa << 8) + HDSb;   //Humidity data
    }
    else
    {
      PMS = 0;
      FMHDS = 0;
      TPS = 0;
      HDS = 0;
    }
  }
  lcd.setCursor(2, 0 );
  sprintf(tempStr, "%d%d.%d", TPS / 100, (TPS / 10) % 10, TPS % 10); //Display temperature
  lcd.print(tempStr);
  lcd.write(0xdf);              //Display °
  lcd.print('C');               //Display C
  lcd.setCursor(11, 0 );
  sprintf(tempStr, "%d%d.%d", HDS / 100, (HDS / 10) % 10, HDS % 10); //Display humidity
  lcd.print(tempStr);
  lcd.print('%');               //Display %
  lcd.setCursor(6, 1 );
  sprintf(tempStr, "%d%d%d", PMS / 100, (PMS / 10) % 10, PMS % 10); //Display PM2.5 ones, tens, hundreds unit ug/m³ 
  lcd.print(tempStr);
}

Result

The LCD shows the current measured temperature (T), humidity (H) and PM2.5 concentration (PM2.5).

Was this article helpful?

TOP