Analog Interrupts

DeadDinoDude
Posts: 1
Joined: Tue Jun 01, 2021 6:54 pm

Analog Interrupts

Postby DeadDinoDude » Tue Jun 01, 2021 7:03 pm

Hello,

I am working with the MQ-2 smoke sensor and am trying to set it up for its analog output to trigger an interrupt.

I got code working that has a capacitive touch button (wire) trigger an interrupt:

Code: Select all

/*
 * Capacitive touch code using ESP32 GPIO4 (TOUCH0)
 */ 
const int touchPIN = 4;           //Touch button PIN
const int n = 50;                 //Number of averages for threshold calc
bool interruptFlag = false;

int thresholdValue = 0;           //Used to detect when pressed
int touchSensorValue = 0;         //Holds touch value
int TEMP = 0;


int thresholdCalc(){
  for(int i=0; i<n; i++){
    touchSensorValue = touchRead(touchPIN);
    TEMP += touchSensorValue;
  }//end for
  return (TEMP/(n*2));            //Sets thresholdValue to 1/2 the average measured value
}//end thresholdCalc


void IRAM_ATTR interruptRoutine(){           //Interrupt routine 
  interruptFlag = true;
}//end interruptRoutine



void setup(){
  Serial.begin(115200);
  delay(1000); 
  thresholdValue = thresholdCalc();
  touchAttachInterrupt(touchPIN, interruptRoutine, thresholdValue);     //Setup the interrupt
}//end setup


void loop(){

  if(interruptFlag){
    interruptFlag = false;
    Serial.println("Touch detected");
    //Serial.println(measureButton());
  }//end if
  delay(50);

}//end loop

But this does not work with the MQ-2 smoke sensor.

I was wondering if anyone knows how to set up general-purpose analog interrupts.

thanks

User avatar
fasani
Posts: 195
Joined: Wed Jan 30, 2019 12:00 pm
Location: Barcelona
Contact:

Re: Analog Interrupts

Postby fasani » Sun Sep 12, 2021 5:13 am

Hi there, interesting sensor. I was checking this instructable:

https://diyi0t.com/mq2-gas-sensor-arduino-esp8266-esp32

And take care because it says the component requires 4.5 to 5 volts so make sure to power it with the 5V usb and not with the 3.3V pin.
About the interrupt so far I only tried digital Rising or the other direction interrupts in IDF and they work fine. Make sure you configure the GPIOs correctly and take a look to this part of the manual and examples
https://docs.espressif.com/projects/esp ... s/adc.html
epdiy collaborator | http://fasani.de Fan of Espressif MCUs and electronic design

Who is online

Users browsing this forum: No registered users and 33 guests