Close

Reading touch sensors

A project log for Xorya Touch

Xorya Touch PCB is even cheaper implementation of Xorya game console on single PIC32 chip

shaosSHAOS 03/06/2017 at 07:520 Comments

In order to read touch sensors (they will be implemented as part of PCB) we need to connect internal pull-ups to those pins then to read we should set pins out, clear pins and set pins in then read a few times (until parasitic capacitor is fully charged through internal pull-up) and analyze (touched pads charge longer):

int tch[4],cc[5],c,i;
ConfigCNBPullups(CNB8_PULLUP_ENABLE|CNB9_PULLUP_ENABLE|CNB10_PULLUP_ENABLE|CNB11_PULLUP_ENABLE);
....
// somewhere in the game loop:
 tch[0]=tch[1]=tch[2]=tch[3]=5;
 mPORTBSetPinsDigitalOut(BIT_8|BIT_9|BIT_10|BIT_11);
 mPORTBClearBits(BIT_8|BIT_9|BIT_10|BIT_11);
 mPORTBSetPinsDigitalIn(BIT_8|BIT_9|BIT_10|BIT_11);
 cc[0] = mPORTBRead();
 cc[1] = mPORTBRead();
 cc[2] = mPORTBRead();
 cc[3] = mPORTBRead();
 cc[4] = mPORTBRead();
 for(i=0;i<5;i++)
 {
  c = cc[i];
  if((c&BIT_8) && tch[0]==5) tch[0]=i;
  if((c&BIT_9) && tch[1]==5) tch[1]=i;
  if((c&BIT_10) && tch[2]==5) tch[2]=i;
  if((c&BIT_11) && tch[3]==5) tch[3]=i;
}
tch[4] are states for 4 touch sensors - if respective number is greater than 1 then sensor is "touched". Experiment showed that if nothing is touched array will have all 1s, but if one or more sensors touched - respective value will go up (usually 3 if one sensor was touched or 2 if more than one sensor were touched). Interesting fact that it's relatively easy to detect intermediate finger position when it touches 2 neighboring sensors so with 4 pads we got 8 positions (or even 9 if we count center position as well).

Discussions