Raspberry pi - reading I2C sensor works, but reading is wrong

66 views Asked by At

I'm trying to read a sensor with my Raspberry Pi. I have a wsen pdus sensor from Wurth electronics: https://www.we-online.com/en/components/products/WSEN-PDUS. This sensor can read the pressure and temp from the environment. When I execute I2Cdectect -y -a, I get the slave address (0x78) so communication through i2c should be possible.

The problem is when I want to read the values. My temperature is correct but my pressure 0.00kPA. Which should not be the case, it should be something like 49.76kPa in normal conditions (so no pressure on the sensor).

The sensor is quite simple, it doesn't have 'registers', if you try to read any address it will respond with 4 bytes. First two bytes are the pressure and the last two are the temp. https://www.we-online.com/components/products/manual/2513130810001_Manual-wsen-pdus-25131308xxx01_rev2.0.pdf

I have the following c code to test the sensor:

#include "platform.h"

#include <stdio.h>
#include <stdint.h>

// gcc -Wimplicit-function-declaration main.c platform.c -lwiringPi

#define WSEN_PDUS_ADDRESS (uint8_t)0x78
#define P_MIN_VAL_PDUS (uint16_t)3277
#define T_MIN_VAL_PDUS (uint16_t)8192
#define NUMBER_OF_BYTES 4

int main() {

        Debug_out("START APPLICATION WSEN PDUS\n", true);

        int8_t status = I2CInit(0x78);
         if(status != WE_SUCCESS) 
         {
                Debug_out("PLATFORM I2C ERROR SUCCESS\n", false);
         }
         else 
         {
                Debug_out("PLATFORM I2C INIT SUCCESS\n", true);
                
                uint8_t tmp[NUMBER_OF_BYTES] = { 0 };
                
                uint16_t rawPress = 0;
                uint16_t rawTemp = 0;
                uint16_t temp = 0;

                uint8_t result = ReadReg(WSEN_PDUS_ADDRESS, NUMBER_OF_BYTES, tmp);

                if(result == WE_FAIL)
                {
                        Debug_out("COULD NOT READ SENSOR!\n", false);
                }
                else
                {
                        Debug_out("READ SENSOR SUCCESS\n", true);

                        rawPress = (uint16_t)(tmp[0] << 8);
                        rawPress |= (uint16_t)tmp[1];

                        rawTemp = (uint16_t)(tmp[2] << 8);
                        rawTemp |= (uint16_t)tmp[3];

                        printf("tmp 0 :%u", tmp[0]);
                        printf("tmp 1 :%u", tmp[1]);
                        printf("raw press: %u\n", rawPress);

                        temp = rawTemp - T_MIN_VAL_PDUS;
                        float tempDegCP = (((float)temp * 4.272)/(1000));

                        temp = rawPress - P_MIN_VAL_PDUS;
                        float presskPap = (((float)temp * 7.63)/(1000000)) - 0.1;

                        printf("TEMP DEG C %.2f\n", tempDegCP);
                        printf("PRESS KPa %.2f\n", presskPap);
                }
         }

        return 0;
}

Part of the code comes from the manufacturer repo. Like platform header and source file as well the conversion method. Those can be found here:

platform lib: https://github.com/WurthElektronik/Sensors-SDK/tree/master/platform wsen_pdus: https://github.com/WurthElektronik/Sensors-SDK/blob/master/WSEN_PDUS_25131308XXX01/drivers/WSEN_PDUS_25131308XXX01.c

0

There are 0 answers