Can't read from LIS2DH12 accelerometer

Posted on
  • Espruino version: 2v03
    Using LIS2DH12 Module, I2C

    Hi Gordon,

    I'm struggling to read from the LIS2DH12 accelerometer. The error I get when using the hardware I2C is a bit cryptic (only info I found on this was an old forum post), but when I use software I2C I get a more descriptive error: WHO_AM_I check failed. I have confirmed that I've specified the correct pinouts when instantiating I2C... Was hoping you could give me more information on the errors?
    I'm just using the sample code, but here's what I have anyway:

    var i2c = new I2C();
    
    i2c.setup({sda: D25, scl: D26});
    var accel = require("LIS2DH12").connectI2C(i2c, {callback:function(xyz) {
       // callback whenever data is received
       console.log(xyz);
       // prints { "x": 3.90625, "y": -7.8125, "z": 984.375 }
    }});
    accel.setPowerMode(on?"low":"powerdown")­;
    
    

    When using hardware I2C, I get a write error:

    Connected
    --] >
    --]  ____                 _ 
    --] |  __|___ ___ ___ _ _|_|___ ___ 
    --] |  __|_ -| . |  _| | | |   | . |
    --] |____|___|  _|_| |___|_|_|_|___|
    --]          |_| espruino.com
    --]  2v03 (c) 2018 G.Williams
    --] 
    --] >
    --] Uncaught InternalError: I2C Write Error 33281
    --] 
    --]  at line 1 col 18
    --] b.writeTo(c,a|128);return b.readFrom(c,d)
    --]                  ^
    --] in function "r" called from line 1 col 96
    --] ...back;if(this.r(b.WHO_AM_I,1)[0]!=b.I_­AM_MASK)throw"LIS2DH12 ...
    --]                               ^
    --] in function "f" called from line 2 col 118
    --] ...on(a,d){b.writeTo(c,a,d)},a)
    --]                               ^
    --] in function "connectI2C" called from line 10 col 3
    --] }});
    --]   ^
    
    

    When using software I2C, I get a who_am_i error:

    Connected
    --] >
    --]  ____                 _ 
    --] |  __|___ ___ ___ _ _|_|___ ___ 
    --] |  __|_ -| . |  _| | | |   | . |
    --] |____|___|  _|_| |___|_|_|_|___|
    --]          |_| espruino.com
    --]  2v03 (c) 2018 G.Williams
    --] 
    --] >
    --] Uncaught LIS2DH12 WHO_AM_I check failed
    
    

    Thanks!

  • Just quickly looking at the datasheet:
    Do you set CS to high?
    Is your wiring good?
    Is the I2C address correct?

    For example, have a bunch of INA226 modules that don't use the default I2C address, and SDA and SCL markings on the breakout board are swapped...

  • Thanks for your help,
    1) CS is set to high according to my schematic
    2) Wiring is good as far as I can tell
    3) I2C address is correct as far as I can tell too, although I have tried many different ones and still get the WHO_AM_I check fail

  • Hi! Which Espruino board are you using? And do you have a link to the accelerometer breakout?

    I2C write error is usually because the chip isn't responding - so wrong address, or not wired up right.

    It may be that your breakout board doesn't have any pullup resistors on I2C so you could try adding some ~10k resistors to each of the 2 lines.

    Of course it's probably a combination of things, so after you added the pullups I'd try setting addr:0x18 as well in case it was the different address that was the issue.

  • Using Puck.js :)
    Yeah so I've polled every address on the I2C bus - the only thing that responds is the humidity sensor. From this I can assume that there must be something wrong with the wiring to the accelerometer.
    I'm going to look further into the wiring here and let you know. For those interested in checking whether your sensor(s) can be contacted through I2C:

    let i2c = new I2C();
    i2c.setup({sda: D25, scl: D26}); 
    for (i=0; i<128; i++) {
      i2c.writeTo(i, 0x0F);
      console.log("I2C addr is: " + i + ", response: " + i2c.readFrom(i, 1)[0]);
    }
    
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Can't read from LIS2DH12 accelerometer

Posted by Avatar for user94249 @user94249

Actions