ESP32 - Analog read pin 33 affects interrupt on pin 36?

In the ESP32 design, pin 36 is connected to a push button and configured as input (that's all it can be anyway). and used to trigger interrupts. This is working well.

I just started to use pin 33 as an analog input, to read from a potentiometer, using AnalogRead command. This is working well, tested standalone.

When tested with the main software, it seems that AnalogRead of pin 33 is triggering an interrupt on pin 36.
When the AnalogRead of pin 33 is disabled, the problem stops. Is there a reason for that?

Typically the pins on portB, GPIO_NUM > 32, are configured for analog readings and not for regular digital I/O.

Post your sketch, please.

It has to be put into an anolog read pin for it to get adigital input

Also you should put this into the hardware problems not programming problems to keep the forum organized

I was not sure if this is a hardware problem or software

I am not sure I understand. GPIO 36 is defined as an input pullup digital input. GPIO33 is simply used as an AnalogRead.

Are you sure? As far as I know these pins can be used as input only, without restriction on digital usage. As I mentioned, they work just fine, as long as I don't use them together.

the go to source for ESP32 pins:

the relevant part:

Input only pins

GPIOs 34 to 39 are GPIs – input only pins. These pins don’t have internal pull-up or pull-down resistors. They can’t be used as outputs, so use these pins only as inputs:

  • GPIO 34
  • GPIO 35
  • GPIO 36
  • GPIO 39

if you want input pullup you must supply it yourself

if you digitalRead() them they are digital. if you analog Read
() them they are analog

I use external pull up resistor with GPIO 36, as input. So it is as written. GPIO36 is not really being read with DigitalRead - it is used to trigger an interrupt, which works well, as long as GPIO 33 is not AnalogRead.

#include <Arduino.h>

#define button_stop_pin    36
#define potsmtr_pin       33

volatile bool stop_pressed = false;

int num_end_pressed = 0;

void IRAM_ATTR Button_stop_isr() {
  detachInterrupt(button_stop_pin);
  stop_pressed = true;
} // end of ISR

void setup() {
  Serial.begin(9600);
  Wire.begin(); // init I2C bus as a master 
  Serial.println("..starting SETUP....");

  pinMode(button_stop_pin, INPUT);
   
  attachInterrupt(digitalPinToInterrupt(button_stop_pin), Button_stop_isr, RISING);

  Serial.println("..End of SETUP");
} // of SETUP()  


void handle_buttons(); // the function is declared at the bottom after the loop

void loop() {
  
  Serial.println("   ");
  int speed_knob_val = analogRead(potsmtr_pin); 
  
  Serial.print(speed_knob_val);
  //return;

  if (stop_pressed ) {
    // Push buttpn event occured
    Serial.print(" Push Button event occured ");
    handle_buttons();   
  } // of if()      
} // of LOOP()


void handle_buttons() {
  Serial.print("   in START_pressed   ");
  
  if (stop_pressed) {
    Serial.print("    in STOP_PRESSED    ");
    stop_pressed = false;
    wait_millis(100);
    attachInterrupt(digitalPinToInterrupt(button_stop_pin), Button_stop_isr, RISING);   
  } // of if() stopped pressed


} // of handle_buttons

I think that you need to declare function before setup loop.

Not really. It works well. The function needs to be declared before usage.

Problem is solved, yet I am not sure exactly why:
I moved the AnalogRead(33) to Core0 of the ESP32, while leaving all the main code to run on Core1. This solved the problem - interrupts on GPIO36 occur normally, while AnalogRead from GPIO33 work well.
I assume that the CPU work for AnalogRead is not well synched when interrupts are set. Yet the 2 cores actually solved the problem.

I take it back - using a separated ESP32 core did NOT solve the problem. I thought so because my testing program was faulty.

Yet now the problem is really solved - the problem was with GPIO 36. Although stated it is Input only, it connected to the voltage reference of the DAC, so every AnalogRead() affected it, and caused a false interrupt. After moving the push button to another GPIO, the problem was solved.

1 Like

I just found out: it is a BUG, confirmed by Espressif: https://github.com/espressif/esp-idf/commit/d890a516a1097f0a07788e203fdb1a82bb83520e

Does that mean that the GPIO-36 should not be used as IRQ-pin while GPIO-33 is being used as ADC line?

More than that: GPIO 36 should not be used as IRQ-pin while ANY AnalogRead is used. I checked it across GPIOs for AnalogRead. However, since Espressif identified it as a bug, it may have been fixed on later version of ESP32. I may be using old ones.

1 Like

There are:
ESP32S (marked at the bottom side)/ESP32
ESP32S2
ES32S3
ESP32C

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.