touchRead breaking analogWrite?

zmaker
Posts: 6
Joined: Tue Aug 29, 2023 3:42 pm

touchRead breaking analogWrite?

Postby zmaker » Tue Aug 29, 2023 3:58 pm

Some context for my issue, I'm designing a custom flashlight and have already received the prototype pcb. I thought I'd try to add capacitive touch but now 2/5 of the lights don't work. The lights DO work when I run a code that simply turns each one on and then off to test the leds. However, when I add touchRead(T0) the leds on gpio pins 14 and 15 stop working while the leds on pins 16, 17, and 18 continue to function as normal.

I'm using touchRead(T0) to reach the touch input, and have tried digitalWrite, analogWrite, and ledcWrite which all work without touchRead but fail when I add it back.

I have done a little bit of reading and the things that jump out are the following. If they are in fact issues, I haven't found a way around them.
1) Slot 1 of SDIO pins, which includes pins 14 and 15, is multiplexed with touch... though I don't really know how that could cause an issue
2) Touch0 is on an ADC2 pin, pins 14 and 15 are also on ADC2 pins... again, not sure why this would cause an issue though.


***
  1. //#include <WiFi.h>
  2. #include "driver/adc.h"
  3.  
  4. #define LEDwhite 14
  5. #define LED1 15
  6. #define LED2 16
  7. #define LED3 17
  8. #define LED4 18
  9.  
  10. byte brightness;
  11.  
  12. int state = 0;
  13.  
  14.  
  15. // TESTING
  16. // the number of the LED pin
  17. const int ledPin1 = 15;
  18. const int ledPin2 = 16;  // 16 corresponds to GPIO16
  19. const int ledPin3 = 17;
  20. const int ledPin4 = 18;
  21.  
  22. // setting PWM properties
  23. const int freq = 5000;
  24. const int ledChannel = 0;
  25. const int resolution = 8;
  26.  
  27. // the setup routine runs once when you press reset:
  28. void setup() {
  29.   // initialize serial communication at 9600 bits per second:
  30.   Serial.begin(9600);
  31.   //Serial.begin(115200);
  32.   //adc_power_release();  // disable ADC
  33.  
  34.   //pinMode(LEDwhite, OUTPUT);
  35.   //pinMode(LED1, OUTPUT);
  36.   //pinMode(LED2, OUTPUT);
  37.   ledcSetup(ledChannel, freq, resolution);
  38.   ledcAttachPin(ledPin1, ledChannel);
  39.   ledcAttachPin(ledPin2, ledChannel);
  40.   //ledcAttachPin(ledPin3, ledChannel);
  41.   //ledcAttachPin(ledPin4, ledChannel);
  42.  
  43.   //pinMode(LED3, OUTPUT);
  44.   //pinMode(LED4, OUTPUT);
  45.  
  46.   //digitalWrite(LEDwhite, LOW);
  47.   //digitalWrite(LED1, LOW);
  48.   //digitalWrite(LED2, LOW);
  49.   //digitalWrite(LED3, LOW);
  50.   //digitalWrite(LED4, LOW);
  51.  
  52.   brightness = 100;
  53. }
  54.  
  55. // the loop routine runs over and over again forever:
  56. void loop() {
  57.   //Serial.print("\nDefault ESP32 MAC Address: ");
  58.   //Serial.println(WiFi.macAddress());
  59.  
  60.   // Various Tests
  61.   capacativeTouchTest();
  62.   //blinkTest();
  63.   //printButtonStates();
  64. }
  65.  
  66. void capacativeTouchTest() {
  67.   //void adc_power_on(void);
  68.   //delay(10);
  69.   Button1 = touchRead(T0); // get value of Touch 0 pin = GPIO 4... can swap with 24?
  70.   //void adc_power_off(void);
  71.   //delay(10);
  72.  
  73.   if (Button1 < 30){
  74.     ledcWrite(ledChannel, 50);
  75.     Serial.println("ON");
  76.     delay(10);
  77.     //turnWhiteOn();
  78.   }
  79.   else {
  80.     ledcWrite(ledChannel, 0);
  81.     Serial.println("OFF");
  82.   }
  83. }
  84.  
  85. void brightnessTest() {
  86.   delay(500);        // delay in between reads for stability
  87.   //digitalWrite(LEDwhite, LOW);
  88.  
  89.   if (brightness < 50) {
  90.     brightness = brightness + 20;
  91.   } else {
  92.     brightness = 0;
  93.   }
  94.  
  95.   analogWrite(LEDwhite, brightness);
  96.  
  97.   Serial.print("Brightness: ");
  98.   Serial.println(brightness);
  99. }
  100.  
  101. void turnWhiteOn(){
  102.   analogWrite(LEDwhite, 50);
  103.   delay(50);
  104. }
  105.  
  106. void blinkTest() {
  107.   delay(250);
  108.   turnWhiteOn();
  109.   delay(250);
  110.   analogWrite(LEDwhite, LOW);
  111.   analogWrite(LED1, 50);
  112.   delay(250);
  113.   analogWrite(LED1, LOW);
  114.   analogWrite(LED2, 50);
  115.   delay(250);
  116.   analogWrite(LED2, LOW);
  117.   analogWrite(LED3, 50);
  118.   delay(250);
  119.   analogWrite(LED3, LOW);
  120.   analogWrite(LED4, 50);
  121.   delay(250);
  122.   analogWrite(LED4, LOW);
  123.   delay(5000);
  124. }
  125.  
  126. }

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: touchRead breaking analogWrite?

Postby lbernstone » Thu Aug 31, 2023 6:34 pm

I recommend against using T0 naming (A0, D0, etc). Use the actual pin numbers. That way you know exactly which pin you are talking about.
ADC2 is unavailable when WiFi is running. I don't know whether this affects the touch peripheral, but you can certainly test without WiFi and see if that is what is affecting it.

zmaker
Posts: 6
Joined: Tue Aug 29, 2023 3:42 pm

Re: touchRead breaking analogWrite?

Postby zmaker » Thu Aug 31, 2023 6:38 pm

Wi-Fi was commented out and not being run while testing

zmaker
Posts: 6
Joined: Tue Aug 29, 2023 3:42 pm

Re: touchRead breaking analogWrite?

Postby zmaker » Tue Sep 19, 2023 4:42 am

zmaker wrote:
Thu Aug 31, 2023 6:38 pm
Wi-Fi was commented out and not being run while testing
Good, I don't want wifi interfering.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: touchRead breaking analogWrite?

Postby lbernstone » Wed Sep 20, 2023 1:23 am

Does everything work if you set an interrupt instead of a direct read?
https://github.com/espressif/arduino-es ... errupt.ino

zmaker
Posts: 6
Joined: Tue Aug 29, 2023 3:42 pm

Re: touchRead breaking analogWrite?

Postby zmaker » Wed Sep 20, 2023 1:46 am

lbernstone wrote:
Wed Sep 20, 2023 1:23 am
Does everything work if you set an interrupt instead of a direct read?
https://github.com/espressif/arduino-es ... errupt.ino
I did try this, great suggestion. It did not work either, I even tried detaching the interrupt before writing the led pwm. With both cases, I had the same issues as before where it worked on 3/5 of the led pins and did not with 2 of them.

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: touchRead breaking analogWrite?

Postby lbernstone » Wed Sep 20, 2023 3:26 am

Your example is ... confused. Can you reproduce the issue with this code?

Code: Select all

// TESTING
// the number of the LED pin
const int LEDwhite = 14;
const int ledPin1 = 15;
const int ledPin2 = 16;
const int ledPin3 = 17;
const int ledPin4 = 18;
 
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
 
void setup() {
  Serial.begin(9600);
  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(ledPin1, ledChannel);
  ledcAttachPin(ledPin2, ledChannel);
  ledcAttachPin(ledPin3, ledChannel);
  ledcAttachPin(ledPin4, ledChannel);
}
 
void loop() {
  analogWrite(LEDwhite, HIGH);
  delay(1000);
  ledcWrite(ledChannel, 128);
  delay(1000);
  analogWrite(LEDwhite, LOW);
  ledcWrite(ledChannel, 0);
  Serial.printf("touchRead: %d\n", touchRead(4));
  delay(5000);
}

zmaker
Posts: 6
Joined: Tue Aug 29, 2023 3:42 pm

Re: touchRead breaking analogWrite?

Postby zmaker » Thu Sep 21, 2023 1:18 am

I slightly changed the code to the below (just to make the timing go quicker).
In the first test I ran the exact code below, and after the first run, only 3/5 lights came on (this is after the touchRead(4) ran)
In the second test, the only thing I changed was commenting out the print line... then all 5 lights continuously toggled

Code: Select all

// TESTING
// the number of the LED pin
const int LEDwhite = 14;
const int ledPin1 = 15;
const int ledPin2 = 16;
const int ledPin3 = 17;
const int ledPin4 = 18;
 
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
 
void setup() {
  Serial.begin(9600);
  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(ledPin1, ledChannel);
  ledcAttachPin(ledPin2, ledChannel);
  ledcAttachPin(ledPin3, ledChannel);
  ledcAttachPin(ledPin4, ledChannel);
}
 
void loop() {
  analogWrite(LEDwhite, HIGH);
  delay(1000);
  ledcWrite(ledChannel, 128);
  delay(1000);
  analogWrite(LEDwhite, LOW);
  ledcWrite(ledChannel, 0);
  Serial.printf("touchRead: %d\n", touchRead(4)); //DISABLED THIS FOR TEST 2
  delay(500);
}
Attachments
test2Cycle2.png
this is a photo from test 2, the second time the void loop() ran
test2Cycle2.png (1.08 MiB) Viewed 4680 times
test1Cycle2.png
this is a photo from test 1, the second time the void loop() ran
test1Cycle2.png (913.71 KiB) Viewed 4680 times

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: touchRead breaking analogWrite?

Postby lbernstone » Thu Sep 21, 2023 2:15 am

I'm unable to reproduce. If you are driving those LEDs through the esp32, then I would suggest a power issue. I have run many LED projects using the esp32 (and variants), and have not encountered this, although I generally prefer a button over capacitive.

zmaker
Posts: 6
Joined: Tue Aug 29, 2023 3:42 pm

Re: touchRead breaking analogWrite?

Postby zmaker » Tue Oct 03, 2023 7:14 am

Do you know if there is a way to completely "turn off" the capacitive touch function? I have found a bunch of article online, and read though tons of forums to no avail

I even tried running the touchRead() on a separate core, but experienced the same loss in function

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 77 guests