Electronics > Beginners
How to initialise INPUT_PULLUP before Interrupt runs in Arduino?
(1/4) > >>
skillz21:
I have an Arduino sketch that involves something like this. There is a button, and because I'm too lazy to create a voltage divider by adding an extra resistor at the breadboard, I just did "pinMode(buttonPin, INPUT_PULLUP);". Easy peasy. In my project, I have an interrupt that runs off this button, FALLING. So when it is pressed, the pin goes low and the interrupt is triggered. Unfortunately, for some reason, the interrupt gets triggered when the code starts to run, at the beginning. Thinking about this with my noob reasoning, I can only conclude that the interrupt runs even before the part that makes the button pin an INPUT_PULLUP, therefore triggering it falsely.

To test this, in the interrupt function, I added an if statement that goes like this:

  if(millis() > 1)
  {                         
    someBoolean = true;
  }

instead of my original

    someBoolean = true;

And what do you know, that actually solved the issue. Now, I could just leave this as it is, but I don't like leaving a part of the code and commenting that I don't understand why it has to be there. Plus I'm curious to know whether there is a fix to this other than what I did. Also, I could be completely wrong in why this is happening, if so, please do let me know.
iMo:
Use "attachInterrupt()" after some small delay.
It also could be you do "attachInterrupt" before the "pinMode".
skillz21:

--- Quote from: imo on March 22, 2019, 12:07:13 pm ---Use "attachInterrupt()" after some small delay.
It also could be you do "attachInterrupt" before the "pinMode".

--- End quote ---
I do attatchInterrupt() after the pinMode. Is there no other way to get rid of this issue than just use a delay? whether it be at attachInterrupt() or the actual function?
iMo:
The internal pullups are rather week. It could be the longer wires to your button pick a noise.
You have to overcome your laziness and add a 10k pullup and 10nF against gnd :)
skillz21:

--- Quote from: imo on March 22, 2019, 12:28:37 pm ---The internal pullups are rather week. It could be the longer wires to your button pick a noise.
You have to overcome your laziness and add a 10k pullup and 10nF against gnd :)

--- End quote ---
You're probably right. But do you know why adding a delay just gets rid of the problem altogether?
Navigation
Message Index
Next page
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod