In this post, I explain how to use a CO₂ sensor – specifically the Sensirion SCD30 – with an Arduino or an ESP32. This sensor is particularly versatile because it can measure not only CO₂ but also temperature and humidity.
Why the Sensirion SCD30?
The SCD30 from :contentReference[oaicite:0]{index=0} is a precise and reliable sensor that communicates via an I²C interface. This makes it relatively easy to connect to most microcontrollers.
You just need to identify the I²C pins of your microcontroller.
For the ESP32 Dev Kit, which I use in this example, these are typically:
- Pin 21 → SDA
- Pin 22 → SCL
Additionally, the sensor requires power supply:
- 3V or 5V → VCC
- GND → microcontroller ground
Required Components
The following components are needed for this project:
- Sensirion SCD30 CO₂ sensor
- Microcontroller (e.g., Arduino or ESP32)
- Jumper wires
- USB cable for programming and power supply
The Code
The following code works for Arduino boards from :contentReference[oaicite:1]{index=1} as well as ESP32 boards from :contentReference[oaicite:2]{index=2}.
First, include the necessary libraries:
#include
#include "SparkFun_SCD30_Arduino_Library.h"
SCD30 airSensor;
setup()
In setup(), the I²C communication, serial interface, and the sensor are initialized:
void setup()
{
Wire.begin(); // Start I2C communication
Serial.begin(9600); // Start serial communication
airSensor.begin(); // Initialize the sensor
}
loop()
The actual measurement work takes place in the loop() function:
void loop()
{
if (airSensor.dataAvailable())
{
Serial.print("CO2 (ppm): ");
Serial.print(airSensor.getCO2());
Serial.print(", Temp (C): ");
Serial.print(airSensor.getTemperature(), 1);
Serial.print(", Humidity (%): ");
Serial.print(airSensor.getHumidity(), 1);
Serial.println();
}
else
{
// Serial.println("Waiting for data..."); // Optional
}
delay(500); // Short pause between measurements
}
Complete Code
#include
#include "SparkFun_SCD30_Arduino_Library.h"
SCD30 airSensor;
void setup()
{
Wire.begin(); // Start I2C communication
Serial.begin(9600); // Serial communication at 9600 baud
airSensor.begin(); // Initialize the SCD30 sensor
}
void loop()
{
if (airSensor.dataAvailable()) // Check if new sensor data is available
{
Serial.print("CO2 (ppm): ");
Serial.print(airSensor.getCO2());
Serial.print(", Temp (C): ");
Serial.print(airSensor.getTemperature(), 1);
Serial.print(", Humidity (%): ");
Serial.print(airSensor.getHumidity(), 1);
Serial.println();
}
else
{
// Optional: output if no data is available
// Serial.println("Waiting for data...");
}
delay(500); // 500 ms pause until the next measurement
}
Conclusion
With this simple setup, you can transform your Arduino or ESP32 into a compact environmental monitoring station. The Sensirion SCD30 is excellent for projects requiring reliable measurements of CO₂, temperature, and humidity.