Hoping I can get some guidance on using the built-in capacitive touch sensing capability on the R4 minima for multiple pins. Tried searching but could not find much. Apologies in advance for the long post.
Here is a picture of my hardware setup for this entire post. Just jumper wires with alligator clips connected to the pins. I only touch the tips of the alligator clips with my left hand when testing, and am careful not to touch the desk or keyboard with my other hand.
I can get the example code from this tutorial to work just fine: UNO R4 Capacitive-Touch Tutorial | Arduino Documentation. The serial output is exactly what’s expected when the pin is not touched:
10:10:40.405 -> Raw value: 0
and when the pins is touched:
10:11:01.926 -> Raw value: 5758
10:11:01.970 -> Button touched!
However, if I modify the code to use two pins simultaneously, things get weird. I will paste the code here and then discuss the output when testing in different configurations. TLDR, sometimes one pin does not work at all and the other pin detects that both pins have been touched, and the behavior seems to depend on which pin is initialized first. It doesn’t seem to depend on pin proximity (I tried pins D0 and D1 initially, then D2 and D8, just in case the pins being too close to each other had something to do with it). I’ve also swapped my cables to make sure it isn’t a bad alligator clip or jumper wire.
Modified code for two pins:
#include "Arduino_CapacitiveTouch.h"
CapacitiveTouch touchButton1 = CapacitiveTouch(D0);
CapacitiveTouch touchButton2 = CapacitiveTouch(D1);
void setup() {
Serial.begin(9600);
if(touchButton1.begin() && touchButton2.begin()){
Serial.println("Capacitive touch sensing initialized.");
} else {
Serial.println("Failed to initialize capacitive touch sensor. Please use a supported pin.");
while(true);
}
touchButton1.setThreshold(1000);
touchButton2.setThreshold(1000);
}
void loop() {
int sensorValue1 = touchButton1.read();
int sensorValue2 = touchButton2.read();
Serial.print("Sensor 1: ");
Serial.print(sensorValue1);
Serial.print(" Sensor 2: ");
Serial.println(sensorValue2);
if (touchButton1.isTouched()) {
Serial.println("Button 1 touched!");
}
else{
}
if (touchButton2.isTouched()) {
Serial.println("Button 2 touched!");
}
else{
}
delay(100);
}
Here is some example serial output with this code:
Pins not touched:
10:21:15.370 -> Sensor 1: 0 Sensor 2: 4
10:21:15.492 -> Sensor 1: 0 Sensor 2: 4
10:21:15.580 -> Sensor 1: 0 Sensor 2: 0
Pin D0 touched: D0 DOESN’T WORK
10:21:59.085 -> Sensor 1: 0 Sensor 2: 0
10:21:59.156 -> Sensor 1: 0 Sensor 2: 0
10:21:59.291 -> Sensor 1: 0 Sensor 2: 0
Pin D1 touched: D1 DETECTS BOTH PINS BEING TOUCHED
10:22:33.747 -> Sensor 1: 4831 Sensor 2: 4846
10:22:33.747 -> Button 1 touched!
10:22:33.747 -> Button 2 touched!
10:22:33.826 -> Sensor 1: 4825 Sensor 2: 4872
10:22:33.826 -> Button 1 touched!
10:22:33.826 -> Button 2 touched!
However, if I reverse this line, so D1 is initialized first:
if(touchButton2.begin() && touchButton1.begin()){
Then the output is as expected:
No pins touched
10:23:57.325 -> Sensor 1: 0 Sensor 2: 30
10:23:57.401 -> Sensor 1: 0 Sensor 2: 31
10:23:57.529 -> Sensor 1: 0 Sensor 2: 25
Pin D0 touched
10:24:17.571 -> Sensor 1: 6886 Sensor 2: 33
10:24:17.614 -> Button 1 touched!
10:24:17.709 -> Sensor 1: 6901 Sensor 2: 45
10:24:17.709 -> Button 1 touched!
Pin D1 touched
10:25:18.071 -> Sensor 1: 0 Sensor 2: 5968
10:25:18.111 -> Button 2 touched!
10:25:18.205 -> Sensor 1: 0 Sensor 2: 5947
10:25:18.205 -> Button 2 touched!
Again, this problem persists with different pins, like D2 and D8 instead of D0 and D1, although I did not exhaustively test all possible combinations.
I also tried breaking the initializations up into separate lines and adding a 1s delay between them, and adding a 100ms delay between the read functions, in case they were affecting each other somehow, but that didn’t do anything.
I realize there are many other permutations I could try here (e.g. I switched the initialization order, but not the read order), but figured I would make a post about this before I go further down the rabbit hole.
