Pin change interrupt not firing with MySensors

  • Hi!

    I have been working on building motion sensors with MySensors to be able to trigger events on my Vera. Earlier I was able to get a few sensors working using 3.3V 8MHz Pro Minis. Now I am trying to build one with a custom ATMega328 board that uses the internal RC oscillator. For some reason I am unable to get the board to respond to pin change interrupts when using MySensors gw.sleep() using the MotionSensor.ino. I debugged the issue with a Pro Mini that (afaicr) is also using the internal oscillator instead of the external oscillator on the board. Everything however works correctly if I do not include MySensors code in my sketch and use attachInterrupt with mode set to CHANGE:

    void setup() {
      pinMode(3, INPUT_PULLUP);
      attachInterrupt(digitalPinToInterrupt(3), handler, CHANGE);
    void loop() {
    void handler() {

    However if I pass LOW mode to gw.sleep, the interrupt seems to work and a message seems to be sent to the controller correctly. But of course this only happens when the pin is in LOW state and not on changes.

    Has anyone got a clue on what might be causing the issue? I read through the interrupt handling code in MySensors that I believe is relevant in my case (MyHwATMega328 class), but was unable to find anything that might cause the issue.

    edit Forgot to mention, but I am using version 1.5 of MySensors /edit

  • Hardware Contributor


    for the moment you can't use pin interrupt change lib with mysensors.
    Also, note, pinchange need you handle the change state. these are not working like D2/D3 INT0/1.
    I'm using pinchange with mysensors, but i use raw registers, my own isr for interrupts, and my sleep function.

  • Thanks for the reply scalz!

    @scalz said:

    for the moment you can't use pin interrupt change lib with mysensors.

    I'm not sure what you mean by "pin interrupt change lib", but I would assume it would be possible to use the CHANGE type when calling gw.sleep as in the examples:

    Also, note, pinchange need you handle the change state. these are not working like D2/D3 INT0/1.
    I'm using pinchange with mysensors, but i use raw registers, my own isr for interrupts, and my sleep function.

    I have only connected the one motion sensor to my board and the interrupt handler just sends the current state (tripped / not tripped) to the controller. Thus I do not think I need to do anything special here.

    To clarify, I am using the MotionSensor.ino code without any modifications on my board. The execution reaches the gw.sleep call without problems, but does not resume even if the pin state changes. I am able to read the value from the pin by setting SLEEP_TIME to 1000 and this way confirm that the value of the pin does change and that can be read correctly.

  • Hardware Contributor

    you're right, i misread because of the title "pin change" lol . So you're using D3, clear 🙂
    quickly checked the lib, but i don't see anything fancy for the moment. if i find time, perhaps i'll try to reproduce but i've not heard others having this issue. Do you have this problem without doing any changes in the Motion sketch?

  • What I am currently uploading is the unmodified version of MotionSensor.ino linked above. I have previously build a motion sensor using a 3.3V pro mini with the same code. The only difference that I can think of with my current setup is that I am using the internal RC oscillator instead of the external and have changed the fuses accordingly by using the bootloader upload in Arduino IDE. I used the following board configuration: ATmega328P (ATmega boot, internal 8MHz)

  • Hi.
    Perhaps I ran into a similar issue?
    I created a new node with older 1.5.x version of my sensors-library.
    But I was not able to get a door-contact working via interrupt on pin 3 with a sketch I used more than 5 times before.
    Last change I can remember, was updating Arduino IDE for MacOS Sierra....

    How can I get back a working interrupt controlled door-switch?!

  • @idstone: Have you tried passing another mode (e.g. LOW) to gw.sleep method? The issue I am having is that with LOW the interrupt fires, but with CHANGE (which is the default in the sketches) it does not.

  • Mod

    @elysion Issues were fixed in the 2.1 beta of the MySensors library, so you'd better have a look at this code to see how to wake from interrupt.
    Furthermore, you're using the ATmega out of spec when trying to wake from CHANGE; see

  • @elysion

    Hi. It seems, that the problem is related to complete charge of my 3,3V arduino pro's.
    Reaction on interrupt while closing a door-switch on pin 3 works on an older 5V arduino.
    I've read about problems at a customer-evalutation (amazon), especially with arduino pro clones and their interrupt-function.
    Bought some more over store from mysensors on ebay... hope these will work correctly 😞

  • Mod

    @idstone said:

    I've read about problems at a customer-evalutation (amazon), especially with arduino pro clones and their interrupt-function.

    Can you quote your source please?

  • @Yveaux

    5.0 von 5 SternenWunderbar!
    Von Ludwig Enfield am 19. Juli 2015

    Bei den Arduino Remakes gibt es immer wieder kleine Abweichungen im Pinout. Also aufpassen - das betrifft hier insbeondere die Interrupt-Pins 0 und 1 bzw. die Pins A6/A7 (u.a. Analog-Ports).

  • Mod

    @idstone thanks!
    If I understand it correctly, the feedback states the pinout of arduino pro mini clones is often slightly different, especially on interrupt pins and analog.
    For analog pins this is certainly true, for interrupt pins I have not experienced this myself (yet).

  • In one sketch I am using pin change interrupt. But that is a node I built from scratch. I have the feeling the issues arise with the MyS function "sleep()" in combination with a set time. As long as I call sleep(0xff,0x00, 0xff, 0x00, 0);
    pin change interrupts work. As soon as I change any of the parameters, the node will not wake up anymore.

    That was on 2.0 I have yet to convert and try it on 2.1.

    As soon as I have done that, I will post the code .

  • Mod

    @DavidZH Just a shot in the dark, but could you try @tekka workaround from and see if it still crashes ?

  • @Yveaux I will try that as soon as I'm back from my winter sports vacation (as far as you can call lush green Alpine meadows that. At least the slopes are well maintained!). I have no access to a node and gateway now.

    For me it worked for that sketch as it's main function is reading momentary switches, either on mains power or on battery. As soon as I want to add a sensor that needs to send it's info on a set interval I needed it to be on mains power.

    But like I said, I will convert the sketch to 2.1 this week, and next week I'll give the workaround a shot.
    Keep you posted!

  • @Yveaux

    Got some news... the new charge of Arduino Pros arrived, but the problem persists. So I started more trail ' n error and got something:

    I installed the Arduino-IDE and mysensors-1.5.4-library on an manjaro-based notebook. I had to do some symlinking to get a newer version of avrdude working and now the same sketch I used before works now!

    Back to my Mac, flashing the same sketch, the node doesn't work correctly?!
    So problem seems to be the version of avrdude, which is offered by the arduino-ide?!

    I finally downgraded to IDE 1.6.8 and especially AVR Boards 1.6.8.
    Now everything works as expected .... 😄

  • Mod

    So it was just a bug in the latest updates of the ide and boards? I'm having probably the same problem on a mega2560

  • @gohan

    I'm not familiar with your problem, but mine was, that the interrupt on pin3 didn't work as expected. So I was not able to use my new sensor as watchdog for my windows/doors. With downgrade of the Arduino-IDE AND especially AVR Boards 1.6.8 my problem is solved now and every change on that interrupt-pin is correctly recognised and send to gateway.

  • Mod

    @idstone if you don't mind, please take a look at last line of my code

  • @gohan

    Hi again. I can confirm, that I got similar problems... the state of the interrupt pin wasn't correctly recognized, too.
    May be You could try downgrading Arduino-IDE and (very important) avr boards (1.6.8) ?!

Log in to reply





Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.