This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

TWI Issue With Peripheral Sensors

I'm building some code off of the twi_master_using_app_twi example which is available in the SDK (v11.0.0).

The two peripherals I'm working on are an accelerometer (LIS3DH) and gyroscope (L3GD20H).

I'm able to read and confirm the WHOAMI registers of both sensors, but that's about it. When I check the status register, both return all zeros. When I read the XYZ data off of each sensor the output data is just a constant value for all directions of motion with no sensitivity to movement.

I've been stuck at this point for a couple days now and I feel like I must be missing something simple/obvious. Any tips on where to start?

  • I might have been printing the status bits out wrong to my terminal. If I print it as a hex to Putty using app_trace_log I get 0xFF which is 1111 1111.

    For the gyroscope it looks like that means I'm getting new data: www.st.com/.../en.DM00036465.pdf

    But the values of XYZ rotation still aren't changing...

  • I also verified that the accelerometer works on an Arduino by using the simple example as shown here: learn.adafruit.com/.../wiring-and-test

    I used a similar Adafruit example for the gyroscope and it seems to work correctly there, too.

  • SInce you are able to read out the WHOAMI registers it seems safe to assume that you have configured the TWI and connected everything correctly. So my bet would be that there are some "start up" registers in your sensors that need to be set. For example, some sensors I have worked with start up in sleep mode by default, and one have to manually start the sampling of sensor data. Maybe this was handled in the background in the Arduino drivers?

  • Hi Martin,

    Thanks for the reply!

    It's my first time working with I2C/TWI but yea, that's what I assumed too with the WHOAMI registers. From what I understand, both sensors have start up registers and are by default in "low power" mode so I think the first thing I have to do, like you said, is set the sensor to "normal" mode.

    I'm trying to follow the init setup shown in lm75b.c. For the accelerometer (data sheet here click) I have:

        #define LIS3DH_INIT_TRANSFER_COUNT 1
        #define LIS3DH_CTRL_REG1 0x20
        #define LIS3DH_ADDR 0x18
    
        // Set default configuration of LIS3DH
        static uint8_t const default_config1[] = { LIS3DH_CTRL_REG1, 0x07 }; // Set CTRL_REG1 to 00000111 (0000 = 50 Hz, 0 = normal mode, 111 = Zen,Yen,Xen Enabled)
        
        app_twi_transfer_t_const lis3dh_init_transfers[LIS3DH_INIT_TRANSFER_COUNT] = 
        {
            APP_TWI_WRITE(LIS3DH_ADDR, default_config1, sizeof(default_config1, 0), 0);
        }
    

    This gets called in main.c to initialize the sensor with app_twi_perform. Does that seem correct?

    Also, when I read the status register (0x27 on the accelerometer) I get back a value of 0xFF but the default value is 0x00... so it makes me want to believe that the sensor is doing something.

Related