sankmv
Published © GPL3+

Capacitive Touch Disk Pad

Let's consider connecting the Capacitive Touch Desk Pad keyboard to the NodeMCU ESP8266 Board. let's create a project for controlling the RGB

BeginnerProtip10 hours1,148
Capacitive Touch Disk Pad

Things used in this project

Hardware components

WIFI NodeM ESP8266
×1
Keyboard Capacitive Touch Disk Pad
×1
16 Keys Capacitive touch TTP229 I2C module
×1
Transistor Tip122
×3
Resistor 1 kOm
×3
Power supply unit 12V 2A
×1

Software apps and online services

Arduino IDE
Arduino IDE
MicroPython
MicroPython

Story

Read more

Schematics

Connecting Capacitive Touch Disk Padto ESP8266

Project to set the RGB color of the led strip using the Capacitive Touch Disk Pad keyboard

Code

Sketch of the definition of the pressed key on the keyboard in C (Arduino IDE)

Arduino
#include "Wire.h"
 
#define TTP229_LSF 0x57 
 
void setup() {
  Wire.begin(0,2); // wake up I2C bus
  Serial.begin(9600);
}
 
void getTTP229data(byte *a, byte *b) {
  Wire.requestFrom(TTP229_LSF, 2);    // request 2 bytes from slave device TTP229  
  int dataN = 0;
  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read();    // receive a byte as character 
    if (dataN == 0) *a = c;
    if (dataN == 1) *b = c;  
    dataN++;
  }
}
 
void showTTP229data()
{
  byte aa,bb = 0;
  getTTP229data(&aa,&bb);
  Serial.println();
  printByte(aa);
  printByte(bb);
 
  delay(1000);
}
 
void loop() {
  showTTP229data();
}
 
void printByte (byte bytePrint) {
   for (unsigned int mask = 0x80; mask; mask >>= 1) {
       if (mask & bytePrint) {
           Serial.print('1');
       }
       else {
           Serial.print('0');
       }
   }
}

Script for determining the pressed key on the keyboard in python

Python
from machine import I2C,Pin 
import time

data = bytearray(2)

def getkeys(data):
   arr=[0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
   str1="click "
   mask=0x0001
   for i in range(1,16):
     if (mask<<i) & data > 0:
        str1=str1+str(arr[i])+" "
   print(str1)

i2c =I2C(scl=machine.Pin(2), sda=machine.Pin(0))

while True:
    data = i2c.readfrom(0x57,2)
    word=data[0]<<8 | data[1]
    if word !=0 :
       getkeys(word)
    time.sleep(0.5)

RGB led strip color setup program using the Capacitive Touch Disc Pad keyboard - (Arduino IDE)

Arduino
#include "Wire.h"
 
#define TTP229_LSF 0x57 

int pwmRED=4;
int pwmGREEN=14;
int pwmBLUE=5;

int colors[][3]={{0,0,0},{0,0,0},
            {100,0,0},{100,100,0},{0,100,0},
            {0,100,100},{0,0,100},{100,0,100},
            {255,0,255},{255,0,0},{255,255,0},
            {0,255,0},{0,255,255},{0,0,255},
            {60,60,60},{150,150,150}};

void setup() {
  Wire.begin(0,2); // wake up I2C bus
  //Serial.begin(9600);
  pinMode(pwmRED,OUTPUT);
  pinMode(pwmGREEN,OUTPUT);
  pinMode(pwmBLUE,OUTPUT);
  analogWrite(pwmRED,0);
  analogWrite(pwmGREEN,0);
  analogWrite(pwmBLUE,0);
}
 
void getTTP229data(byte *a, byte *b) {
  Wire.requestFrom(TTP229_LSF, 2);    // request 2 bytes from slave device TTP229  
  int dataN = 0;
  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.read();    // receive a byte as character 

    if (dataN == 0) *a = c;
    if (dataN == 1) *b = c;  
    dataN++;
  }

}
 
int getKey()
{
  byte aa,bb = 0;
  unsigned int mask=0x01;
  int key=0;
  
  getTTP229data(&aa,&bb);
  //printByte(aa);
  //printByte(bb);
  //Serial.println();
  for(int i=0;i<8;i++) {
     if(((mask<<i) & aa) > 0) {
        key=8-i;
        i=8;
     }
  }
  for(int i=0;i<8;i++) {
     if(((mask<<i) & bb) > 0) {
        key=16-i;
        i=8;
     }
  }
  return key;
}
 
void loop() {
  int k=getKey();
  if(k>0) {
    analogWrite(pwmRED,colors[k][0]);
    analogWrite(pwmGREEN,colors[k][1]);
    analogWrite(pwmBLUE,colors[k][2]);
  }
  delay(500);
}
 
void printByte (byte bytePrint) {
   for (unsigned int mask = 0x80; mask; mask >>= 1) {
       if (mask & bytePrint) {
           Serial.print('1');
       }
       else {
           Serial.print('0');
       }
   }
}

RGB led strip color setup program using the Capacitive Touch Disc Pad keyboard - python

Python
from machine import I2C,Pin,PWM 
import time

i2c =I2C(scl=machine.Pin(2), sda=machine.Pin(0))
pwmRED=PWM(Pin(4))
pwmGREEN=PWM(Pin(14))
pwmBLUE=PWM(Pin(5))

data = bytearray(2)

def getkeys(data):
   arr=[0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
   str1="click "
   mask=0x0001
   for i in range(1,16):
     if (mask<<i) & data > 0:
        print("i=",arr[i])
        return arr[i]
   
def setRGB(key):
    colors=[[0,0,0],[0,0,0],
            [512,0,0],[512,512,0],[0,512,0],
            [0,512,512],[0,0,512],[512,0,512],
            [1023,0,1023],[1023,0,0],[1023,1023,0],
            [0,1023,0],[0,1023,1023],[0,0,1023],
            [200,200,200],[500,500,500]]
     
    pwmRED.duty(colors[key][0])
    pwmGREEN.duty(colors[key][1])
    pwmBLUE.duty(colors[key][2])


while True:
    data = i2c.readfrom(0x57,2)
    word=data[0]<<8 | data[1]
    if word !=0 :
       k=getkeys(word)
       setRGB(k)
    time.sleep(0.5)

Credits

sankmv
5 projects • 8 followers
Contact

Comments

Please log in or sign up to comment.