Reading pulses from Microwave

If I am looking at this chip correctly, it simply pulses on and off when motion is detected using the doppler effect. My goal is to read the pulses, divide by 4 and it will give me the RPM of a fan blade going max 600RPM. I can't seem to get the serial to print anything other than 0.

int pin = 3; 
volatile unsigned int pulse; 

void setup() 
{ 
Serial.begin(9600); 
 
pinMode(pin, INPUT); 
attachInterrupt(0, pulse, RISING); 
} 
 
void loop() 
{ 
pulse=0; 
interrupts(); 
delay(1000); 
noInterrupts(); 
 
Serial.print("Pulses per second: "); 
Serial.println(pulse); 
void count_pulse(); 
}

Any help is appreciated, thank you

Bring uo the Arduino Reference and look at how Interrupt is arranged.

To be honest first time I'm using interrupts. Copied the code from an "Internet of things" article and made a few modifications (they were counting pulses from a flow meter and converting to Liters per minute.

https://subscription.packtpub.com/book/web_development/9781785888564/3/ch03lvl1sec27/reading-and-counting-pulses-with-arduino

I modified your code a bit:

  • you should use digitalPinToInterrupt() to set the interrupt number in the call to attachInterrupt()
  • you need to specify an interrupt handler (aka interrupt service routine or ISR) as the 2nd parameter in attachInterrupt()
  • you should declare pins as "const" -- they're not variables and you don't want them changing
  • you should use more descriptive names for variables, pins etc
  • unless otherwise required, I use INPUT_PULLUP on digital inputs

Compiles, not tested:

const uint8_t pinInput = 3;
volatile unsigned int pulse;

void setup()
{
    Serial.begin(9600);
     
    pinMode(pinInput, INPUT_PULLUP );
    attachInterrupt(digitalPinToInterrupt(pinInput), isrPulse, RISING);    
    
}
 
void loop()
{
    pulse = 0;
    interrupts();
    delay(1000);
    noInterrupts();
 
    Serial.print("Pulses per second: ");    
    Serial.println(pulse);  

}

void isrPulse( void )
{
    pulse++;
    
}//isrPulse

Blackfin:
I modified your code a bit:

  • you should use digitalPinToInterrupt() to set the interrupt number in the call to attachInterrupt()
  • you need to specify an interrupt handler (aka interrupt service routine or ISR) as the 2nd parameter in attachInterrupt()
  • you should declare pins as "const" -- they're not variables and you don't want them changing
  • you should use more descriptive names for variables, pins etc
  • unless otherwise required, I use INPUT_PULLUP on digital inputs

Compiles, not tested:

const uint8_t pinInput = 3;

volatile unsigned int pulse;

void setup()
{
   Serial.begin(9600);
   
   pinMode(pinInput, INPUT_PULLUP );
   attachInterrupt(digitalPinToInterrupt(pinInput), isrPulse, RISING);    
   
}

void loop()
{
   pulse = 0;
   interrupts();
   delay(1000);
   noInterrupts();

Serial.print("Pulses per second: ");    
   Serial.println(pulse);

}

void isrPulse( void )
{
   pulse++;
   
}//isrPulse

You're a life saver, thank you!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.