This code helped me a lot, but it doesn't read correctly the data because we skipped info. from the manufacturers Datasheet. This one should work.
#include <Wire.h>
int offset = 32000; // Offset for the sensor
float scale = 140.0; // Scale factor for Air and N2 is 140.0, O2 is 142.8
void setup()
{
Wire.begin();
Serial.begin(9600);
int a = 0;
int b = 0;
int c = 0;
/*Wire.requestFrom(0x40, 3); //
a = Wire.read(); // first received byte stored here
b = Wire.read(); // second received byte stored here
c = Wire.read(); // third received byte stored here
Wire.endTransmission();
delay(5);
Wire.requestFrom(0x40, 3); //
a = Wire.read(); // first received byte stored here
b = Wire.read(); // second received byte stored here
c = Wire.read(); // third received byte stored here
Wire.endTransmission();
delay(5);*/
}
uint8_t crc8(const uint8_t data, uint8_t crc) {
crc ^= data;
for ( uint8_t i = 8; i; --i ) {
crc = ( crc & 0x80 )
? (crc << 1) ^ 0x31
: (crc << 1);
}
return crc;
}
void loop() {
delay(500);
Wire.beginTransmission(byte(0x40)); // transmit to device #064 (0x40)
Wire.write(byte(0x10)); //
Wire.write(byte(0x00)); //
Wire.endTransmission();
Wire.requestFrom(0x40, 3); // read 3 bytes from device with address 0x40
while (Wire.available()) { // slave may send less than requested
uint16_t a = Wire.read(); // first received byte stored here. The variable "uint16_t" can hold 2 bytes, this will be relevant later
uint8_t b = Wire.read(); // second received byte stored here
uint8_t crc = Wire.read(); // crc value stored here
uint8_t mycrc = 0xFF; // initialize crc variable
mycrc = crc8(a, mycrc); // let first byte through CRC calculation
mycrc = crc8(b, mycrc); // and the second byte too
if (mycrc != crc) { // check if the calculated and the received CRC byte matches
Serial.println("Error: wrong CRC");
}
Serial.println('p');
Serial.println(a);
Serial.println(b);
Serial.println(crc);
Serial.println('h');
a = (a << 8) | b; // combine the two received bytes to a 16bit integer value
// a >>= 2; // remove the two least significant bits
int Flow = (a - offset) / scale;
//Serial.println(a); // print the raw data from the sensor to the serial interface
Serial.println(Flow); // print the calculated flow to the serial interface
}
}
Forse:
Ok. Just to close this so others reading this thread can use the code.
I enden up not reporting the CRC check as I cant get it to work. The readings from the sensor are fine and have been verified with another flowmeter. In the code is an offset so flows in one way are reported as positive numbers and the other way is negative. There is also a scale factor which is dependent on the gas being measured.
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
int a = 0;
int b = 0;
int c = 0;
delay(1000);
Wire.beginTransmission(byte(0x40)); // transmit to device #064 (0x40)
Wire.write(byte(0x10)); //
Wire.write(byte(0x00)); //
Wire.endTransmission();
delay(5);
Wire.requestFrom(0x40, 3); //
a = Wire.read(); // first received byte stored here
b = Wire.read(); // second received byte stored here
c = Wire.read(); // third received byte stored here
Wire.endTransmission();
Serial.print(a);
Serial.print(b);
Serial.println(c);
delay(5);
Wire.requestFrom(0x40, 3); //
a = Wire.read(); // first received byte stored here
b = Wire.read(); // second received byte stored here
c = Wire.read(); // third received byte stored here
Wire.endTransmission();
Serial.print(a);
Serial.print(b);
Serial.println(c);
delay(5);
}
uint8_t crc8(const uint8_t data, uint8_t crc) {
crc ^= data;
for ( uint8_t i = 8; i; --i ) {
crc = ( crc & 0x80 )
? (crc << 1) ^ 0x31
: (crc << 1);
}
return crc;
}
void loop() {
int offset = 32000; // Offset for the sensor
float scale = 140.0; // Scale factor for Air and N2 is 140.0, O2 is 142.8
Wire.requestFrom(0x40, 3); // read 3 bytes from device with address 0x40
uint16_t a = Wire.read(); // first received byte stored here. The variable "uint16_t" can hold 2 bytes, this will be relevant later
uint8_t b = Wire.read(); // second received byte stored here
uint8_t crc = Wire.read(); // crc value stored here
uint8_t mycrc = 0xFF; // initialize crc variable
mycrc = crc8(a, mycrc); // let first byte through CRC calculation
mycrc = crc8(b, mycrc); // and the second byte too
if (mycrc != crc) { // check if the calculated and the received CRC byte matches
//Serial.println("Error: wrong CRC");
}
a = (a << 8) | b; // combine the two received bytes to a 16bit integer value
// a >>= 2; // remove the two least significant bits
float Flow = ((float)a - offset) / scale;
//Serial.println(a); // print the raw data from the sensor to the serial interface
Serial.println(Flow); // print the calculated flow to the serial interface
delay(25);
}