I'm trying to make this project.

But i have a problem running this code. It appears to be val = digitalRead(inPin); breaks entire program. just because i call this from the callback function which attached as an interrupt function Timer1.attachInterrupt(callback);

Here is the more simplified problem simulation program,

#include "TimerOne.h"

int inPin = 7;   // sensing digital pin 7

int val;

void setup() {
  pinMode(inPin, INPUT);      // sets the digital pin 7 as input to sense receiver input signal

  Timer1.initialize(8);  // initialize timer1, and set the frequency; this drives both the LC tank as well as the pulse timing clock
  // note: modify this as needed to achieve resonance and good match with the desired tags
  // the argument value is in microseconds per RF cycle, so 8us will yield RF of 125kHz, 7us --> 143kHz, etc.

  Timer1.pwm(9, 512);


void callback()
  val = digitalRead(inPin);//once you comment this line, it works fine

void loop() {
  Serial.print("Program running \n");


Any help is much appreciated.

  • 2
    I give you +1 point for taking care of reducing your code to the minimum that show the problem.
    – user31481
    Commented Jan 24, 2018 at 16:10

1 Answer 1


The time between 2 interrupt triggers is only 8us. Also digitalRead() is a quite big function, that needs some time to execute (I don't know how much). That way it is to short for the Serial print command to execute in the leftover time. Make the following change and you will see, that it works.

void loop(){
  Serial.print("Program running \n");

Now the interrupts are disabled during the execution of the Serial.print(), so that it can finish. After this the interrupts are enabled again and we wait for 1 second to give the interrupts some time before we are disabling them again.

The speed of the Arduino is limited, so you cannot do much, when constantly triggering interrupts in the pace of 8us. If you only want to check this digital input you can get the value directly with using the PINB registers (refer to this side), which works much faster.

Also you should consider making the variable val volatile, since it will can change everytime through your interrupt service routine.

  • Thanks mate, It all makes sense. And i'll try this out and let you know how it went. Still i wonder how the tutorial worked for them at some point, Perhaps it was an older Arduino core? Commented Jan 25, 2018 at 8:15
  • I'm not sure about how the tutorial worked for them. On a quick look I see, that they are not sending serial in every loop cycle, but only when special conditions arrive. Not sure about how this changes the problem. To be sure you would have to measure the length of the interrupt routine with an oszilloscope
    – chrisl
    Commented Jan 25, 2018 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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