Wire library I2C with DPS310

I have a hard time with Wire library

What my sensor datasheet says about reading a register value is sending the following

(S)SLAVE address(WA)>Register address(A)>(S)SLAVE address(RA)>Register data(A)

Where S= start W= write A= Ack R=read

So, I tried the following The default address in the datasheet is 0x77

#include<Wire.h>

void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
byte data;
Wire.beginTransmission(59);
Wire.write(byte(0x0D));

Wire.beginTransmission(59);
Wire.requestFrom(59,8);
Serial.print(data);
data=Wire.read();
Serial.print(data);

Wire.endTransmission();
delay(1000);

}

What’s wrong I am doing that I always get 255 in data?? I do not have Oscilloscope to check what each bit is doing, Unfortunately!!

not sure if device configuation is needed prior to requesting the data but this is how I would have coded to check the pressure raw data:

#include <Wire.h>

void setup() {
  Serial.begin(115200);          // start serial communication

  //you may need to add in the sensor setup here...

}

uint32_t reading = 0;

void loop() {

  //Read. Pressure Data registers. 3 bytes: 2's complement pressure measurement value.
  Wire.beginTransmission(0x77); //transmit to sensor (using default address)
  Wire.write(byte(0x00));      // sets register pointer to PRS_B2 register (0x00)
  Wire.endTransmission();      // stop transmitting

  //request reading from sensor
  Wire.requestFrom(0x77, 3);    // request 3 bytes from slave device 0x77

  //receive reading from sensor
  if (3 <= Wire.available()) { // if three bytes were received
    reading = Wire.read();  // receive high byte (PRS_B2)
    reading = reading << 8;    // shift byte by 8 bits
    reading |= Wire.read(); // receive middle byte (PRS_B1)
    reading = reading << 8;    // shift byte by 8 bits
    reading |= Wire.read();  // receive low byte (PRS_B0)
    Serial.println("PRS_Bn (2's Complement): ");
    Serial.println(reading, HEX);   // print the 'reading' in hexadecimal
  }

  delay(250);                  // wait a bit since people have to read the output :)
}

btw, there’s actually a library already made for the DPS310:

https://github.com/Infineon/DPS310-Pressure-Sensor

So, I tried the following The default address in the datasheet is 0x77

Wire.beginTransmission(59);

Please explain this discrepancy in your post.

@sherzaad

yes, now it seems working but not sure about that "I am fetching correct data"!!

What I was missing was that endtransmission in between. Also, request from is necessary...only read() don't work... As you state no need for configuration in setup...

It is difficult to understand I2C in terms of functions better could be the timing diagram of each function!!

@pylon : it is because I thought it is 7 bit addressing mode library which means after shifting one bit to right it becomes 00111011 which is 59!!

it is because I thought it is 7 bit addressing mode library which means after shifting one bit to right it becomes 00111011 which is 59!!

I2C addresses are always 7bit (by the standard). Seldom you see datasheets where they show the first byte of the communication as the "address" but the usually explain that they provide the complete first byte and they either write about a read and a write address or the provide a byte where the lowest bit is not set.

What I was missing was that endtransmission in between.

In the Wire library a Wire.beginTransmission() starts a new write request but nothing is sent until you call the Wire.endTransmission(). A Wire.requestFrom() starts a read request, the method doesn't return until all the requested bytes are completely read into the internal buffer.

@pylon

I don’t get your first paragraph explanation of addressing… I2C is 8 bit address in this case as 0x77 is eight-bit. you can also see in the code…

I2C is 8 bit address in this case as 0x77 is eight-bit. you can also see in the code.

No, I2C is always using a 7bit address. As I explained, only some exotic datasheets provide the first byte instead of the I2C address. If you read I2C address in a datasheet it's always the 7bit address, so you don't have to shift anything.