touchRead breaking analogWrite?
Posted: 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.
***
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.
***
- //#include <WiFi.h>
- #include "driver/adc.h"
- #define LEDwhite 14
- #define LED1 15
- #define LED2 16
- #define LED3 17
- #define LED4 18
- byte brightness;
- int state = 0;
- // TESTING
- // the number of the LED pin
- const int ledPin1 = 15;
- const int ledPin2 = 16; // 16 corresponds to GPIO16
- const int ledPin3 = 17;
- const int ledPin4 = 18;
- // setting PWM properties
- const int freq = 5000;
- const int ledChannel = 0;
- const int resolution = 8;
- // the setup routine runs once when you press reset:
- void setup() {
- // initialize serial communication at 9600 bits per second:
- Serial.begin(9600);
- //Serial.begin(115200);
- //adc_power_release(); // disable ADC
- //pinMode(LEDwhite, OUTPUT);
- //pinMode(LED1, OUTPUT);
- //pinMode(LED2, OUTPUT);
- ledcSetup(ledChannel, freq, resolution);
- ledcAttachPin(ledPin1, ledChannel);
- ledcAttachPin(ledPin2, ledChannel);
- //ledcAttachPin(ledPin3, ledChannel);
- //ledcAttachPin(ledPin4, ledChannel);
- //pinMode(LED3, OUTPUT);
- //pinMode(LED4, OUTPUT);
- //digitalWrite(LEDwhite, LOW);
- //digitalWrite(LED1, LOW);
- //digitalWrite(LED2, LOW);
- //digitalWrite(LED3, LOW);
- //digitalWrite(LED4, LOW);
- brightness = 100;
- }
- // the loop routine runs over and over again forever:
- void loop() {
- //Serial.print("\nDefault ESP32 MAC Address: ");
- //Serial.println(WiFi.macAddress());
- // Various Tests
- capacativeTouchTest();
- //blinkTest();
- //printButtonStates();
- }
- void capacativeTouchTest() {
- //void adc_power_on(void);
- //delay(10);
- Button1 = touchRead(T0); // get value of Touch 0 pin = GPIO 4... can swap with 24?
- //void adc_power_off(void);
- //delay(10);
- if (Button1 < 30){
- ledcWrite(ledChannel, 50);
- Serial.println("ON");
- delay(10);
- //turnWhiteOn();
- }
- else {
- ledcWrite(ledChannel, 0);
- Serial.println("OFF");
- }
- }
- void brightnessTest() {
- delay(500); // delay in between reads for stability
- //digitalWrite(LEDwhite, LOW);
- if (brightness < 50) {
- brightness = brightness + 20;
- } else {
- brightness = 0;
- }
- analogWrite(LEDwhite, brightness);
- Serial.print("Brightness: ");
- Serial.println(brightness);
- }
- void turnWhiteOn(){
- analogWrite(LEDwhite, 50);
- delay(50);
- }
- void blinkTest() {
- delay(250);
- turnWhiteOn();
- delay(250);
- analogWrite(LEDwhite, LOW);
- analogWrite(LED1, 50);
- delay(250);
- analogWrite(LED1, LOW);
- analogWrite(LED2, 50);
- delay(250);
- analogWrite(LED2, LOW);
- analogWrite(LED3, 50);
- delay(250);
- analogWrite(LED3, LOW);
- analogWrite(LED4, 50);
- delay(250);
- analogWrite(LED4, LOW);
- delay(5000);
- }
- }