Hey! I've take first steps with a pressure sensor i2c (HSCMRND030PD3A3 / 3.3V I2C Differential +-30 psi / Datasheet).I've read some topics and with this "help" I get some vaules from the sensor, now. But I have a problem with the length of the reading-bits - the values are differently from every reading.
Here is my actual test-code:
void setup() {
float Output, Pressure;
const long OutMax = 14745, OutMin = 1638;
const long PressureMax = 30, PressureMin = -30;
}
void loop() {
byte firstbyte;
byte secbyte;
Wire.beginTransmission(pressure_address);
Wire.write(1); // move your register pointer back to 0
Wire.endTransmission();
Wire.requestFrom(pressure_address, 2); // contents of your first two registers
while (Wire.available() < 2)
{}
firstbyte = Wire.read(); // Read press high byte
secbyte = Wire.read(); // Read press low byte
Output = BitShiftCombine(firstbyte , secbyte);
Pressure = (((Output - OutMin) * (PressureMax - PressureMin)) / (OutMax - OutMin)) + PressureMin;
Serial.println(Pressure); //Inches of H20
Serial.print("first byte ");
Serial.print(firstbyte, BIN);
Serial.print(" ");
Serial.print(firstbyte, HEX);
Serial.print(" ");
Serial.print(firstbyte, DEC);
Serial.print(" sec byte ");
Serial.print(secbyte, BIN);
Serial.print(" ");
Serial.print(secbyte, HEX);
Serial.print(" ");
Serial.print(secbyte, DEC);
Serial.println();
}
long BitShiftCombine( unsigned char x_high, unsigned char x_low) {
long combined;
combined = x_high; //send x_high to rightmost 8 bits
combined = combined << 8; //shift x_high over to leftmost 8 bits
combined |= x_low; //logical OR keeps x_high intact in combined and fills in rightmost 8 bits
return combined;
}
My output is (some values):
00:10:35.683 -> 150.05
00:10:35.683 -> first byte 10100000 A0 160 sec byte 1001 9 9
00:10:45.673 -> 150.06
00:10:45.673 -> first byte 10100000 A0 160 sec byte 1100 C 12
00:10:55.689 -> 150.06
00:10:55.689 -> first byte 10100000 A0 160 sec byte 1100 C 12
00:11:05.679 -> 0.06
00:11:05.679 -> first byte 100000 20 32 sec byte 1100 C 12
00:12:25.679 -> 150.05
00:12:25.679 -> first byte 10100000 A0 160 sec byte 1010 A 10
00:12:35.681 -> 0.05
00:12:35.681 -> first byte 100000 20 32 sec byte 1010 A 10
The values all ~150 or ~0 without any hose-connectors at sensor. Sometimes I've got 7 bit length values and sometimes 8 bit values and the second value is every time 4 bit length. If I take an zero in front of the 7 bit legth value it was an good explanation for me and if I "append" the 8 bit and 4 bit, I've got the 12 bit value. But why I have the difference from ~150 to ~0 - that isn't possible?!
I think that the 0 value could be possible, because the sensor are not connected and lies on my table in "normal air". And if I blow a little bit in the connector, the values goes up to ~1.37 and ~151.28.
Can anybody help me to understand my problem or explain my error?
Best regards and sorry about my real old school english ![]()