Skip to main content
Graduate
June 27, 2024
Question

Strange values when reading bytes from my I2C sensor [STM32L0]

  • June 27, 2024
  • 2 replies
  • 2502 views

I have a system of sensors connected to an STM32 chip, all working via I2C and 3.3V.
I've created interface libraries for my sensors, specifically the temperature and accelerometer ones. However, when I read the values measured by the sensors, I came across a strange result.

Here's a printout of the serial communication:

Starting...
Scanning I2C bus:
Device found at 0x18
Device found at 0x1F
Device found at 0x50
Device found at 0x51
Device found at 0x7C
I2C scan completed.
Ready KX122
Ready MCP9808
Error in MAX17048 connection
Ready FRAM
Ready!
Next Memory Adds: 0
Starting Temperature Reading:
Temp:
Temp: 1077936128
Starting Battery Reading:
Failed to read voltage
Failed to read SOC
Bat: 0
RMS Accel: -1067319296, 0, 1081114624
Starting Vibration Reading:

 

As you can see, the acceleration and temperature values are all wrong. They should be double values.

Temp: 1077936128

RMS Accel: -1067319296, 0, 1081114626

The library codes, my main.c file and its header files are in Firmware Tools.rar.

    This topic has been closed for replies.

    2 replies

    Super User
    June 27, 2024

    I'm not going to download archives and plow through them.  You can post relevant snippets of code here using the code tags ("</>").  I am presuming your are doing something like:

    printf( "Temp: %f\n", temperature );

    By default the built-in printf library does not support floating point.  There are compiler/linker flags to enable that.

    Graduate II
    June 27, 2024

    Not sure there's a huge amount of interest for working over broken code. If you want people to browse I'd suppose GitHub would be better and platform agnostic. ie at least usable on a phone..

    The thing with I2C is to use the register auto-increment to read a related block of registers. It also tends to be more atomic on the peripheral side, as it can see a large linear transaction, and knows when it ends, which might be particularly consequential when reading multi-byte values from a continuous device like a RTC, gyro or accelerometer.

    Not all I2C device play well together, you might need to use multiple buses, and pay attention to the topology, pull-up values, and current sinking / low power operation of given slave devices.

    Think also how you might attach a Logic Analyzer to monitor troublesome traffic, and generate GPIO triggering when range checking or error flagging senses an issue STM32 side.

    Graduate
    June 27, 2024

    So, the problem is that these codes are in a private GitHub repository, and as I couldn't use the tags I preferred to put everything in a folder so that the question doesn't lack information. I apologize for that.
    As for compatibility, I don't think that's the problem. I've tried removing the accelerometer and testing only temperature, and the other way around too.

    I think there really is a problem with my libraries. This is the first time I've programmed a library for an i2c device from scratch.

    Super User
    June 27, 2024

    So post the relevant sections of your library code here.  Perhaps the code that reads from the temperature sensor and manipulates the data to calculate the floating point value.  Include the temperature sensor mfg and part number for those so inclined to fetch the data sheet (for those like me whose IT departments restrict downloads).

    For debugging purposes, have you looked at the raw data that you read from the sensor? Does it make sense?  Post that here as well.