Skip to content

i2c communication of esp32 with Sensirion SFM3000-D air flow sensor. #4605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
csce5612 opened this issue Dec 4, 2020 · 2 comments
Closed
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@csce5612
Copy link

csce5612 commented Dec 4, 2020

This is the following code I have run to get the values from the airflow sensor, I got constant values (239.54). But I need to read the volume data from the airflow sensor for each second.
Please help me out in rectifying my code to get the results. I got this from the Arduino forum.
I am using espressif esp32-wrooom-32UE development kit
Here is the datasheet for esp32
[](https://www.espressif.com/sites/default/files/documentation/esp32-wroom-32e_esp32-wroom-32ue_datasheet_en.pdf)
datasheet for sensor
https://www.mouser.com/datasheet/2/682/Sensirion_Mass_Flow_Meters_SFM3400_Datasheet-1853359.pdf

after running i2c scanner for esp32 and sensirion airflow sensor
I got two addresses 1. 64 (0x40) 2. 118 (0x76)

#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 #64 (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(2500);
}

@stale
Copy link

stale bot commented Feb 2, 2021

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Feb 2, 2021
@stale
Copy link

stale bot commented Feb 18, 2021

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Feb 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

1 participant