Hi I am trying to communicate SM5852 differential pressure sensor with arduino using i2c communication.This is the following code i got ( ref below). whenever i connecting with arduino board i am getting "I2C transaction error" in serial monitor.can anybody help me what is wrong here.
Besides the lack of proper capitalization and spacing between sentences?
There is an I2C scanner sketch that you should run BEFORE making any attempt to actually send data to/receive data from an I2C device. That sketch tells you the address of the device(s) that are connected to the I2C bus correctly. Have you run that sketch? Is the device KNOWN to be connected correctly?
Wire.requestFrom(SENSOR_I2C_ADDR,);
That can't be the code you are running on the Arduino, since that won't even compile.
After EVERY call to Wire.requestFrom(), you need to read the response BEFORE sending data to the device again and calling Wire.requestFrom() again.
2. You have poor understanding (or not at all!) on the working principles of the I2C Bus in respect of data communication. Therefore, I have tried to correct your codes based on the suggestions of the previous posts. Give a try on it and report the result. I can't test the sketch due to lack of the sensor.
#include "Wire.h"
//#include <stdint.h> why is this file included?
#define SENSOR_I2C_ADDR 95
#define SENSOR_REG_PRESSURE_LSB 128
#define SENSOR_REG_PRESSURE_MSB 129
uint16_t pressureRaw;
void setup()
{
Wire.begin();
Serial.begin(9600);
//checking that the device is present
Wire.beginTransmission(SENSOR_I2C_ADDR);
byte busStatus = Wire.endTransmission();
if (busStatus != 0x00)
{
Serial.println("I2C Bus Error/Device not Found!");
while (1); //wait for ever
}
}
void loop()
{
Wire.beginTransmission(SENSOR_I2C_ADDR);
Wire.write(SENSOR_REG_PRESSURE_LSB); //pointing the low-byte register of pressure sensor
Wire.endTransmission(); //transfer the above queued data and bring STOP condition on I2C Bus(false);
Wire.requestFrom(SENSOR_I2C_ADDR, 2);//request for 2-byte data byte(1));
//Wire.beginTransmission(SENSOR_I2C_ADDR);
//Wire.write(SENSOR_REG_PRESSURE_MSB);
//Wire.endTransmission(false);
//Wire.requestFrom(SENSOR_I2C_ADDR,);
// uint16_t pressureRaw; declare in the global area
// if (Wire.available() == 2)
// {
uint8_t lsb = Wire.read(); //6-bit
uint8_t msb = Wire.read();//6-bit uint16_t msb = Wire.read();
//fit <msb, lsb> into pressureRaw variable
for (int i = 0; i < 6; i++)
{
bitWrite(pressureRaw, i, bitRead(lsb, i));
}
for (int i = 6, j = 0; i < 12, j < 6; i++)
{
bitWrite(pressureRaw, i, bitRead(msb, j));
}
pressureRaw = pressureRaw & 0x0FFFF;//(msb << 6 | lsb) & 0x0FFF;
Serial.print("Raw pressure value: ");
Serial.println(pressureRaw);
//}
//else
//{
//while (Wire.available())
//Wire.read();
//Serial.println("I2C transaction error");
//}
delay(1000);
}