Reading Square Wave Interrupts

Hi all, I'm having difficulty with my arduino detecting an interrupt. The signal I'm looking to capture is a Square wave, switch to ground signal. Right now I have a second arduino outputting a HIGH/LOW pulses from a PWM pin through a 1k resistor into the interrupt 0 pin on the original arduino.

I tested the secondary, signal generating arduino, with an LED to verify it is outputting a pulse (anywhere from 1hz to 50hz).

Any ideas how I can detect a simple DC pulse?

Thanks,

Mike.

High +5v -------- | | | | Low GND -- --------

I’m having difficulty with my arduino detecting an interrupt.

What kind of difficulty? What are the symptoms? Do you have any code you can post to see how you are trying to perform the task?

Lefty

Here is the code for the original arduino to read the pulse:

 volatile byte rpmcount;
 unsigned int rpm;
 unsigned int tach;
 unsigned long timeold;
 
 
 void setup()
 {
   Serial.begin(9600);
   attachInterrupt(1, rpm_counter, FALLING);
   rpmcount = 0;
   rpm = 0;
   timeold = 0;
   tach = 0;
  
  pinMode(5,OUTPUT);    //  Green   LED
  pinMode(6,OUTPUT);    //  Green   LED
  pinMode(7,OUTPUT);    //  Green   LED 
  pinMode(8,OUTPUT);    //  Yellow   LED
  pinMode(9,OUTPUT);    //  Yellow   LED
  pinMode(10,OUTPUT);   //  Yellow  LED
  pinMode(11,OUTPUT);   //  Red     LED
  pinMode(12,OUTPUT);   //  Red     LED
  
 }
 
 
 
 void loop()
 {
   if (rpmcount >= 2) { 
     //Update RPM every 2 counts, increase this for better RPM resolution,
     //decrease for faster update
     rpm = 2*60*1000000/(micros() - timeold)*rpmcount;
     timeold = micros();
     rpmcount = 0;
     Serial.println(rpm,DEC);
     tach_state();
   
     
     
     switch(tach)
     {
       // cases go here, they were omitted for internet posting
     }
     
     
     
   }
 }
 
 
 void rpm_counter()
 {
   rpmcount++;
   //Each rotation, this interrupt function is run ONCE.
 }
 
 
 
 void tach_state()
 {
  if(rpm<=250){tach=0;}
  else if(rpm>500 && rpm<=2000){tach=1;}
  else if(rpm>2000 && rpm<=4000){tach=2;}
  else if(rpm>4000 && rpm<=6000){tach=3;}
  else if(rpm>6000 && rpm<=8000){tach=4;}
  else if(rpm>8000 && rpm<=10000){tach=5;}
  else if(rpm>10000 && rpm<=12000){tach=6;}
  else if(rpm>12000 && rpm<=14000){tach=7;}
  else {tach=8;};
 }

The secondary arduino, generating the signal:

int analogPin = 0;     // potentiometer wiper (middle terminal) connected to analog pin 0
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read
int signalPin = 9;
int value = 0;

void setup()
{
  Serial.begin(9600);          //  setup serial
}

void loop()
{
  val = analogRead(analogPin);    // read the input pin
  Serial.println(val);             // debug value
  
  value = (val)/10;
  
  Serial.println(value);
  
  digitalWrite(signalPin,HIGH);
  delay(value);
  digitalWrite(signalPin,LOW);
  delay(value);
}

The problem is that the original arduino doesn’t run the interrupt at all, even though the secondary arduino is pulsing high (+5V) and GND.

change this: attachInterrupt(1, rpm_counter, FALLING); to attachInterrupt(1, rpm_counter, RISING);

and also add interrupts(); after that line.

You do have a ground wire between the two arduino boards? It is required.

Also:

rpm = 2*60*1000000/(micros() - timeold)*rpmcount;

Looks like trouble to me as you are mixing long constants with a int result? I'm still weak on the software side of things but I've seen people use things like 1000000UL as a specific cast to force correct results.

Lefty

unfortunately changing it to a rising edge didn't make a difference.

I have tested the original arduino's code (the one sensing the pulses) on a function generator outputting a AC square wave and it worked just fine.

The problem is that the second arduino can only output a DC (positive to ground) square wave, which isn't being read by the other arduino's interrupt

Again I ask, You do have a ground wire between the two arduino boards? It is required.

They're directly grounded to eachother.

Would a PulseIn() command work any differently than an interrupt?

Don’t you need this at the top of the sketch?

#include <avr/interrupt.h> // interrupts library

CrossRoads:
Don’t you need this at the top of the sketch?

#include <avr/interrupt.h> // interrupts library

Oh my good Lord that might have just worked. 10 hours of troubleshooting before that…I’m not overly optimistic yet…but all of the sudden it’s working. I’ll report back my finding tomorrow. Thank yoU!!!

I had the same problem trying to get PCInts to work - trying all kinds of stuff. Finally gave up and went to external interrupt - still no go! No one seemed to notice in the forum either. Finally came across that while looking at some code else where - worked after adding that.

Good luck.