kelly / node-i2c

Node.js native bindings for i2c-dev. Plays well with Raspberry Pi and Beaglebone.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

readBytes function not working with LM75A

GuySawyer opened this issue · comments

commented

Good day all

I am using the readBytes() function to read two bytes of an LM75A temperature sensor.
My code is as follows:

var i2c = require('i2c');       
var address = 0x48;     //Device address found using i2c-tools functions from command line
var device = {device: '/dev/i2c-1', debug: false};  //LM75A connected: SDA > P9_20 , SCL > P9_19
var command = 0x00;     //Not sure what the command means

var wire = new i2c(address, device); 

wire.readBytes(0x00, 2, function(err, res)  //Reads 4 Bytes from the LM75A
{
    var Temp = ((res[1] * 2^8) + res[0]) / 2^7;
    Temp = Temp * 0.5;
    console.log(res);
    console.log(Temp +" deg");
});

The address is correct, I am sure of that. My problem is with the Command variable.
I can't find out from the source code what that variable means, and to my knowledge, the LM75A doesn't require a command variable for reading.

The results I am getting from this code are all over the place. Varying between 2 deg, and 115 deg.

Any advice?
Thanks

The readBytes command requires a command. For your case, i'd just use the ReadByte and read in two bytes. Then, it looks like you just need to do some bit shifting as per the LM75A spec.

var temp = bytes[0] << 8;
newTemp |= bytes[1];
newTemp = newTemp >> 5;

temp = newTemp * 0.125;
commented

Thanks for your quick response.

I started using the readByte function. It worked some of the time, but eventually it ends up pulling the SDA line low, and then failing to read the value anymore.

Any idea what might be causing this?

It's really hard to diagnose hardware issues, but you may want to try a pull-up resistors on the line.

i've digged through the same problem and ended up with:

wire.readBytes(0x00, 2, function(err, data){         
        console.log(err);                            
        console.log(data);                           

        var tempA = data[0];                         
        var tempB = data[1];                         
        var temp = (tempA << 8) | tempB;             
        temp = temp >> 5                             


        var signBit = temp >> 10;                    
        var result;                                  
        if(signBit > 0){                             
                result = (temp * 0.125) - 256;       
        } else {                                     
                result = temp * 0.125;               
        }                                            

        console.log(result);                         
});                    

and it works just fine, shows both positive and negative temps and does readBytes without command - registry with temp according to the LM75A spec, and I read just two bytes. Then some shifting here and there to get thre real value and voila :)