Pixl.js & MCP23017 connected using I2C

Posted on
  • Hi,
    I have connected a port expander MCP23017 to pixl.js using I2c (using D5 for scl : D5 and D4 for sda)
    My target is to use MCP 23017 for reading or writing some signal.

    There are instructions in https://www.espruino.com/MCP23xxx but it doesn't work for reading or writing:
    It doesn't reports error messages, here is the code:

       var i2c = new I2C();
       i2c.setup({ scl : D5, sda: D4 });
       var address = 0x20; //this is the address, set by the address pins.
       // for MCP23017
       var port=require("MCP23017").connect(i2c,nul­l,address);
       port.B0.write(1);      //<--IT MUST CHANGE TO '1' BUT B0 CONTINUE AT '0' LEVEL 
     console.log(port.A0.read());    //<--ALWAYS READ '1'

    I wrote a new code and now is working just for reading signals:

       var i2c = new I2C();
       i2c.setup({ scl : D5, sda: D4 });     
       i2c.writeTo(0x20, 0X12); 
       var x = i2c.readFrom(0x20, 1);  
       i2c.writeTo(0x20, 0X13); 
       var y = i2c.readFrom(0x20, 1);

    But for writing , it doesn't work:

       var i2c = new I2C();
       i2c.setup({ scl : D5, sda: D4});
       i2c.writeTo(0x20, 0x00);
       i2c.writeTo(0x20, 0x12);      // program all Bank A to output
       i2c.writeTo(0x20, 0x00);
       i2c.writeTo(0x20, 0x00);
       i2c.writeTo(0x20, 0x12);      // Change all pins of Bank A to '1'
      i2c.writeTo(0x20, 0xFF);
    i2c.writeTo(0x20, 0x13);      // program all Bank B to output     
    i2c.writeTo(0x20, 0x00);
    i2c.writeTo(0x20, 0x00);
    i2c.writeTo(0x20, 0x13);      // Change all pins of Bank B to '1'
    i2c.writeTo(0x20, 0xFF); 

    Some help, please?
    Thans in advance.


  • I did some port expander work... up to try to use interrupts... it's not Puck, but take a look at MCP23017 PortExpander driving 4x4 KeyPad w/ interrupt / setWatch for 'keyDown' detection.

    What power supply / source are you using? I hope that you have something stronger or separate in place to run the additional electronics. A button cell is just not power full enough and may cause brown outs...

  • Hi,
    Thanks for your answer, your port expader is a really excellent job for reading a keypad.
    I tested your software and it changes signal output of GPA0, GPA1, GPA2, GPA3 to '1'. !!

    Reading your code, I solved my problem. Attached is a simple example of a '0' & '1' signal serie.

    Thanks a lot!!!!!!!,


    1 Attachment

  • @pacogh, urvw.

    Which post was actually helping you? ...because I noticed that I gave you the link to the conversation that was using the existing module, and the change/addition was done separately in order to avoid polling / full scans. But there is another conversation that just deals with port expander in general: Exploring adding setWatch/clearWatch (interrupt handling) to MCP23017 (MCP2308) Portexpander Ports. In this conversation I worked through the module and made some changes and additions.

    Therefore, the question again, which post was it, that was instrumental to get your application working?

    ...some fine 'fine print': Btw, if you give the uploaded file the extension .js, default browser configuration let's you look right into the file. If you want to embed code in the post, prefix it with an empty line and a line with 3 backwards ticks, and also end it with with an extra line of 3 backwards ticks and an empty line (you can also click the [ </> code ] button in the post editing pane menu bar / on top of the text edit area and replace enter code here with a copy of your code from the IDE code editor.

  • Hi,

    Your post is at http://forum.espruino.com/conversations/­312487/

    Attachment that helped me to clarify how MCP2307 is working is: https://espruino.microco.sm/api/v1/files­/e9474e4809d42cf2aa0a28c9377a4c4987ee1b7­2.js
    and MCP2307 datasheet.


  • Is there some obvious issue with the code from https://www.espruino.com/MCP23xxx (eg http://www.espruino.com/modules/MCP23017­.js) that is causing it not to work?

    If so it'd be good to get it fixed so others don't hit the same problems :)

  • Hi Gordon,

    http://microco.sm/out/zEd6h is full working but it was not simple for me to understand how it manages I2C bus.

    I printed data sent from Espruino to MCP23017 thru function ' p._w = function(r,dArr) { this.i2c.writeTo(0x20,r,dArr); print (r,dArr); return this; }; '

    Comparing it with MCP23017 it was easy to write minimun code to use bank A & B as output.

    Other experiences:

    • I'm using a power Bank using 'alway-on mode' instead of a button battery, so power wasn't the problem as 'allObjects' sujected too.
    • My previous design was running on Arduino, my mistake was transtlate that Arduino code to Espruino. Espruino needs other detailed commands to manage I2C (attached Arduino example)


    1 Attachment

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Pixl.js & MCP23017 connected using I2C

Posted by Avatar for pacogh @pacogh