AtTouch Library for Arduino and the AT42QT1070
As a follow up to the previous post, I would like to present to readers the new AT Touch for the Arduino. This library wraps the code mentioned in the previous article in a nice easy to use Arduino Library. The name “AtTouch” comes from At (Atmel, so we don’t confuse it with other brands of touch capacitance chips) and Touch because its for their 7 channel touch capacitance chip, the AT42QT1070.
We created this library during our development of the Trigger Trap which heavily relies the AT42QT1070 for its 4 main user interface buttons.
The Library uses the Arduino Wired library to communicate with the AT42QT1070 via i2C. This means the AT42QT1070 has to be used in Coms mode, not standalone. The schematic below is taken from page 6 of the datasheet, please refer to the datasheet for greater details on the chips layout.
Once you got the chip all wired up in Comms mode, to the Arduino you should be ready to start programming with the AtTouch library. The first step is to of course download the library: Download AtTouch
The next thing to do is to place the library folder labeled AtTouch in your sketchbook/libraries folder. Then place the example folders in your sketchbook folder.
Open up the example titled “AtTouch_press”.
“AtTouch_press” is the the simpler example, that just give you the plain functionally of a single button press. The library is very simple to use, but I will explain each detail below:
At the start of your sketch be sure to include both the Wire.h library and the AtTouch.h library. The AtTouch.h depends on Wire.h so make not forget it.
#include #include
The following line declares the pin number of the external interrupt you want to use. The Arduino Uno only has 2 pins that work as interrupts. Pin 2, or 3. Its your choice, but obviously make sure it matches the CHANGE wire from the AT42QT1070.
int interruptPin = 3;
After that, its just a matter of instantiating the library object, and declaring a simple variable to hold the currently active key value.
AtTouch touch; int hitKeyNum;
The next important part of the code, is in the loop function. The touch.hit() member function just returns true or flash, whether or not a key has been pressed. If the key has been pressed the we call touch.readActiveKey() in order to find out which key has been pressed.
if(touch.hit() == true) { hitKeyNum = touch.readActiveKey();
The variable hitKeyNum holds the key number. The AT42QT1070 key numbers are labelled 0-6. However there is one exception to this. When a user stops pressing a key the function touch.readActiveKey() will return a “9”. What this means is the key is now up. Its a signal to the user that the key is no longer pressed. Thats all there is to, it! I told you its simple!
The next example demonstrates how to use press and hold functions as well as single press.
Everything is essentially the same as the previous example, but you need to add another set of if statements to use the additional function.
if(touch.hold() == true) //press and hold { keyDown = true; hitKeyVal = touch.getKey(); }
the touch.hold() function works just like the touch.hit() function however its actually timing your press to see if the key is held longer then 900msecs. If it is held longer, then it sets a boolean variable to keyDown true, and allows you to execute some specific code when that state is set. Also the touch.hold() can be used in conjunction with touch.hit() so you can detect one or the other for each key. In the following code we are using hold just to output a value multiple times rather then just once with a one time press:
if(touch.hold() == true) //press and hold { keyDown = true; hitKeyVal = touch.getKey(); } if(touch.hit() == true) //just press once { keyDown = true; //read which key was hit hitKeyVal = touch.readActiveKey(); } if(keyDown == true) { keyDown = false; digitalWrite(ledPin,HIGH); Serial.print("button #"); Serial.print(hitKeyVal); Serial.println(" Down"); }
Anyway that should get you started with the AtTouch library. As you can see its super easy to use, and really there is not much to explain, so there is no excuse not to add touch capacitance buttons to your next project!
This was helpful. Thanks.