• Hello,

    I had a TTP229 capacitative keypad module lying around and thought it would be useful with Espruino. These can be picked up fairly cheaply online. Just be careful: There are actually two variants of this chip and it isn't always clear which is which!

    The modules I see on ebay seem to have the TTP229-BSF version of the chip - this is what mine is and what this code works with. I think if the module has "SCL" and "SDO" pins on it things should be OK. The other version has an I2C interface and won't work with this code.

    To explain the code: You can treat the chip as though it has an SPI interface, and read out 2 bytes of data from it. These contain the keypresses, but inverted - ie '1' means not set, and '0' means set.

    I hope the code below makes sense and is useful to somebody!

    // Espruino example for TTP229-BSF keypad
    
    // Connect keypad SCL and SDO lines to these pins:
    SCL = D14;
    SDO = D15;
    
    // Setup SPI interface but clock needs to be quite slow otherwise we get data errors
    SPI1.setup({miso:SDO, sck:SCL, mode:integer=3,baud:integer=10000});
    
    // Periodically read the keys and display on the console
    
    setInterval(function() {
      // Read 2 bytes of data from the device
      data=SPI1.send([0,0]);
      // Invert the data so that a '1' means button pressed. This gives us
      // two bytes: 
      // Top = buttons 1-8 : Bits are Button 1,2,3,4,5,6,7,8
      // Bottom = buttons 9-16 : Bits are Button 9,10,11,12,13,14,15,16
      top=data[0]^0xff;
      bottom=data[1]^0xff;
    
      // Now display as binary
      console.log("BTN1-8",top.toString(2),"BT­N0-16",bottom.toString(2));
    
    },500);
    
    

    1 Attachment

    • ttp229-keypad.jpg
  • Sat 2020.05.02

    Thank you @ColinP for the code snippet and pics. Had just seen a few of these recently and started imagining what uses might result. Hex calculator maybe?

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

Code example: Using a TTP229 capacitative keypad with Espruino

Posted by Avatar for ColinP @ColinP

Actions