I need to record the appearance of a signal from the sensor on the PD0 pin. This is the first time in my life that I want to use an interrupt for this. With your help, I configured the sketch for my pin PD0

#include <avr/interrupt.h>

volatile int value = 0;

void setup()
  PCICR |= 0b00000100;    // turn on port d
  PCMSK2 |= 0b00000001;    // turn on pins PD0 (PCINT16) 


void loop()

ISR(PCINT2_vect)// Port D, PCINT16 - PCINT23

enter image description here

I need to wait and take a certain action when a high signal comes from the sensor. For example, add one to the constant.enter image description here I also did not understand when interruptions occur. When the signal changes from bottom to top, when from top to bottom, or no matter how? Can't see if these settings are here? Maybe it is set somehow by default?

  • 1
    It is unclear, what exactly your problem is. In the example the used pin is pinBlink with the value 7. You can change it with the define at the start of the sketch – chrisl Nov 28 '20 at 11:01
  • The pin numbers of the TQFP package are irrelevant here. You just need to know that pin “PD0” of the ATmega328P is “digital 0” of the Arduino Uno and Arduino core library. – Edgar Bonet Nov 28 '20 at 11:14
  • PD0 is Serial RX and pin 0 on Uno/Nano/Mini. why do you want use this pin? – Juraj Nov 28 '20 at 11:15
  • why not use "external interrupt" peripheral? – Juraj Nov 28 '20 at 13:33
  • I dont know. It seems that this is how it works, but I don't understand how it works. Which edge of the pulse. I turned on the button instead of the sensor, but I still can't understand – Антон Nov 28 '20 at 13:39

Here is a link to a good tutorial on how to enable, respond to, and clear interrupts on the ATMega328P device:

Arduino Pin Change Interrupts

I'll repeat the overall steps here in case this URL disappears in the future:

First, you must enable pin change interrupts:

Pin change Interrupts

Note the use of 3 bits in the PCICR register [0:2]. These enable pin change interrupts on ports B, C, or D in general. You can choose any combination of these ports or all of the ports as you desire.

Next, you select which pin or pins on the enabled ports that you want to receive interrupts from. You have control here over each individual port bit:

Pin Change Bits

Then you must write an ISR (Interrupt Service Routine) to handle the interrupt once it occurs. You have the option of up to THREE ISRs, one to handle each port or you can have a single ISR to handle any pin change interrupt. The way this works is that when an interrupt happens on an enabled port and pin, the AVR chip calls the ISR that you have configured for that event:


So when an interrupt happens, how do you know which pin(s) caused the ISR? Let's assume you used a single ISR to handle all 3 ports. At this point you know only that an interrupt happened somewhere. That's the purpose of the PCIFR register bits. From the AVR datasheet we see this:


By checking the status of these bits you can now tell which pin or pins caused the interrupt. Note that there CAN BE MORE THAN ONE, so it's up to you to decide the priority of each one and handle them accordingly. Note that you only get ONE chance to read these during any ISR function call since they get cleared when the ISR executes.

One last note. You MUST call the sei(); function to enable any interrupt to the CPU before any interrupt will happen.

In your example, however, you seem to be attempting to generate a pin change interrupt on an output. This is probably NOT what you want. Try simply using the existing LED output pin as an indicator and choose a different pin, setup as INPUT, to be your pin change interrupt source.

  • Thanks! I got something. The counter increases the number when I touch the wire coming out of the PD0 pin with my hand. But I still do not understand on what front or how it works like that – Антон Nov 28 '20 at 13:22
  • the original question used a library for pin change interrupts. how does this answer answer the question? – Juraj Nov 28 '20 at 13:32
  • @Juraj I see no library being used. – jwh20 Nov 28 '20 at 14:58
  • the question was edited – Juraj Nov 28 '20 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.