Electronic – Correct usage of a pin change interrupt


I'm trying to use pin change interrupts to detect pressed buttons. Until now I have never worked with these kind of interrupts and there are some problems, so I want to make sure if this is the correct usage.

If I got the datasheet right the following things must be done to use a pin change interrupt:

  1. Set which PINs you want to control in the PCMSK register
  2. Enable the PINs register for pin change interrupt control (PCICR)
  3. Enable interrupts
  4. Use the corresponding interrupt vector

Project: Simple Moodlamp, Colors controlled via 4 Buttons.


  • Atmega168A-PU
  • 4 mini push button switches
  • MOSFETS to control my 3 Watt RGB LED

Here is the code I'm using which is not working as expected:

#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define BUTTON1 (1<<PC5) 
#define BUTTON2 (1<<PC4) 
#define BUTTON3 (1<<PC3) 
#define BUTTON4 (1<<PC2) 

#define GREEN   (1<<PB1) 
#define BLUE    (1<<PB2) 
#define RED     (1<<PB3) 

void init() {

        // enable LED
        DDRB |= GREEN;
        DDRB |= BLUE;
        DDRB |= RED;

        // button pullups
        PORTC |= BUTTON1;
        PORTC |= BUTTON2;
        PORTC |= BUTTON3;
        PORTC |= BUTTON4;

        // pin change interrupts for buttons
        PCMSK1 |= PCINT13;
        PCMSK1 |= PCINT12;
        PCMSK1 |= PCINT11;
        PCMSK1 |= PCINT10;

        // enable pin change for buttons
        PCICR |= PCIE2;



ISR(PCINT2_vect) {

                PORTB = BLUE;

void ledTest() {

                PORTB ^= RED;
                PORTB ^= RED;
                PORTB ^= RED;
                PORTB ^= RED;

                PORTB ^= BLUE;
                PORTB ^= BLUE;
                PORTB ^= BLUE;
                PORTB ^= BLUE;

                PORTB ^= GREEN;
                PORTB ^= GREEN;
                PORTB ^= GREEN;
                PORTB ^= GREEN;

int main() {


        PORTB |= GREEN;

        while(1) {

Note: The buttons should be debounced. Since I'm trying to this step by step and it shouldn't mater for turning on the LED, I ignored it here.

Question: Is the way I'm trying to use the interrupts correct?

Problems with my setup:

  • Buttons1-3 are totally ignored.
  • Button4 is triggering a reset of the atmega

Things I checked:

  • Buttons are in no way connected to the reset PIN
  • Buttons are properly connected to GND if pressed
  • Buttons are not connected to GND if not pressed
  • Buttons work nicely if I utilize them without an interrupt, e.g.:

    if(!(PINC & BUTTON4)) {
    PORTB ^= BLUE;

  • 16MHZ external crystal / internal crystal
  • Any errors in the routing
  • I'm using a 100nF capacitator between PWR and GND on the atmega
  • VCC(7), GND(8), GND(22), AVCC(20) are connected (since I don't need AREF, its not connected)

Best Answer

Pin change interrupts are usually not a good way to detect button actions. This is because mechanical buttons bounce, and you will get lots of meaningless interrupts, and then you still have to do debouncing anyway.

A better way is to have a periodic interrupt, like every 1 ms (1 kHz rate). That's a long time on most processors, so the fraction of time spent in the interrupt will be small. Simply sample the button state every interrupt. Declare a new button state if you have seen the new state 50 ms in a row. 50 ms is longer than most buttons bounce, but is still short enough so that humans won't notice or care about the lag.

Note that this way you can also handle multiple buttons in the same periodic 1 ms interrupt. All you need is one counter for each button.

More on debounce time:

Occasionally, as in this case, someone says 50 ms is too long a debounce time. This is not true for ordinary buttons pressed by humans. It might be a issue perhaps in very timing-critical applications like a stopwatch, but so far I haven't run into one. I did test on this in the early 1980s, and lots of other people have too.

It is true that typical pushbutton bounce time is around 10 ms, with just about all settling by 25 ms. The limiting factor on debounce time is human perception. 50 ms is a bit shorter than where people start to notice a delay when they aren't looking for it. Even then, it takes a much longer time for it to be annoying. It can be possible in some cases for a human to detect a difference between 50 ms and 0 ms delay if they are specifically looking for it, but that is quite different from pushing a button and seeing something happen and not thinking about the delay.

50 ms is therefore a good debounce time because the delay is below the perception limit in ordinary applications, way below the annoyance limit, and well above the bounce time of most switches. I have found switches that did bounce for nearly that long, so you might as well push to the perception limit since there is nothing to loose.

I have done many products with firmware-debounced buttons using 50 ms debounce time. Not once did a customer mention even noticing a delay. They all accepted the buttons as working fine without issue.

Related Topic