Hi all, I could use a hand with the following sketch. I'm trying to get the pressure out of an ELVH-100Q-HQNH-C-N2A5. The sketch is a modification from one I found on here. I was hoping to get some clarification on what I'm doing wrong? I cant seem to get the correct temp or pressure readings and I don't really follow the data sheet, I think the shifting is wrong? Thanks for the help!
The basic problem is that your code is hard to check for bugs.
In my opinion, the code should be clear and should show what it does and everyone should be able to check that and it should stay out of trouble.
To make clear what the code is doing, it is best to stay close to the datasheet in my opinion.
If the datasheet uses a 32 bit integer, then we will do the same.
The Wire.read() returns -1 if there was no data, to avoid that, I check if there is data.
I'm always scared to shift a 8-bit variable too much and all the bits are shifted away, so I will only shift with the 32-bit variable.
uint32_t data = 0;
int n = Wire.requestFrom(SENSOR_ADDRESS, 4); // request 4 bytes
if( n == 4) // check if 4 bytes are received
{
for( int i=0; i<4; i++)
{
data <<= 8;
data |= Wire.read();
}
Serial.print( "data = 0x");
Serial.println( data, HEX);
// According to the datasheet, the pressure is from bit D16 up to D29 (14 bits)
// Starting at bit D16, means shifting it 16 times to the right.
unsigned int pressure = (data >> 16) & 0x3FFF;
// According to the datasheet, the temperature is from bit D5 up to D15 (11 bits)
unsigned int temperature = (data >> 5) & 0x07FF;
}
else
{
Serial.println( "Where is it ?");
}
I compared your shifting an my shifting in the Wokwi simulator on a Arduino Uno: Bits Are Shifting Everywhere - Wokwi Arduino and ESP32 Simulator
The start button is in the upper-middle of the screen.
There is a difference, my pressure (10922) is not the same as your pressure (682) and also the temperature is not the same.
have a read of how-to-get-the-best-out-of-this-forum
in particular what arduino are you using?
upload a schematic showing the wiring?
the sensor appears to use I2C - have you run the I2C scanner to check the sensor address and it is responding to I2C communications