Example Code for Arduino-DAC Mode
This example reads the concentration of HCHO in air by DAC mode.
Hardware Preparation
| Name | Model/SKU | Quantity |
|---|---|---|
| DFRduino UNO (or similar) | DFRduino UNO | 1 |
| HCHO Sensor Module | SEN0231 | 1 |
| Gravity 3P Cable | Gravity PH2.0 3Pin Cable | 1 |
Software Preparation
- Development tool: Arduino IDE (Download Arduino IDE)
- No additional libraries required (sample code is standalone).
Wiring Diagram

Other Preparation Work
Before using the sensor in DAC mode, slide the switch on the module to the DAC position. In this mode, the sensor outputs an analog voltage that can be read through an Arduino analog input.
In DAC mode, measurement accuracy depends on the ADC resolution and reference voltage. For best performance, use a stable power supply or reference voltage together with a 10-bit (or higher) ADC.
For best performance, allow the module to warm up for about 5 minutes during first-time use to ensure stable output.
The relationship between output voltage (V) and HCHO concentration (ppm) is linear:
- 0.4 V → 0 ppm
- 2.0 V → 5 ppm
The corresponding linear function graph is shown below:

Connection Diagram

Sample Code
Click here to download the sample code.
/***************************************************
DFRobot Gravity: HCHO Sensor
<https://www.dfrobot.com/wiki/index.php/Gravity:_HCHO_Sensor_SKU:_SEN0231>
***************************************************
This example reads the concentration of HCHO in air by DAC mode.
Created 2016-12-15
By Jason <[email protected]@dfrobot.com>
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
****************************************************/
/***********Notice and Trouble shooting***************
1. This code is tested on Arduino Uno with Arduino IDE 1.0.5 r2.
2. In order to protect the sensor, do not touch the white sensor film on the sensor module,
and high concentration of Hydrogen sulfide, hydrogen, methanol, ethanol, carbon monoxide should be avoided.
3. Please do not use the modules in systems which related to human being’s safety.
****************************************************/
#define SensorAnalogPin A2 //this pin read the analog voltage from the HCHO sensor
#define VREF 5.0 //voltage on AREF pin
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print(analogReadPPM());
Serial.println("ppm");
delay(1000);
}
float analogReadPPM()
{
float analogVoltage = analogRead(SensorAnalogPin) / 1024.0 * VREF;
float ppm = 3.125 * analogVoltage - 1.25; //linear relationship(0.4V for 0 ppm and 2V for 5ppm)
if(ppm<0) ppm=0;
else if(ppm>5) ppm = 5;
return ppm;
}
Result
As indicated above, the value will be printed on the serial monitor every second.
Additional Information
- Avoid touching the white sensing film during use.
- Keep the sensor away from high concentrations of hydrogen sulfide, hydrogen, methanol, ethanol, and carbon monoxide, as these gases may affect accuracy and reduce service life.
- We recommend priming the module by powering it for 5 minutes on first use.
- Avoid exposure to organic solvents, coatings, medicines, oils, and high-concentration gases.
- Do not plug or unplug the sensor from the module, and do not modify any components on the PCB.
- Avoid excessive impact or vibration.
- Do not use the module in systems related to human safety.
- Do not operate the module in environments with strong air convection.
- Do not expose the module to high concentrations of organic gases for extended periods.9. Do not expose the module to high concentrations of organic gases for extended periods.
Was this article helpful?
