0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5StackCore2: Wire.h だけで、SFA30 の値を読む

Posted at

こちらのプログラムと同じことを、SensirionI2CSfa3x.h を使わないで行いました。
M5StackCore2: ホルムアルデヒドセンサー SFA30 を使う

プログラム

フォルダー構造

$ tree
.
├── i2c_sfa30_wire.ino
└── sfa30_read.ino
i2c_sfa30_wire.ino
// ---------------------------------------------------------------------
//	i2c_sfa30_wire.ino for M5StackCore2
//
//						Aug/29/2024
// ---------------------------------------------------------------------
#include <Wire.h>
#define	PROGRAM	"i2c_sfa30_wire.ino"
#define	VERSION	"2024-8-29 AM 10:26"

// SFA30
const int16_t SFA_ADDRESS = 0x5D;

int icount = 0;
// ---------------------------------------------------------------------
void setup() {
	Serial.begin(115200);
	delay(1000);
	Serial.println("*** start ***");

	// init I2C
	Wire.begin();

	// wait until sensor is ready
	delay(1000);
	Serial.println("*** setup aaa ***");
	
	// start SFA measurement in periodic mode, will update every 0.5 s
	Wire.beginTransmission(SFA_ADDRESS);
	Wire.write(0x00);
	Wire.write(0x06);
	Wire.endTransmission();

	Serial.println("*** setup bbb ***");
	delay(3000);
	Serial.println("*** setup ccc ***");
	delay(3000);
	Serial.println("*** setup ddd ***");
	delay(3000);
	// module is not outputing HCHO for the first 10 s after powering up
	Serial.println(PROGRAM);
	Serial.println(VERSION);
	Serial.println();
	delay(2000);
	Serial.println("*** setup end ***");
}

// ---------------------------------------------------------------------
void loop()
{
	float hcho;
	float temperature;
	float humidity;

	sfa30_read_proc(&hcho,&humidity,&temperature);

	char chx[80];
	sprintf(chx,"%d\t%.2f\t%.2f\t%.2f",icount,hcho,humidity,temperature);
	Serial.println(chx);

	delay(5000);
	icount++;
}

// ---------------------------------------------------------------------
sfa30_read.ino
// ---------------------------------------------------------------------
//	sfa30_read.ino for M5StackCore2
//
//						Aug/29/2024
// ---------------------------------------------------------------------
// [4]:
void sfa30_read_proc(float *ahcho,float *ahumidity,float *atemperature)
{
	uint8_t data[9], counter;

	*ahcho = 0.0;
	*ahumidity = 0.0;
	*atemperature = 0.0;

	// send read data command
	Wire.beginTransmission(SFA_ADDRESS);
	Wire.write(0x03);
	Wire.write(0x27);
	Wire.endTransmission();

	//wait time before reading for the values should be more than 2ms
	delay(10);
	
	// read measurement data: 
	// 2 bytes formaldehyde, 1 byte CRC, scale factor 5
	// 2 bytes RH, 1 byte CRC, scale factor 100
	// 2 bytes T, 1 byte CRC, scale factor 200
	// stop reading after 9 bytes (not used)
	Wire.requestFrom(SFA_ADDRESS, 9);
	counter = 0;
	while (Wire.available()) {
		data[counter++] = Wire.read();
	}

	if (counter == 9)
		{
	
	// floating point conversion according to datasheet
	*ahcho = (float)((int16_t)data[0] << 8 | data[1])/5;
	// convert RH in %
	*ahumidity = (float)((int16_t)data[3] << 8 | data[4])/100;
	// convert T in degC
	*atemperature = (float)((int16_t)data[6] << 8 | data[7])/200;
		}
	else
		{
		Serial.print("*** error ***\t");
		Serial.print("counter = ");
		Serial.println(counter);
		}
}

// ---------------------------------------------------------------------

Arduino IDE

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?