Monday, December 1, 2014

Arduino code for TTP229 Touch 16 button

Arduino Code for TTP229 16 Key Touch button

I could not find the arduino library code for TTP229 keys and hoping this will help others who have bought this item from ebay to be able to use the keyboard.

The pin layout is clear enough that only 8 buttons were mapped to direct pin mapping. In order to use full 16 buttons, direct pin mapping is definitely not the way to go. So the serial clock and data line has to be used.

Finding the proper datasheet was a major problem. First I got reference to two conflicting documents, one located here and another here.

The first datasheet had a pin layout for TTP229 as 28 pins which matched with the hardware chip. But second link had a pin layout of TTP229 containing 48pins which did not match what was in hardware. So obvious choice to follow in the datasheet seemed to be the first link which is from datasheet4u. The document mentions that keyboard supports I2C and was happy to hear it. I put that under the buspirate and nothing was detected. After checking all connection, it did not seem to be supporting any i2c protocol and the initial excitement was subdued. Then I decided to see the waveform for serial clock. It was outputting the data as expected as per specification two here. So even though the pin diagram of TTP229 (48pin) does not match the actual hardware pin (28pins), decided to follow it to see what happens.

From the captured waveform subdued the moment I realized that button is outputting 0 for the button number that is pressed as well as for button number+8. So the waveform contained two 0s for even a single key being pressed. After a bit of reading the default configuration as per the spec is, TTP229 works in 8 key mode. In order to make it work in 16 key mode, one has to connect TP2 to ground through a high value resistor. Then referring to the schematic diagram, it is easy to find there is a handy high value resistor (1M) connected to header next to each of the buttons. By manually verifying the wire trace matching with the schematic diagram, decided to connect TP2 to Gnd.

Initially I was skeptical manufacturer will send a 16key keyboard configured to work as 8key keyboard. But after convincing myself that I have done enough reading, and a good enough dose of get it to work or burn it, I added a trace connecting pins (highlighted in Yellow) as shown in the picture.


Then tried the code and it seemed to work exactly as wanted. Emboldened by the success I added two more traces to support multi-key (highlighted in Red). That also worked perfectly. Interestingly, the pressed keys wont appear even if new key has been pressed. Only after the second key is released the first key is shown as pressed.

After going through the spec, I realized it can even be used for interrupts. The spec says the keyboard can generate interrupt every 32ms min. It generates the long low to indicate a button press or release event. It is upto the microcontroller to read the data. It does not state what happens when data read takes a long time from interrupt and the key change happens, can it cause interrupt while reading or right after reading. If the interrupt happens after read but before the interrupt handler is enabled, a key press may be lost.  So, ideally the keyboard interrupt should be serviced well within 32ms, if you don't want to miss any key press. Depending on the exact behavior of the chip, the current code has a potential to have missed data, if the interrupt service routing is not called within 32ms of the key being pressed.

Also, the code takes a bit of time to read from the keyboard. So decided to keep the interrupt service separate from interrupt handler. So the caller has to keep calling HasEvent and if it has Event, then it may call GetButtonEvent to get the event. The event will be triggered for Press and Release and the caller has to find out how long the button was pressed.

On the other hand, calling GetButtonStatus will return the buttons pressed currently.

In short, here is the instruction to get the keyboard working with Arduino

1. Add a trace between 3&4 in header P1 (highlighted with yellow). This is the must if you want to use all 16 buttons. Refer to the picture above.
2. Add a trace between 1&2 in header P1 and 7&8 in  header P2 (highlighted in Red). This is optional and needed only if you want to enable multi-key press at the same time. Refer to the picture above.
3. Connect the VCC & GND from the keyboard to corresponding pin in arduino.
4. Connect SCL from the keyboard to pin 7 in arduino. (If you connect to anything else, need to update the code with the right pin number. Find the line containing statement TTP16Button.Configure and replace 7 with what ever your pin number is used by you.)
5. Connect SDO from the keyboard to pin 2 in arduino. (Connecting to 2 or 3 will support interrupt based event for the keyboard and others has to be constantly polled. Find and replace line containing TTP16Button.Configure with SDO number.)

Here is how my connection looks like


You are good to go and should be able to see the output in the serial port. This is what I saw in my keyboard

This is the output for pressing keys from 16 to 1. But when key 2 is pressed I pressed key 1, that is why you see Button Pressed : 2 followed by pressed 1 and then both these keys are released.

The code can be found here. Feel free to copy/modify and repost the code.
 

24 comments:

  1. Great thanks for this I was getting crazy, cant get it, but your help was GREAT, thanks. Greatings from Mexico

    ReplyDelete
    Replies
    1. Miguel, where is this "util/atomic.h" to be found ?
      I too want to use the 16key touchpad. My search on the net was not successful. Prads has not replied, so I am stuck

      Delete
  2. Thanks man It works like a charm

    ReplyDelete
  3. Thanks, excellent article. With help from this article my TTP229 module works within minutes.

    ReplyDelete
  4. Thanks, excellent article. With help from this article my TTP229 module works within minutes.Even very helpful.
    My question is if you press 1 or another Arduino can light a lamp on a free digital pin but will remain lit until reapesa key on the keyboard?
    A simple code how could I do that would be gold for me.
    P.S.
    For months he lay in a drawer keyboard unfortunately I could not find usefulness because of you now I sterso dust and I hope I can use as I said above.
    Many thanks good man.
    A code with a small example that can illuminate an LED mounted on a digital pin eg D11 but I will stay lit until key on the keyboard reapesa concerned would be very helpful :)
    I should Wed 5 keys to ignite a light touch one port to the next touch to Arduino and Leduc left the key is meant to use it
    E-mail : publicitate77@gmail.com
    And thousands of topics you did a great thing for us that for months looking for an answer I searched even the producer but did not receive e-mail from them from the seller replied that going but knows here me clear Light :))

    ReplyDelete
  5. Thank you a lot for the interrupt-driven code, it saved me a lot of time. It works perfectly on MSP430 with only a minor modification.

    ReplyDelete
  6. i face a trouble to operate a led with the ttp229. can you help me to resolve this. i need a example of in which led's are mounted on a digital pin and that can illuminate by pressing any one of the switch out of 16. i have a 16 way tt9229. please help me to resolve this problem.
    e.mail : himanshuhimgr8@gmail.com

    ReplyDelete
  7. hi it is a very helpful article
    i also bought the same board from local retailer but i am not getting the desired result
    i have made connections as suggested by you highled in red and yellow,
    i have connected SDO to INT1 of my controller but my controller is not getting interrupted ,
    can you suggest any thing

    ReplyDelete
  8. Hi I am also one of those trying to get the touchKeypad16 to work with a ArduinoMega250. I have searched everywhere for the library util which contains the headeder file atomic.h
    Have had no luck. Serching for a library called util produced some results but no such header file in those libraries. Searching for atomic.h produced weird results.
    Which is the util library referred to in your include statement?
    Please help.
    My email is arvindk671@gmail.com.
    Many thanks.

    ReplyDelete
  9. Hi! Very interested article. Welcome to my site about Arduino projects http://ngin.pro/!

    ReplyDelete
  10. The 28pin layout does'nt support I2C (only SPI), the 48pins version has I2C support via SDA and a0,a1,a2 pins.

    ReplyDelete
  11. The solution is to see the chip code and use the propper protocol:
    -TTP229BSF is 2 wire protocol.
    -TTP229LSF is I2C protocol.
    www.abcdosaber.com.br

    ReplyDelete
    Replies
    1. i uploaded same source code but it is sensing some random number from 1 t0 16
      do help me how can i implement this task iam struggling past 3days...

      your help is appreciated
      thank u

      Delete
  12. thanks for sharing your knowledge.
    same source code i uploaded but iam not able to get output please any one help.

    your help is appreciated!
    thank u

    ReplyDelete
  13. This is realy a Nice blog post read on of my blogs It is really helpful article please read it too my blog PINTEREST BUTTON NOT WORKING you can visits our websites or toll free no +1-866-558-4555. solve your problem fastly.

    ReplyDelete
  14. Such a nice blog Thanks for sharing information. It is really helpful article please read it to my blog Pinterest button not working.

    ReplyDelete
  15. This is the only code that works.
    Many many thanx.

    ReplyDelete
  16. Thank you for this code!!!!
    I have one issue though: If the button is pressed too short,
    the message "Are you not using interrupt? Should never come here for interrupt based system." comes up (Button "0" is returned) although the interrupt actually fires.
    I made some additions to capture the press time (It looks it needs to be longer than ~ 500 ms.
    Promise: This link is safe:
    Enjoy: https://drive.google.com/file/d/1Aof0ImfR81NzL5bn0WdIZwFSMxlNg8vz/view?usp=sharing

    P.S: I am not a professional programmer, so don't complain if you don't like something....just change it to your standards.

    Thank you, Prads again!

    ReplyDelete
  17. PS: Still not the greatest touch-pad The Chinese designers could not think of something better than the numbers 1-16?
    How about 0-9, 4 directional arrows, C and BS?
    In a telephone-like layout...
    Would not have cost anything.
    Also the electronics should be on the back-side of the board with no trough-holes.... Just saying....

    Luckily you can print out a new key-board layout on a sticker and stick it on the board (and laminate it)

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. Added all the required files to github along with datasheet and code.
    https://github.com/adpande/electronics/tree/main/arduino/TTP229

    ReplyDelete
  20. Hi perfect blog, can you specif also other configuration via TP jumpers?

    ReplyDelete