• Getting the I2C Interface Working With the Chip

    What is I2C?

    https://en.wikipedia.org/wiki/I%C2%B2C
    So we have to connect the ground, power, SCL and SDA from the PICO to the chip.
    The SCL and SDA lines require a pull-up resistor.
    I’m using a breakout board from Sparkfun and the pullup resistors are on the board.
    https://www.sparkfun.com/products/13284
    As for the pins to use on the PICO
    https://www.espruino.com/I2C
    look at the PICO picture
    http://www.espruino.com/Pico
    Looking at the I2C section of the Espruino Reference Page
    https://www.espruino.com/Reference
    We find the I2C.find command.

    ////////////////////////////////////
    //Configuration
    //The I2C pins that the LSM9D01 is connected to
    //PICO I2C pins
    //IC1  sda=B7  scl=B6
    //IC1  sda=B9  scl=B8  shim pins
    //IC3  sda=B4  scl=A8
    //IC2  sda=B3  scl=B10
    
    console.log(I2C.find(B7));
    console.log(I2C.find(B6));
    
    console.log(I2C.find(B9));
    console.log(I2C.find(B8));
    
    console.log(I2C.find(B4));
    console.log(I2C.find(A8));
    
    console.log(I2C.find(B3));
    console.log(I2C.find(B10));
    
    console.log(I2C.find(B1));
    
    

    Which produces:

    1v92 Copyright 2016 G.Williams
    >I2C {  }
    I2C {  }
    I2C {  }
    I2C {  }
    I2C {  }
    I2C {  }
    I2C {  }
    I2C {  }
    undefined
    =undefined
    

    I2C devices have a 7 bit bus address. The LSM9DS1 has two, one for the magnetometer and one for the gyroscope and accelerometer. Jumpers on the Sparkfun board can be used to change the addresses.
    There is a register labeled WHO_AM_I for the Gyro-Accel, and one for the magnetometer.
    Reading these registers and checking the returned value validates that the I2C interface is working.
    The following code sets up an LSM9DS1 object, initializes the I2C interface and reads the WHO_AM_I_XG register on the LSM9DS1 chip connected to I2C3.setup({ scl :A8, sda: B4} ).

    //Test I2C connection
    var Regs={
     WHO_AM_I_XG: 0x0F,
    };
    
    ////////////////////////////////////////­//////
    /** the LSM9DS1 object*/
    function LSM9DS1(i2c,xgAddress,mAddress) {
      this.i2c = i2c;
      this.xgAddress= xgAddress;
      this.mAddress= mAddress;
    }
    
    /** 'run() initializes the LSM9DS1' */
    LSM9DS1.prototype.run=function(){
    var xgTest = this.xgReadByte(Regs.WHO_AM_I_XG);
    console.log("xgTest= "+xgTest);
    };//end run
    
    /** 'xgReadByte(subAddress) read a byte from the gyro/accelerometer at subAddress'*/
    LSM9DS1.prototype.xgReadByte=function(su­bAddress){
     var x=this.xgAddress;
     var data=Uint8Array(1);
     this.i2c.writeTo(x, subAddress);
     data=this.i2c.readFrom(x, 1);
     return data[0];
    };//end xgReadByte
    
    ////////////////////////////////////
    //Configuration
    //The I2C pins that the LSM9D01 is connected to
    //PICO I2C pins
    //IC1  sda=B7  scl=B6
    //IC1  sda=B9  scl=B8  shim pins
    //IC3  sda=B4  scl=A8
    //IC2  sda=B3  scl=B10
    var W;
    function start(){
    //  console.log("start");
     I2C3.setup({ scl :A8, sda: B4} );
    //console.log(I2C3);
    var xgAddress= 0x6B;// Would be 0x1C if SDO_M is LOW
    var mAddress= 0x1e;// Would be 0x6A if SDO_AG is LOW 
    //W=require("slimLSM9DS1").connect(I2C3,­xgAddress,mAddress);
      W=new LSM9DS1(I2C3,xgAddress,mAddress);
    W.run();//Get it started
    }//end start
    
    //Wait for program to finish loading
    setTimeout(function () {
     start();
    }, 1000);
    

    Which outputs:

    1v92 Copyright 2016 G.Williams
    >
    =undefined
    xgTest= 104
    >
    
About