Mr.Dee67
Published © MIT

Mouse on a TTGO

MCU user input device practically for free, using an empty potato-chips roll and four capacitive touch pins of the ESP32

BeginnerWork in progress1 hour21
Mouse on a TTGO

Things used in this project

Hardware components

LILYGO® TTGO LoRa32 T3 V1.6.1
LILYGO® TTGO LoRa32 T3 V1.6.1
×1
wires
×4
empty potato chips roll
×1
stickytape, thin cargo type
×1

Story

Read more

Schematics

Lilygo TTGO oled v2-1.6.1 pin-out with capacitive touch mouse guide

Stick to the GPIOs and measurements suggested.

Code

ESP32: Processing touch pads for mouse control

C/C++
Code snippet from working sketch on Lilygo TTGO oled V2-1.6.1, used in Arduino IDE
// globals...

int touch_baselevel1=0; // x distance sensor // touch pin base level when untouched
int touch_baselevel2=0; // y distance sensor
int touch_baselevel3=0; // touchpad physical contact sensor
int touch_baselevel4=0; // "leftclick" sensor

int approx1=0;// actual touch pin reads (x ramp)
int approx2=0; //y ramp
int approx3=0; // pad contact sensor for ramp algo
int approx4=0; // for leftclick separate button
int lastapprox1=0;// to remember previous touch pin reads (for ramp detection)
int lastapprox2=0;
int mytouchpin1=4; // user input touchpad GPIO use...
int mytouchpin2=12;
int mytouchpin3=14; // these 2 we share with the sd-card, may have to set again as input after card access.
int mytouchpin4=15; //14=touch_sd_sclk, 15=touch_sd_mosi, btw: 25=adc_led, 34,35=adc;
int mousex=64; // calculated mouse coords, also sets initial mouse coords
int mousey=52;
int realmousex=0; // smoothed mouse coords (rubberband-follower)
int realmousey=0;

// in setup() :
  pinMode(mytouchpin1 , INPUT); // used as touch sensor x-motion gpio 04
  pinMode(mytouchpin2 , INPUT); // used as touch sensor y-motion gpio 12
  pinMode(mytouchpin3 , INPUT); // used as touch sensor finger contact gpio 14
  pinMode(mytouchpin4 , INPUT); // used as touch sensor separate "secure" leftclick button gpio 15

  // touchpin calibration / get average base level
  for(int i=0;i<10;i++)
  {
   touch_baselevel1+=touchRead(mytouchpin1);
   touch_baselevel2+=touchRead(mytouchpin2);
   touch_baselevel3+=touchRead(mytouchpin3);
   touch_baselevel4+=touchRead(mytouchpin4);
   //  todo: find way to auto-calibrate frequently (aka how to know the user doesn't touch anything)
   delay(4);
  }
  touch_baselevel1/=10;
  touch_baselevel2/=10;
  touch_baselevel3/=10;
  touch_baselevel4/=10;

// the following function should then be called in a loop, eg. in loop(),
// every 10 to 20 ms. Then draw a mouse at the coordinate: realmousex,realmousey

void myUpdateMouse()
{
  // user input, touchpin 12, 14, 4 ,15, see definitions in globals section
  lastapprox1=approx1;
  lastapprox2=approx2;
  approx1=touchRead(mytouchpin1); // these variable names seem a bit misleadig, but anyway
  approx2=touchRead(mytouchpin2);
  approx3=touchRead(mytouchpin3);
  approx4=touchRead(mytouchpin4);
  int mi=1;  // window for gradient detection / noise exclusion
  int ma=150;

  // mouse navigation...  
  if(approx3 <(touch_baselevel3-35)) // finger contact? only compute motion if finger is touching
  {
   // any ramp up or down of distance x sensor?
   if ( (approx1 < (touch_baselevel1-10)) && (abs(approx1-lastapprox1) >mi) && (abs(approx1-lastapprox1) <ma)  )
   {
    int tmpx=(approx1-lastapprox1)/4; // this divisor affects mouse sensitivity...
    if(tmpx>0){tmpx*=1.4;} // boost if positive
    mousex-=tmpx; 
    if(mousex>129) mousex=129;
    if(mousex<-2)  mousex=-2;
   }
   // any ramp up or down of distance y sensor?
   if ( (approx2 < (touch_baselevel2)-10) &&   (abs(approx2-lastapprox2) >mi) && (abs(approx2-lastapprox2) <ma)  )
   {
    int tmpy=(approx2-lastapprox2)/6; // this divisor affects mouse sensitivity...
    if(tmpy>0){tmpy*=1.4;} // boost if positive
    mousey-=tmpy;
    if(mousey>65) mousey=65;
    if(mousey<-2) mousey=-2;
   }
  }  
  else // no finger contact
  {
  }
  // smoothing mouse movement
  realmousex-=((realmousex-mousex)/5); // this divisor affects the speed at which the smoothed mouse catches up with the real mouse coords
  realmousey-=((realmousey-mousey)/5); // (so the name may be confusing, but anyway :-) ) 
  // a mouse pointer may then be drawn at position realmousex,realmousey..
}

Credits

Mr.Dee67
1 project • 0 followers

Comments