• If gain is set to 4096 (accoriding to Table 3 on page 13 of TI ADS1115 datasheet:

    • measurement range is from -4.096V..4.095999V
    • values are (always) -32768..0..32767
      (1 value unit equals to 0.000125V)

    Therefore, for 2.048V you should get a value of 16384, which is at half in the positive half of the value range... 0..16384..32767

    6.144/32768 = 0.0001875 - about 50% off from the above calculated 4.096 related 0.000125 value, therefore, I 'assume', the gain setting is not working correctly... it took 6.144 because:

    I did look at the 'last' 'module' code and my language concious did not 'like' property names of the GAINS object that are pure numbers: lines 57..62 - even though it obviously works partially, because you do not mention the built-in error (...! in GAINS) has not caugth on). BUT: ... | 4096 is | 2 ^ 12 or | 0b0001'0000"0000'0000 or | 0x1000 - just sets bit 12 (counting from 0) outside of the PGA_MASK... not what you want. Obviously, it did not mess up, at least not totally. Since it does not throw an error it clears all (gain mattering) bit(s) - see PGA_MASK - and that ets the gain to 2/3: -6.144..0..+6.144 volts, which explains the 6.144 / 32768 term that worked for you. Make the following changes, and things should just work as expected:

    Lines 56..63:

    var GAINS = {
      G6144 : CONFIG.PGA_6_144V,  // +/-6.144V range = Gain 2/3
      G4096 : CONFIG.PGA_4_096V,  // +/-4.096V range = Gain 1
      G2048 : CONFIG.PGA_2_048V,  // +/-2.048V range = Gain 2 (default)
      G1024 : CONFIG.PGA_1_024V,  // +/-1.024V range = Gain 4
      G512 : CONFIG.PGA_0_512V,  // +/-0.512V range = Gain 8
      G256 : CONFIG.PGA_0_256V,  // +/-0.256V range = Gain 16;
    };
    

    Lines 71..75:

    function ADS1X15(i2c) {
      this.i2c = i2c;
      this.addr = 0x48;
      this.gain = 2048;
    }
    

    Lines 80..84:

    ADS1X15.prototype.readRegister = function(reg) {
      this.i2c.writeTo(this.addr, reg);  
      var d = this.i2c.readFrom(this.addr, 2);
      return ((d[0] << 8) | d[1]) / 32768;  // returning value in volts
    };
    

    Lines 85..89:

    // set the gain, with a value in mv (6144, 4096, 2048, 1024, 512 or 256). The value is the full swing, so 256 = +/- 0.256v
    ADS1X15.prototype.setGain = function(gain) {
      if (!(("G" + gain) in GAINS)) throw new Error("Gain "+gain+" not found");
      this.gain = gain;
    };
    

    Lines 97 and 98:

      // Set PGA/voltage range
      config |= GAINS["G"+this.gain];
    

    Usage:

    I2C1.setup({ scl: B6, sda: B7 });
    var ads = require("ADS1X15").connect(I2C1);
    ads.setGain(4096); 
    setInterval(function(){
      ads.getADC(3, function(val) {
          console.log(v.toFixed(3) + "V");     
      });
    },3000);
    

    Now there are a few memory saving options that can be applied...

    a) Throw GAINS away. There is already an object in place that holds the gains: CONFIG... therefore, lines 87. and 98. each become 2 lines and read:

       var g = "PGA_" + ("0" + this.gain + " V").substr(-5); 
       if (!(g.substr(0,5) + "_" +  g.substr(5)) in CONFIG)) throw new Error("Gain "+gain+" not found");
    
      var g = "PGA_" + ("0" + this.gain + " V").substr(-5); 
      config |= CONFIG[g.substr(0,5) + "_" +  g.substr(5)];
    

    b) Simplify the gains part in CONFIG like you had it in GAINS and it forming the property names becomes inline instead of an extra composition line for string g:

    Lines 18..23:

    PGA_6144mV: 0x0000,  // +/-6.144V range = Gain 2/3
    PGA_4096mV: 0x0200,  // +/-4.096V range = Gain 1
    PGA_2048mV: 0x0400,  // +/-2.048V range = Gain 2 (default)
    PGA_1024mV: 0x0600,  // +/-1.024V range = Gain 4
    PGA_512mV: 0x0800,  // +/-0.512V range = Gain 8
    PGA_256mV: 0x0A00,  // +/-0.256V range = Gain 16
    
        if (!("PGA_" + this.gain + "mV") in CONFIG)) throw new Error("Gain "+gain+" not found");
    
        config |= CONFIG["PGA_" + this.gain + "mV"];
    

    Btw, I wondered why the config is set all the times, is it not sufficient to set it once? Could make it sense to have 4 configs (3, or 2 in - 1 or 2 - diff mode)?

About

Avatar for allObjects @allObjects started