Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. ATMega328p/Arduino Interupt enabled pins?

ATMega328p/Arduino Interupt enabled pins?

Scheduled Pinned Locked Moved Hardware
19 Posts 5 Posters 7.4k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sundberg84S sundberg84

    Debounce can be handled in the code with a gw.wait (500) before going to sleep.

    S Offline
    S Offline
    Samuel235
    Hardware Contributor
    wrote on last edited by
    #6

    @sundberg84 - Use this feature along side of the normal software debounce method in which the mySensors library uses, or am i getting mixed up with the method it does use, does it use the normal arduino method of software debounce or is it using the gw.wait methid you state to use?

    @GertSanders - Right okay, i connected my nRF to the INT pin even though the mySensors doesn't use it. I forgot that it doesn't actually require it. I could remove that then since it is a node that isnt going to be used as a repeater at the moment. Am i correct in assuming that theoretically the IRQ is not needed to be connected since it wont be receiving data on the RF module that would require the uC to wake?

    For my knowledge sake, use a sleeping repeater node for example, would connecting the RF IRQ to INT0 enable the repeater node to sleep until receiving data from another node? Again, i'm not using this method, just attempting to widen my knowledge.

    MySensors 2.1.1
    Controller - OpenHAB (Virtual Machine)
    Gateway - Arduino Mega MQTT Gateway W5100

    1 Reply Last reply
    0
    • sundberg84S Offline
      sundberg84S Offline
      sundberg84
      Hardware Contributor
      wrote on last edited by sundberg84
      #7

      It works the same way, create a delay between readings to avoid getting the false debouce values.

      Controller: Proxmox VM - Home Assistant
      MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
      MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
      RFLink GW - Arduino Mega + RFLink Shield, 433mhz

      S 1 Reply Last reply
      1
      • sundberg84S sundberg84

        It works the same way, create a delay between readings to avoid getting the false debouce values.

        S Offline
        S Offline
        Samuel235
        Hardware Contributor
        wrote on last edited by
        #8

        @sundberg84 I can use either way then? I think I'll go down the route of using the gw.wait (500). Any reason to use the other way over this way?

        MySensors 2.1.1
        Controller - OpenHAB (Virtual Machine)
        Gateway - Arduino Mega MQTT Gateway W5100

        1 Reply Last reply
        0
        • sundberg84S Offline
          sundberg84S Offline
          sundberg84
          Hardware Contributor
          wrote on last edited by sundberg84
          #9

          Good thing with gw.wait is that it calls process so if you got an incoming message you dont miss that. Or you can use the normal software debounce and check millis() and dont do a reading for the first 250-500 milliseconds... its the same i think because process is called then. What you dont want to use is delay().

          Controller: Proxmox VM - Home Assistant
          MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
          MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
          RFLink GW - Arduino Mega + RFLink Shield, 433mhz

          S 1 Reply Last reply
          0
          • sundberg84S sundberg84

            Good thing with gw.wait is that it calls process so if you got an incoming message you dont miss that. Or you can use the normal software debounce and check millis() and dont do a reading for the first 250-500 milliseconds... its the same i think because process is called then. What you dont want to use is delay().

            S Offline
            S Offline
            Samuel235
            Hardware Contributor
            wrote on last edited by
            #10

            @sundberg84 said:

            What you dont want to use is delay().

            Thank you for making aware of this. I will be using gt.wait (500) I think.

            Back onto the subject of the interupt pins, so do you advice i use INT0 and INT1? I have potentially 3 switchs to put into this node, so i could do with another one. What needs to be done for the PCINT pins, like I said earlier, the state needs to be saved while it sleeps. I will be saving the state of the pin anyway for the switch to work, surly? My switch will be a normal light switch push-on push-off style (Just one button that you depress to toggle the light). Either way, if I monitor the switch in terms of saving the switch state to a variable each time it is changed then surly i would be good to just use any PCINT enabled pins?

            I'm assuming the above based on the extract of the following from the AVRFreaks forum thread I posted earlier.

            "While on modern AVRs most (all?) IO are PCINT. So there's far more interrupt sources but you cannot specify when the interrupt occurs. It just happens on every low-high or high-low transition so you have to keep track of the previous state of pins to know which way it just changed. Also they are grouped in 8's so all 8 pins on a PORT just cause a single PCINT vector/handler to be called. It's then up to you to work out which of the 8 caused the change.
            Oh and whatever else you do, do not make the mistake of using them to read switches/buttons. That's a mistake almost all beginners make as they don't know about contact bounce.
            "

            MySensors 2.1.1
            Controller - OpenHAB (Virtual Machine)
            Gateway - Arduino Mega MQTT Gateway W5100

            1 Reply Last reply
            0
            • GertSandersG Offline
              GertSandersG Offline
              GertSanders
              Hardware Contributor
              wrote on last edited by
              #11

              @samuel235 If a node goes into sleep mode using gw.sleep(0), then the radio is powered of as well. So the radio will NOT be able to wake the node via INT.

              There are ways to put the processor to sleep and keep the radio alive, but that would eat the battery very quickly. The Mysensors lib does not provide for this scenario at this moment.

              Maybe someone can extend the MySensor SLEEP function with an additional parameter to indicate the type of sleep (all peripherals, only processor and I2C, only processor and SPI/radio, only processor).

              1 Reply Last reply
              1
              • mfalkviddM Online
                mfalkviddM Online
                mfalkvidd
                Mod
                wrote on last edited by
                #12

                I like that idea.

                For the ATmega328, sleeping the mcu saves about 15mA, which is on the same level as the nrf in receive mode (14mA). So such a mode would "only" double the battery life. Other mcus (ESP8266?) might have larger differences though, resulting in higher gains.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Samuel235
                  Hardware Contributor
                  wrote on last edited by
                  #13

                  Ahh okay, thats useful information to know. Thank you @GertSanders!

                  I'm currently having a read through this PDF, more specifically page 15 reads:

                  External Interrupts
                  Pins:
                  INT0 and INT1 – range of event options
                  INT0 – PORT D [2]
                  INT1 – PORT D [3]

                  PCINT[23:0] – any signal change (toggle)
                  PCINT[7:0] – PORT B [7:0]
                  PCINT[14:8] – PORT C [6:0]
                  PCINT[23:16] – PORT D [7:0]

                  Pulses on inputs must be slower than I/O clock rate

                  So, would you advise me attaching the 3 switches to pins INT0, INT1 and any pin on that port B range. Then if i ever needed 4 switches, I could then go to any pin in port C range. Because the PCINT pins are inside of different port ranges from each other, as long as save the state of the switch/pin in a variable I should have no issue with it waking the uC up when the switch is toggled?

                  I hope i'm understanding your information correctly guys :)

                  MySensors 2.1.1
                  Controller - OpenHAB (Virtual Machine)
                  Gateway - Arduino Mega MQTT Gateway W5100

                  1 Reply Last reply
                  1
                  • scalzS Offline
                    scalzS Offline
                    scalz
                    Hardware Contributor
                    wrote on last edited by scalz
                    #14

                    hi.
                    just want to add my piece too :)
                    just a little OT about waking up with radio int. For nrf, you can't as it's power hungry , but for rfm69 you can. Guys at lowpowerlab have already made it, and it seems to consumes uA. but it is not implemented in Mysensors yet. It is a matter of RX ON/OFF timings and ATC Rssi. It is one thing I am interested in, and is on my big todolist :persevere:

                    For PCINT, you understand right if I understand you well too lol!
                    For PCINT, I have already played with this for my knowledge months ago. I did the same way @mflakvidd has linked (by playing directly with registers and setting/checking bits).
                    And yes, it would be better to make a software debounce. Something like:
                    -set your interrupts

                    • go to sleep
                    • if wake up: in interrupt routine, save state or what you want. check the interrupt "mask" meaning check the bit according to your pin in the right register. disable PCINT on your pin. Disable PCINT for your pin only could be in case your isr could be re-fired.
                    • then in your main loop, just after your gw.sleep, do a software debounce like guys advised you. Use millis or gw.wait for instance easier and test state pin. It is better to do this there, not in isr, your isr has to be short (you don't want to spend time in an interrupt)
                    • then re-enable PCINT and go to sleep.

                    Like you are reading in docs, you will need to handle the pin state. For using registers, it is just a matter of setting the right bits to enable or disable, and in your isr routine checking the right bits to know which pin has fired. That's all. I would advise you to test this in a separate sketch (not a mysensors one) to debug easier (fo instance testing some online already made sketch to see how it works for you.

                    My tests were not mysensors related, so it's useless I give you dirty things as others already gave you good links. I began with examples too ;)
                    Another docs for interrupt, but maybe you already read it:
                    http://www.gammon.com.au/interrupts
                    http://www.gammon.com.au/forum/?id=11091

                    S 1 Reply Last reply
                    2
                    • scalzS scalz

                      hi.
                      just want to add my piece too :)
                      just a little OT about waking up with radio int. For nrf, you can't as it's power hungry , but for rfm69 you can. Guys at lowpowerlab have already made it, and it seems to consumes uA. but it is not implemented in Mysensors yet. It is a matter of RX ON/OFF timings and ATC Rssi. It is one thing I am interested in, and is on my big todolist :persevere:

                      For PCINT, you understand right if I understand you well too lol!
                      For PCINT, I have already played with this for my knowledge months ago. I did the same way @mflakvidd has linked (by playing directly with registers and setting/checking bits).
                      And yes, it would be better to make a software debounce. Something like:
                      -set your interrupts

                      • go to sleep
                      • if wake up: in interrupt routine, save state or what you want. check the interrupt "mask" meaning check the bit according to your pin in the right register. disable PCINT on your pin. Disable PCINT for your pin only could be in case your isr could be re-fired.
                      • then in your main loop, just after your gw.sleep, do a software debounce like guys advised you. Use millis or gw.wait for instance easier and test state pin. It is better to do this there, not in isr, your isr has to be short (you don't want to spend time in an interrupt)
                      • then re-enable PCINT and go to sleep.

                      Like you are reading in docs, you will need to handle the pin state. For using registers, it is just a matter of setting the right bits to enable or disable, and in your isr routine checking the right bits to know which pin has fired. That's all. I would advise you to test this in a separate sketch (not a mysensors one) to debug easier (fo instance testing some online already made sketch to see how it works for you.

                      My tests were not mysensors related, so it's useless I give you dirty things as others already gave you good links. I began with examples too ;)
                      Another docs for interrupt, but maybe you already read it:
                      http://www.gammon.com.au/interrupts
                      http://www.gammon.com.au/forum/?id=11091

                      S Offline
                      S Offline
                      Samuel235
                      Hardware Contributor
                      wrote on last edited by
                      #15

                      @scalz - Thank you for this information.

                      I'm not sure I completely get your example method, however I will do some reading of those links you provided at some point today when I have a spare 5 minutes at work, hoping to futher understand how you meant certain things.

                      When you say "Set your interrupts" i'm assuming you meant that I need to assign the interrupt pins:

                      digitalPinToInterrupt(2)
                      digitalPinToInterrupt(3)
                      

                      Is this what you meant?

                      I will then need to have two variables saved, say sw1 and sw2, write the state of the respecting pins to those variables.
                      Once i have done that, I then send the node to sleep.

                      What do you mean by disable PCINT? (sorry)
                      Perform a gw.wait(500)
                      Enable PCINT
                      Then go back to sleep.

                      Did i understand that process correctly?

                      I will be performing all of this on a arduino to make sure i understand the processes and the ordering of what to do what, then I'll get it attached to Rev2 board, once i have finished designing and ordering the PCB that is.

                      MySensors 2.1.1
                      Controller - OpenHAB (Virtual Machine)
                      Gateway - Arduino Mega MQTT Gateway W5100

                      1 Reply Last reply
                      0
                      • scalzS Offline
                        scalzS Offline
                        scalz
                        Hardware Contributor
                        wrote on last edited by scalz
                        #16

                        @samuel235: maybe I should have said "init your interrupt" or attach it. it was late lol. It was related to the External interrupt pins(PCINT). It seems digitalpinToInterrupt is new to me, I didn't know it, I used to attachInterrupt and registers when needed. I will look at digitalPintoInterrupt.
                        So when I said disable PCINT I was meaning to detach it in the interrupt routine and do your debouncing in loop.

                        Yep you have understood the global process. of course you will need to run some tests as what I said is not complete I think, just a way to go.

                        • init/ attach your interrupt(pin x)
                        • sleep mode
                        • on wake up, you will be in your ISR (interrupt service routine). Save state and detach your interrupt for the concerned pin.
                        • after the isr is executed, you will be redirected just after the gw.sleep of course. So after this line, do your debouncing by testing pin state for instance, with millis and gw.wait. Maybe you will need to adapt the delay you check pin state with millis because debouncing will depend on your switch. But this should not be a big delay, maybe 5 to 20ms, maybe more it depends on your switch. When I say delay, it is not delay() of course! but the time you use millis()
                        S 1 Reply Last reply
                        0
                        • mfalkviddM Online
                          mfalkviddM Online
                          mfalkvidd
                          Mod
                          wrote on last edited by
                          #17

                          digitalPinToInterrups just translates the pin ID to something useful for attachInterrupt. It does nothing when used alone. See
                          https://www.arduino.cc/en/Reference/AttachInterrupt

                          1 Reply Last reply
                          0
                          • scalzS Offline
                            scalzS Offline
                            scalz
                            Hardware Contributor
                            wrote on last edited by
                            #18

                            @mfalkvidd: thx ;) I am already reading it, too curious :)

                            1 Reply Last reply
                            0
                            • scalzS scalz

                              @samuel235: maybe I should have said "init your interrupt" or attach it. it was late lol. It was related to the External interrupt pins(PCINT). It seems digitalpinToInterrupt is new to me, I didn't know it, I used to attachInterrupt and registers when needed. I will look at digitalPintoInterrupt.
                              So when I said disable PCINT I was meaning to detach it in the interrupt routine and do your debouncing in loop.

                              Yep you have understood the global process. of course you will need to run some tests as what I said is not complete I think, just a way to go.

                              • init/ attach your interrupt(pin x)
                              • sleep mode
                              • on wake up, you will be in your ISR (interrupt service routine). Save state and detach your interrupt for the concerned pin.
                              • after the isr is executed, you will be redirected just after the gw.sleep of course. So after this line, do your debouncing by testing pin state for instance, with millis and gw.wait. Maybe you will need to adapt the delay you check pin state with millis because debouncing will depend on your switch. But this should not be a big delay, maybe 5 to 20ms, maybe more it depends on your switch. When I say delay, it is not delay() of course! but the time you use millis()
                              S Offline
                              S Offline
                              Samuel235
                              Hardware Contributor
                              wrote on last edited by
                              #19

                              @scalz - Right okay, i think i understand this pretty clearly now on the method to go about enabling interrupt to wake the uC.

                              I have just realized that there is a example sketch that uses sleep function for 2 switches, i can use this and modify it to work for 3 when needed. I shall be studying this sketch along with your links later on tonight hopefully.

                              Once i have sent off my deigns to get my Rev2 remade i will focus my attention to making sure i completely understand the method of this sleep with interrupt function.

                              Thank you for your help guys!

                              MySensors 2.1.1
                              Controller - OpenHAB (Virtual Machine)
                              Gateway - Arduino Mega MQTT Gateway W5100

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              25

                              Online

                              11.7k

                              Users

                              11.2k

                              Topics

                              113.1k

                              Posts


                              Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • MySensors
                              • OpenHardware.io
                              • Categories
                              • Recent
                              • Tags
                              • Popular