Skip to content

Instantly share code, notes, and snippets.

@ksasao
Last active February 27, 2023 23:13
Show Gist options
  • Select an option

  • Save ksasao/6c4e969948d4355f692316376e5983a7 to your computer and use it in GitHub Desktop.

Select an option

Save ksasao/6c4e969948d4355f692316376e5983a7 to your computer and use it in GitHub Desktop.
Sensirion SCD40を M5Atom で動かすためのサンプルコード。SensirionI2CScd4x の 0.3.0 以降ではreadMeasurementの仕様が変わっているので temperature, humidity をfloatにして、数値変換式を削除するなどの修正が必要です。 https://github.com/Sensirion/arduino-i2c-scd4x/commit/741b9a443b1f5e66b12c4f3075b232667b4811f0
// Sensirion SCD40を M5Atom で動かすためのサンプルコード
// 2021/7/20 @ksasao
// * 公式のサンプルコードを一部改変
// Arduino の [ツール]>[ライブラリを管理]で、SDC40で検索し、
// Sensirion I2C SCD4x を追加してビルドしてください
// https://twitter.com/ksasao/status/1417455450929262598
/*
* Copyright (c) 2021, Sensirion AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Sensirion AG nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <M5Atom.h>
#include <SensirionI2CScd4x.h>
#define SDA_PIN 25
#define SCL_PIN 21
SensirionI2CScd4x scd4x;
void printUint16Hex(uint16_t value) {
Serial.print(value < 4096 ? "0" : "");
Serial.print(value < 256 ? "0" : "");
Serial.print(value < 16 ? "0" : "");
Serial.print(value, HEX);
}
void printSerialNumber(uint16_t serial0, uint16_t serial1, uint16_t serial2) {
Serial.print("Serial: 0x");
printUint16Hex(serial0);
printUint16Hex(serial1);
printUint16Hex(serial2);
Serial.println();
}
void setup() {
M5.begin(true, false, true);
Serial.println();
while (!Serial) {
delay(100);
}
Wire.begin(SDA_PIN, SCL_PIN);
uint16_t error;
char errorMessage[256];
scd4x.begin(Wire);
// stop potentially previously started measurement
error = scd4x.stopPeriodicMeasurement();
if (error) {
Serial.print("Error trying to execute stopPeriodicMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
uint16_t serial0;
uint16_t serial1;
uint16_t serial2;
error = scd4x.getSerialNumber(serial0, serial1, serial2);
if (error) {
Serial.print("Error trying to execute getSerialNumber(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
printSerialNumber(serial0, serial1, serial2);
}
// Start Measurement
error = scd4x.startPeriodicMeasurement();
if (error) {
Serial.print("Error trying to execute startPeriodicMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
}
void loop() {
uint16_t error;
char errorMessage[256];
delay(5000);
// Read Measurement
uint16_t co2;
uint16_t temperature;
uint16_t humidity;
error = scd4x.readMeasurement(co2, temperature, humidity);
if (error) {
Serial.print("Error trying to execute readMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else if (co2 == 0) {
Serial.println("Invalid sample detected, skipping.");
} else {
Serial.print("Co2:");
Serial.print(co2);
Serial.print("\t");
Serial.print("Temperature:");
Serial.print(temperature * 175.0 / 65536.0 - 45.0);
Serial.print("\t");
Serial.print("Humidity:");
Serial.println(humidity * 100.0 / 65536.0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment