内蔵タッチセンサ
ESP32

概要

Arduino core for the ESP32を使った、ESP-WROOM-32開発ボードの内蔵タッチセンサについての実験です。

ESP-WROOM-32には、10個の静電容量式タッチセンサが内蔵されていて、Arduino core for the ESP32にも、それを利用するための関数が用意されています。また、スケッチ例も付属していたので、簡単に実験してみました。

Arduino core for the ESP32のインストールのページはこちら

実験

利用できるピン

ESP-WROOM-32には、10個のタッチセンサが付属しています。以下に、Arduinoソフトウェアから利用する際のシンボルとピン番号との対応を記載します。

シンボル ピン番号
T0 GPIO4
T1 GPIO0
T2 GPIO2
T3 GPIO15
T4 GPIO13
T5 GPIO12
T6 GPIO14
T7 GPIO27
T8 GPIO33
T9 GPIO32

利用できる関数

タッチセンサ用に関数が3つ用意されています。以下の説明は、ソースコード上のコメントから読み取ったものです。詳細なデータシートまでは見て記載したものではありません。

関数名 説明
void touchSetCycles(uint16_t measure, uint16_t sleep); センサの値を読み取る際の、測定とスリープの周期を設定する。デフォルトは、measure、sleep とも、0x1000で、その際には、touchRead()は0.5msかかる。
uint16_t touchRead(uint8_t pin); pinで指定したタッチパッドの値を読み取る。値が0に近ければ、タッチされたことを示す。
void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint16_t threshold); pinで指定したタッチパッドの値が、threshold未満になると、userFuncを呼び出す。

実験

TouchRead

ソースコードはスケッチ例そのままなですが、以下に引用します。

ファイル > スケッチ例 > ESP32 > Touch > TouchRead から取得できます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// ESP32 Touch Test
// Just test touch pin - Touch0 is T0 which is on GPIO 4.
 
void setup()
{
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Test");
}
 
void loop()
{
  Serial.println(touchRead(T0));  // get value using T0
  delay(1000);
}

T0(IO4)に、ミノムシクリップ付きのジャンプワイヤを接続したときの結果を以下に示します。

それぞれの値のときに、どういう状態だったかを以下にまとめます。

状態
60付近 T0に何も接続していないとき
49付近 T0にジャンプワイヤを接続したとき
13付近 ジャンプワイヤのミノムシクリップを手で触ったとき

TouchInterrupt

こちらもソースコードはスケッチ例そのままなですが、以下に引用します。

ファイル > スケッチ例 > ESP32 > Touch > TouchInterrupt から取得できます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
This is un example howto use Touch Intrrerupts
The bigger the threshold, the more sensible is the touch
*/
 
int threshold = 40;
bool touch1detected = false;
bool touch2detected = false;
 
void gotTouch1(){
 touch1detected = true;
}
 
void gotTouch2(){
 touch2detected = true;
}
 
void setup() {
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Interrupt Test");
  touchAttachInterrupt(T2, gotTouch1, threshold);
  touchAttachInterrupt(T3, gotTouch2, threshold);
}
 
void loop(){
  if(touch1detected){
    touch1detected = false;
    Serial.println("Touch 1 detected");
  }
  if(touch2detected){
    touch2detected = false;
    Serial.println("Touch 2 detected");
  }
}

T2(IO2)とT3(T15)に、ミノムシクリップ付きジャンプワイヤを接続したときの結果を以下に示します。

ピンに触っている間中、割り込みが発生するようです。

バージョン

Hardware:ESP-WROOM-32
Software:Arduino 1.8.3/Arduino core for the ESP32

最終更新日

March 21, 2022

inserted by FC2 system