Skip to content

analogRead() triggers unrelated interrupt #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MaxSNi opened this issue Jul 28, 2017 · 4 comments
Closed

analogRead() triggers unrelated interrupt #541

MaxSNi opened this issue Jul 28, 2017 · 4 comments
Assignees
Labels
Area: Peripherals API Relates to peripheral's APIs. Type: For reference Common questions & problems

Comments

@MaxSNi
Copy link

MaxSNi commented Jul 28, 2017

I am experiencing a strange bug:

Whenever i use analogRead(), an unrelated interrupt will also trigger, and by changing around a little i could even get 2 interrupts to trigger with every analogRead().

I have not testet every analog pin, but so far this bug happens with A0, A3, A4, A5, A6, A7, but not A10, A11, A12, A13, A14.

I can reproduce the issue with the code below - every 2 seconds it will trigger AnalogRead() and this will trigger one of the button Interrupts. I have have multiple components hooked up as well, but this did not seem to change anything when i tried removing some of them, or having the button GPIOs unconnected.

const int BUTTON_1 = 25;
const int BUTTON_2 = 26;

void setup() 
{
  pinMode(BUTTON_1, INPUT_PULLUP);  
  pinMode(BUTTON_2, INPUT_PULLUP);
  
  attachInterrupt(BUTTON_1, int1, FALLING);
  attachInterrupt(BUTTON_2, int2, FALLING);
  
  Serial.begin(115200);
}

void loop() 
{
  delay(2000);  
  float derp = analogRead(A3);  
  Serial.println(derp);
}

void int1() 
{
  Serial.println("1 Interrupt triggered");
}

void int2() 
{
  Serial.println("2 Interrupt triggered");
}
@Raienryu97
Copy link
Contributor

Hey
Tried the code you posted along with just the board, and yes, the interrupts are being triggered.
If you're looking for a workaround
Change the Pull type to PULLDOWN (and of course you should change the button trigger from GND to Vcc since it's pulled down)

I tried with Pulldown and the problem is solved.

  pinMode(BUTTON_1, INPUT_PULLDOWN);
  pinMode(BUTTON_2, INPUT_PULLDOWN);

Also, maybe you already know this , but just in case, it's never good to Serial.println() inside of an ISR. The best idea would be to update a volatile bool inside of ISR and based on the bool, print in void loop()

@me-no-dev me-no-dev added the Type: For reference Common questions & problems label Aug 1, 2017
@KevinVanthuyne
Copy link

Had this issue as well, the workaround works 👍 Very weird that this is still happening though

@VojtechBartoska
Copy link
Contributor

@PilnyTomas Please check if this is still relevant now. There have been several updates to AnalogRead so maybe this is already covered.

@PilnyTomas
Copy link
Contributor

I have tried this with ESP32, C3, S2 and S3 and could not reproduce...
I'm closing this issue. If anyone experiences this error again, fell free to reopen this issue, or open a new one. Please don't forget to add as much info as possible.

@PilnyTomas PilnyTomas self-assigned this Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Peripherals API Relates to peripheral's APIs. Type: For reference Common questions & problems
Projects
None yet
Development

No branches or pull requests

6 participants