kelly / node-i2c

Node.js native bindings for i2c-dev. Plays well with Raspberry Pi and Beaglebone.
Other
216 stars 91 forks source link

MCP23017 - Cannot write bytes correctly. Help please! #26

Closed bwarp closed 10 years ago

bwarp commented 10 years ago

I am using a raspberryPi and nodeJS. I am trying to communicate to an MCP23017. I can manually init the chip, and turn a led on and off with i2cset below. // configure Port A pins GPA0-6 as outputs and GPA7 as an input. (10000000 in binary and 0×80 in hex) : // i2cset -y 1 0x20 0x00 0x80 // Then we set GPA0 to logic high which will enable the LED : // i2cset -y 1 0x20 0x14 0x01

But in node I cannot get it to work! What am I doing wrong? var i2c = require('i2c'); var address = 0x20; // point to your i2c address, debug provides REPL interface var wire = new i2c(address, {device: '/dev/i2c-1', debug: true}); wire.writeBytes(0x00, 0x80); // Init MCP 23017 wire.writeBytes(0x14, 0x01); // Turn LED ON Thanks!!!!

jvondrus commented 10 years ago

Try this:

var i2c                 = require ('i2c');
var MCP23017            = new i2c (0x20);
MCP23017.writeBytes (0x00, [0x80], function (err)   { handle err});
MCP23017.writeBytes (0x14, [0x01], function (err)   { handle err});

Your problem is at data type, you have to use "[ ]" for data to send. Like:

wire.writeBytes("register", ["byte1", "byte2", "byte3" ...]);
bwarp commented 10 years ago

That worked when I added the callback err function! Thanks!

function callback(err) { if (err) { console.log(err.message); } }