Skip to content

11. Input devices

Group assignment

  • Probe an input device's analog levels and digital signals

Probe an input device's digital levels

ESP32 touch pins can be easily integrated into capacitive pads, and replace mechanical buttons. Additionally, the touch pins can also be used as a wake up source when the ESP32 is in deep sleep.

Take a look at your board pinout to locate the 10 different touch sensors – the touch sensitive pins are highlighted in pink color.

Code Touch Pin as Button

Get Touch Test Write program for getting board information over Serial monitor

// ESP32 Touch Test
// Just test touch pin - Touch5 is T5 which is on GPIO 12.

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(T5));  // get value of Touch 5 pin = GPIO 12
  delay(500);
}

Probe an input device's analog levels



Code Touch Pin Analgue Test

Get Touch Test Write program for getting board information over Serial monitor

// ESP32 Touch Test
// Just test touch pin - Touch5 is T5 which is on GPIO 12.
const byte LEDPin = 4;
void setup()
{
  Serial.begin(115200);
  delay(100); // give me time to bring up serial monitor
  pinMode(LEDPin, OUTPUT);
  Serial.println("ESP32 Touch Test");
}

void loop()
{
  delay(100);
  Serial.println(touchRead(T5));  // get value using T5 (GPIO 12)
  if(touchRead(T5)<50)
  digitalWrite(LEDPin, HIGH);
  else
  digitalWrite(LEDPin,LOW);
}

The ESP32 touch pins can sense variations in anything that holds an electrical charge. The ESP32 has 10 capacitive touch GPIOs. These GPIOs can sense variations in anything that holds an electrical charge, like the human skin. So they can detect variations induced when touching the GPIOs with a finger.

Code Analogue Read Test with CDS and potentiometer

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
void setup()
{
  Serial.begin(115200);
  delay(100); // give me time to bring up serial monitor

  Serial.println("ESP32 Analogue Test");
}

void loop()
{
  // read the analog in value:
  sensorValue = analogRead(12);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);
  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);

A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value. For example, that value controls the rate at which an LED blinks.

I connect three wires to the Arduino board. The analog input GPIO 12 connect to the signal pin of the potentiometer.


Last update: July 3, 2021