forked from Sensirion/arduino-i2c-scd4x
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from CDFER/1.2.0
1.2.0
- Loading branch information
Showing
9 changed files
with
469 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"datasheet", | ||
"EEPROM", | ||
"LOGI", | ||
"selftest", | ||
"Sensirion" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
SCD4X Measurement Task Example | ||
This example demonstrates the usage of the SCD4X CO2 sensor library with FreeRTOS on an Arduino board, | ||
specifically designed for the ESP32's version of Arduino with FreeRTOS. | ||
Instructions: | ||
- Install the SCD4X library in your Arduino IDE or add it to platformio.ini | ||
- Connect the SCD4X CO2 sensor to the ESP32: | ||
- SDA: GPIO 21 | ||
- SCL: GPIO 22 | ||
- VCC: 3V3 (3.3V) | ||
- Optionally, you can add external 4.7k pull-up resistors on the SDA and SCL lines. | ||
- Flash code | ||
- Open the Serial Monitor in the Arduino IDE to view the measurement data. | ||
Note: This example is specifically designed for the ESP32's version of Arduino with FreeRTOS. | ||
*/ | ||
|
||
#include <scd4x.h> | ||
|
||
SCD4X co2; | ||
double co2Value = 0, temperature = 0, humidity = 0; | ||
TaskHandle_t measurementTask; | ||
|
||
void measurementTaskFunction(void* parameter) { | ||
Wire.begin(); // SDA: GPIO 21, SCL: GPIO 22 by default on the ESP32 | ||
co2.begin(Wire); | ||
co2.startPeriodicMeasurement(); | ||
|
||
while (true) { | ||
if (co2.isDataReady()) { | ||
if (co2.readMeasurement(co2Value, temperature, humidity) == 0) { | ||
Serial.printf("CO2: %.0f ppm, Temperature: %.1f °C, Humidity: %.0f %%RH\n", co2Value, temperature, humidity); | ||
vTaskDelay(pdMS_TO_TICKS(4750)); // New data available after approximately 5 seconds | ||
} | ||
} | ||
vTaskDelay(pdMS_TO_TICKS(50)); // Check every 50ms | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
while (!Serial) { | ||
// Wait for serial port to connect | ||
delay(100); | ||
} | ||
|
||
xTaskCreatePinnedToCore(measurementTaskFunction, "MeasurementTask", 2048, NULL, 1, &measurementTask, 0); | ||
} | ||
|
||
void loop() { | ||
// Your other code here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <SD.h> | ||
#include <Wire.h> | ||
#include <scd4x.h> | ||
|
||
SCD4X co2; | ||
|
||
const int chipSelectPin = 10; // Chip select pin for SD card module | ||
const String logFileName = "co2_log.txt"; // Name of the log file | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
while (!Serial) { | ||
// Wait for serial port to connect | ||
delay(100); | ||
} | ||
|
||
// Initialize SD card | ||
if (!SD.begin(chipSelectPin)) { | ||
Serial.println("SD card initialization failed."); | ||
return; | ||
} | ||
|
||
// Initialize the I2C communication | ||
Wire.begin(); | ||
|
||
// Initialize the SCD4x library | ||
if (!co2.begin()) { | ||
Serial.println("Failed to initialize SCD4X sensor."); | ||
return; | ||
} | ||
|
||
// Check if the sensor is connected | ||
if (!co2.isConnected()) { | ||
Serial.println("Sensor not connected. Please check the wiring."); | ||
return; | ||
} | ||
|
||
// Check if auto-calibration is enabled | ||
if (co2.getCalibrationMode()) { | ||
// Disable auto-calibration | ||
co2.setCalibrationMode(false); | ||
|
||
// Save the settings to EEPROM | ||
co2.saveSettings(); | ||
} | ||
|
||
// Start periodic measurement after updating settings | ||
co2.startPeriodicMeasurement(); | ||
|
||
// Create or append to the log file | ||
File logFile = SD.open(logFileName, FILE_WRITE); | ||
if (logFile) { | ||
logFile.println("CO2 (ppm), Temperature (°C), Humidity (%RH)"); | ||
logFile.close(); | ||
Serial.println("Data logging started. Press reset to start a new log."); | ||
} else { | ||
Serial.println("Failed to open log file."); | ||
return; | ||
} | ||
} | ||
|
||
void loop() { | ||
double co2Value, temperature, humidity; | ||
|
||
// Read measurement data from the sensor | ||
uint8_t errorCode = co2.readMeasurement(co2Value, temperature, humidity); | ||
|
||
// Check for errors | ||
if (errorCode == 0) { | ||
// Format the measurements into a string | ||
char measurementString[50]; | ||
snprintf(measurementString, sizeof(measurementString), "%.0f, %.1f, %.0f", co2Value, temperature, humidity); | ||
|
||
// Open the log file in append mode | ||
File logFile = SD.open(logFileName, FILE_WRITE | FILE_APPEND); | ||
if (logFile) { | ||
// Write the measurement data to the log file | ||
logFile.println(measurementString); | ||
logFile.close(); | ||
Serial.println("Data logged: " + String(measurementString)); | ||
} else { | ||
Serial.println("Failed to open log file."); | ||
} | ||
|
||
} else { | ||
// Convert the error code to text | ||
const char* errorText = co2.getErrorText(errorCode); | ||
|
||
// Print the error message | ||
Serial.print("Error reading measurement: "); | ||
Serial.println(errorText); | ||
} | ||
|
||
// Delay for new measurement to be taken | ||
delay(5000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
SCD4x CO2 Sensor Example | ||
This example demonstrates the usage of the SCD4x CO2 sensor library on an Arduino board. | ||
Instructions: | ||
- Install the SCD4x library in your Arduino IDE. | ||
- Connect the SCD4X CO2 sensor to the following pins on your Arduino board: | ||
- SDA: Default SDA pin | ||
- SCL: Default SCL pin | ||
- VCC: 3.3V or 5V (make sure you match this with your Arduino VCC voltage or otherwise you can damage it) | ||
- Optionally, you can add external 4.7k pull-up resistors on the SDA and SCL lines. | ||
- Flash code | ||
- Open the Serial Monitor in the Arduino IDE to view the measurement data and any error messages. | ||
Default I2C pins for some common Arduino boards: | ||
- Arduino Uno, Nano: SDA - A4, SCL - A5 (VCC: 5V) | ||
- Arduino Mega: SDA - 20, SCL - 21 (VCC: 5V) | ||
- Arduino Leonardo: SDA - 2, SCL - 3 (VCC: 5V) | ||
- Arduino Due: SDA - 20, SCL - 21 (VCC: 3.3V) | ||
- ESP32: SDA - GPIO 21, SCL - GPIO 22 (VCC: 3.3V) | ||
- ESP8266: SDA - GPIO 4, SCL - GPIO 5 (VCC: 3.3V) | ||
- RP2040: SDA - GP5 (pin 4), SCL - GP4 (pin 3) (VCC: 3.3V) | ||
Note: The default SDA and SCL pins may vary depending on the specific board or variant. Please refer to the board documentation for the correct pin mappings. | ||
4.7k Pull-up Resistors: | ||
- I2C communication requires pull-up resistors on the SDA and SCL lines to ensure proper signal levels. | ||
- Some Arduino boards already have built-in pull-up resistors for the I2C pins, so external resistors may not be necessary. | ||
- However, if you encounter communication issues or have long wire lengths, adding external 4.7k pull-up resistors between the SDA/SCL lines and VCC can help improve signal stability. | ||
*/ | ||
|
||
#include <Wire.h> | ||
#include <scd4x.h> | ||
|
||
SCD4X co2; | ||
|
||
void setup() { | ||
// Initialize the I2C communication | ||
Wire.begin(); | ||
|
||
// Initialize the SCD4x library | ||
co2.begin(); | ||
|
||
// Check if the sensor is connected | ||
if (!co2.isConnected()) { | ||
while (true) { | ||
Serial.println("Sensor not connected. Please check the wiring."); | ||
delay(1000); | ||
} | ||
} | ||
|
||
// Check if auto-calibration is enabled | ||
if (co2.getCalibrationMode()) { | ||
// Disable auto-calibration | ||
co2.setCalibrationMode(false); | ||
|
||
// Save the settings to EEPROM | ||
co2.saveSettings(); | ||
} | ||
|
||
// Start periodic measurement after updating settings | ||
co2.startPeriodicMeasurement(); | ||
|
||
// Wait for the sensor to warm up and take the first reading | ||
delay(5000); | ||
} | ||
|
||
void loop() { | ||
double co2Value, temperature, humidity; | ||
|
||
// Read measurement data from the sensor | ||
uint8_t errorCode = co2.readMeasurement(co2Value, temperature, humidity); | ||
|
||
// Check for errors | ||
if (errorCode == 0) { | ||
// Format the measurements into a string | ||
char measurementString[100]; | ||
sprintf(measurementString, "CO2: %.0f ppm, Temperature: %.1f °C, Humidity: %.0f %%RH", co2Value, temperature, humidity); | ||
|
||
// Print the measurements | ||
Serial.println(measurementString); | ||
} else { | ||
// Convert the error code to text | ||
const char* errorText = co2.getErrorText(errorCode); | ||
|
||
// Print the error message | ||
Serial.print("Error reading measurement: "); | ||
Serial.println(errorText); | ||
} | ||
|
||
// Delay for new measurement to be taken | ||
delay(5000); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
{ | ||
"name": "scd4x-CO2", | ||
"version": "1.1.0", | ||
"description": "A library to interface esp chips with the SCD4x CO2 sensors in the Arduino (c++) Framework.", | ||
"keywords": "co2, scd40, scd41, scd4x, i2c", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/CDFER/scd4x-CO2.git" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "scd4x-CO2", | ||
"version": "1.2.0", | ||
"description": "An Arduino library for interfacing ESP chips with SCD4x CO2 sensors using the I2C protocol.", | ||
"keywords": [ | ||
"co2", | ||
"scd40", | ||
"scd41", | ||
"scd4x", | ||
"i2c" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/CDFER/scd4x-CO2.git" | ||
}, | ||
"author": { | ||
"name": "Chris Dirks", | ||
"url": "https://keastudios.co.nz/about.htm", | ||
"maintainer": true | ||
} | ||
], | ||
"license": "MIT", | ||
"homepage": "https://github.com/CDFER/scd4x-CO2", | ||
"frameworks": "arduino", | ||
"platforms": "*" | ||
}, | ||
"license": "Other", | ||
"homepage": "https://github.com/CDFER/scd4x-CO2", | ||
"frameworks": "arduino", | ||
"platforms": "*" | ||
} |
Oops, something went wrong.