hi I'm trying to use a 16 channel capacitive touch sensor named ttp229BSF with Arduino uno. i use the following code that doesn't work. when i touch the keys it detects the keys but also prints out some wrong keys before detecting the touched key . i linked the required jumper as shown in the attached pic . here is my code. what is the problem here? is there a better and more robust way to do this based on the timing diagram on the datasheet that i attached ?
#define SCL_PIN 8
#define SDO_PIN 9
/* Used to store the key state */
byte Key;
void setup()
{
/* Initialise the serial interface */
Serial.begin(9600);
/* Configure the clock and data pins */
pinMode(SCL_PIN, OUTPUT);
pinMode(SDO_PIN, INPUT);
}
/* Main program */
void loop()
{
/* Read the current state of the keypad */
Key = Read_Keypad();
/* If a key has been pressed output it to the serial port */
if (Key)
{
Serial.println(Key);
}
/* Wait a little before reading again
so not to flood the serial port*/
delay(300);
}
/* Read the state of the keypad */
byte Read_Keypad(void)
{
byte Count;
byte Key_State = 0;
/* Pulse the clock pin 16 times (one for each key of the keypad)
and read the state of the data pin on each pulse */
for(Count = 1; Count <= 16; Count++)
{
digitalWrite(SCL_PIN, LOW);
/* If the data pin is low (active low mode) then store the
current key number */
if (!digitalRead(SDO_PIN))
Key_State = Count;
digitalWrite(SCL_PIN, HIGH);
}
return Key_State;
}
Failing to provide the TTP229 datasheet and failing to comprehend what an I²C interface is.
It appears you also do not understand the alternate reading mode which is similar to I²C in a very "stripped-down" version, as your code simply fails to implement it, thus the random data.
The timing chart you cite is not in the datasheet I found. Best give us the Web link to the information you are attempting to follow.
no you are wrong the module I'm using is ttp229BSF which is different from the link you provided and doesn't work with normal I2C it is based on a 2 wire serial interface you can download it here sunrom-611100-1.pdf (326.2 KB) @Paul_B
Try posting a link to what you think your sensor is. After all you don't go to a doctors and when he asks you what is wrong say what I had last time look it up in my notes. Or maybe you are just as uncooperative with your doctor?
OK, so there are two different version s of the TTP229; the "BSF" which appears to be the one @salar1991 has, and the "LSF" which has an actual I²C interface.
The "simplified" interface of the "BSF" nevertheless requires a protocol which in some ways resembles I²C; you need to synchronise the serial stream by asserting a pulse on the "SDO" line, then clock out the bits with the "SCL" line whilst reading the "SDO" line (which means it must variously be an output and then an input).
That is what your code needs to do.
So the question remains, from where are you getting the supposed information on how to do this? (Mike or) I could probably figure out how to do it (I have ordered a board; will arrive in three weeks or so) but if you have an actual reference it may be a good start.
@Paul_B i changed the code as you said the correct number is printed out but its repeating for ever when i press the key one time
#define SCL_PIN 8
#define SDO_PIN 9
byte Count;
byte Key_State = 0;
/* Used to store the key state */
int Key1;
void setup()
{
/* Initialise the serial interface */
Serial.begin(9600);
/* Configure the clock and data pins */
pinMode(SCL_PIN, OUTPUT);
}
/* Main program */
void loop()
{
/* Read the current state of the keypad */
Key1 = Read_Keypad();
/* If a key has been pressed output it to the serial port */
if (Key1)
{
Serial.println(Key1);
}
/* Wait a little before reading again
so not to flood the serial port*/
delay(300);
}
/* Read the state of the keypad */
byte Read_Keypad(void)
{
pinMode(SDO_PIN, OUTPUT);
digitalWrite(SDO_PIN, HIGH);
delayMicroseconds(100);
digitalWrite(SDO_PIN, LOW);
delayMicroseconds(100);
digitalWrite(SDO_PIN, HIGH);
delayMicroseconds(20);
pinMode(SDO_PIN, INPUT);
/* Pulse the clock pin 16 times (one for each key of the keypad)
and read the state of the data pin on each pulse */
for (Count = 1; Count <= 16; Count++)
{
/* If the data pin is low (active low mode) then store the
current key number */
digitalWrite(SCL_PIN, HIGH);
delayMicroseconds(100);
if (digitalRead(SDO_PIN))
Key_State = Count;
digitalWrite(SCL_PIN, LOW);
delayMicroseconds(100);
}
return Key_State;
}