RAK12047 WisBlock VOC Sensor Module Quick Start Guide
Prerequisite
-
Assemble the RAK12047 with a WisBlock Base and WisBlock Core by following the RAK12047 Installation Guide.
-
Download and install the Arduino IDE, then add the RAKwireless Core boards by installing the RAKwireless Arduino BSP.
-
USB Cable
Initial Test of the RAK12047 WisBlock Module
-
This guide uses the RAK4631 WisBlock Core to demonstrate the configuration process. You can also use the RAK11200 or RAK11310 as the WisBlock Core.
-
When using the RAK11200, configure the BOOT0 pin correctly before uploading the source code. Otherwise, the upload may fail. For detailed instructions, refer to the RAK11200 Quick Start Guide.
-
Install the RAKwireless Arduino BSPs for WisBlock by using the
package_rakwireless_index.jsonboard installation package, the WisBlock Core should now be available on the Arduino IDE. -
You need to select the WisBlock Core you have.
Figure 1: Selecting RAK4631 as WisBlock Core- Next, copy the following sample code into your Arduino IDE:
Click to view the code
/**
@file RAK12047_SGP40_GetVOCIndex.ino
@author rakwireless.com
@brief Get the VOC index data from the sensor SGP40 and print it out through the serial port.
The voc index can directly indicate the quality of the air. The higher the value, the worse the air quality.
@version 0.1
@date 2022-1-22
@copyright Copyright (c) 2020
**/
#include <SensirionI2CSgp40.h> // Click here to get the library: http://librarymanager/All#SensirionI2CSgp40
#include <Wire.h>
SensirionI2CSgp40 sgp40;
void setup()
{
uint16_t error;
char errorMessage[256];
uint16_t serialNumber[3];
uint8_t serialNumberSize = 3;
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
// Initialize Serial for debug output
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
Serial.println("RAK12047 SGP40 example");
Wire.begin();
sgp40.begin(Wire);
error = sgp40.getSerialNumber(serialNumber, serialNumberSize);
if (error)
{
Serial.print("Error trying to execute getSerialNumber(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
else
{
Serial.print("Serial Number:");
Serial.print("0x");
for (size_t i = 0; i < serialNumberSize; i++)
{
uint16_t value = serialNumber[i];
Serial.print(value < 4096 ? "0" : "");
Serial.print(value < 256 ? "0" : "");
Serial.print(value < 16 ? "0" : "");
Serial.print(value, HEX);
}
Serial.println();
}
uint16_t testResult;
error = sgp40.executeSelfTest(testResult);
if (error)
{
Serial.print("Error trying to execute executeSelfTest(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
else if (testResult != 0xD400)
{
Serial.print("executeSelfTest failed with error: ");
Serial.println(testResult);
}
}
void loop()
{
uint16_t error;
char errorMessage[256];
uint16_t srawVoc = 0;
float vocIndex = 0;
/*
* @brief Set the relative humidity and temperature in the current environment.
* Temperature and humidity calibration has been performed inside the sensor.
* RH/ticks=RH/%×65535/100
* T/ticks=(T/°C + 45)×65535/175
*/
uint16_t defaultRh = 0x8000; // 50 %RH
uint16_t defaultT = 0x6666; // 25 ℃
delay(1000);
error = sgp40.measureRawSignal(defaultRh, defaultT, srawVoc);
if (error)
{
Serial.print("Error trying to execute measureRawSignal(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
else
{
Serial.print("SRAW_VOC:");
Serial.print(srawVoc);
vocIndex = (float)srawVoc/131.07 ;
/* VOC index.
* The voc index can directly indicate the quality of the air. The higher the value, the worse the air quality.
* 0-100,no need to ventilate,purify.
* 100-200,no need to ventilate,purify.
* 200-400,ventilate,purify.
* 400-500,ventilate,purify intensely.
*/
Serial.print(" VOC Index:");
Serial.println(vocIndex);
}
}
If you experience any error in compiling the example sketch, check the updated code for your WisBlock Core Module that can be found on the RAK12047 WisBlock Example Code Repository and this sample code in Github will work on all WisBlock Core.
- Once the example code is open, install the Sensirion SGP40 library by clicking the link highlighted in yellow.
Figure 1: Accessing the library used for RAK12047 Module
Figure 1: Installing the compatible library for RAK12047 ModuleDuring the installation, you will be asked to install the other libraries. Install it as well (optional).
Figure 1: Installing other optional Libraries- After successful installation of the library, you can now select the right serial port and upload the code.
Figure 1: Selecting the correct serial port
Figure 1: Uploading the RAK12047 example code- When you have successfully uploaded the example sketch, open the serial monitor of the Arduino IDE to see the sensor's reading logs. If you see the logs, then your RAK12047 is communicating with the WisBlock core properly.
Figure 1: RAK12047 VOC Sensor Module data logs