Hello,
I'm using the HB100 sensor to measure the speed of an approaching object.
I built amplifying and filter circuit based on the example presented here:
After the comparator, the output is a nice square signal (images attached - yellow - signal at the IF output of the sensor, blue - signal after the amplifier and filter, purple - signal after the comparator) when the sensor detect movement.
I'm using the following code:
#define MainPeriod 10
long previousMillis = 0; // will store last time of the cycle end
volatile unsigned long duration=0; // accumulates pulse width
volatile unsigned int pulsecount=0;
volatile unsigned long previousMicros=0;
void setup()
{
Serial.begin(19200);
attachInterrupt(6, myinthandler, RISING);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= MainPeriod)
{
previousMillis = currentMillis;
// need to bufferize to avoid glitches
unsigned long _duration = duration;
unsigned long _pulsecount = pulsecount;
duration = 0; // clear counters
pulsecount = 0;
float Freq = 1e6 / float(_duration);
Freq *= _pulsecount; // calculate F
// output time and frequency data to RS232
Serial.print(currentMillis);
Serial.print(" "); // separator!
Serial.print(Freq);
Serial.print(" ");
Serial.print(_pulsecount);
Serial.print(" ");
Serial.println(_duration);
}
}
void myinthandler() // interrupt handler
{
unsigned long currentMicros = micros();
duration += currentMicros - previousMicros;
previousMicros = currentMicros;
pulsecount++;
}
to measure the frequency. it's working for frequencies that are higher than 1.5kHz for a square wave that is generated by signal generator.
The problem is when I input the signal at the comparator output to pin 6 of my Arduino Due (and looking at the signals through a scope) the signals are getting very noisy, thus I can't measure the frequency accurately (when I disconnect the signal from pin 6 it all goes back to normal).
Any idea how to fix this?
Thanks.