Hi. I just started with Arduino last week and am loving it so far.
For this project, I'm trying to build a simple speed radar and am at a sticking point. Would love some help.
The sensor is a 10.525 GHz X-band doppler sensor: http://docs.microwave-solutions.com/createPdf.php?id=MDU1720
It outputs the doppler shift frequency.
I'm using an LM386 to amplify the signal with a gain of 200: http://www.ti.com/lit/ds/snas545a/snas545a.pdf (page 5, top right)
I've adapted my code from this tutorial's main code. And the second code example here.
My code currently looks like this:
// These are the counts needed for 250 msec interrupt
// using the timer prescaler settings set to 128.
const uint16_t TICK_CNT = 34286; // 65536-(16MHz/128/4Hz)
static uint16_t freq = 0;
double sped = 0; //"speed" seems to be a reserved term
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(115200);
noInterrupts(); // disable all interrupts while we configure
// init Timer1 - 16-bit timer/counter
TCNT1 = 0; // start count at zero.
TCCR1B |= _BV(CS12) | _BV(CS11) | _BV(CS10); // Increment T1 input on each positive edge
// using an external source. Table 16-5, pg 139.
// init Timer2 - 8-bit timer/counter
TCNT2 = TICK_CNT; // preload Timer2 to interrupt every 250 msec
TIMSK2 = _BV(TOIE2); // enable the Timer2 overflow interrupt
TCCR2B |= _BV(CS22) | _BV(CS20); // init clock prescaler to 128. Table 18-9, page 164.
interrupts(); // enable all interrupts
Serial.println("Ready...");
}
ISR(TIMER1_OVF_vect) {
// do nothing. this is just a dummy ISR in case it actually overflows.
Serial.println("Inside Timer1 Overflow Interrupt.");
}
ISR(TIMER2_OVF_vect) {
//Serial.print("TCNT1: ");
//Serial.println(TCNT1);
freq = TCNT1;
//Serial.println(freq);
TCNT1 = 0;
TCNT2 = TICK_CNT;
}
void loop() {
if (freq != 0) {
freq = freq << 2; // multiple the frequency * 4 (using leftshift 2 places). 250ms*4 = 1 sec.
sped = freq * .03225; // multiplying freq * 0.0325 will give speed in mph. 31Hz == 1 mph.
// see: http://www.microwave-solutions.com/contents/en-uk/d13_System_Design.html
Serial.print("Freq: ");
Serial.print(freq, DEC);
Serial.print(" Hz, Speed: ");
Serial.print(sped, 3);
Serial.println(" mph");
}
}
I've found that the only pin that gives any output when the output of the amplifier is connected to it is Digital pin5. (I'm not sure why this is, and would love to know.)
When I wave my hand in front of or near the sensor, my output looks like this:
Ready...
Freq: 4 Hz, Speed: 0.129 mph
Freq: 16 Hz, Speed: 0.516 mph
Freq: 4 Hz, Speed: 0.129 mph
Freq: 16 Hz, Speed: 0.516 mph
Freq: 4 Hz, Speed: 0.129 mph
Freq: 16 Hz, Speed: 0.516 mph
Freq: 16 Hz, Speed: 0.516 mph
Freq: 64 Hz, Speed: 2.064 mph
Freq: 4 Hz, Speed: 0.129 mph
Freq: 4 Hz, Speed: 0.129 mph
Freq: 16 Hz, Speed: 0.516 mph
Freq: 4 Hz, Speed: 0.129 mph
Couple of questions:
-
Why Digital Pin 5? I thought it would've been one of the analog pins.
-
I'm pretty sure I'm waving my hand faster than 2mph. Why does my output give such low speeds? When I hook up my multimeter, set to Hz, to the output of the amplifier, I'm getting readings > 100 Hz when I wave my hand. (I also get a standing frequency of 100Hz when connected to the output and 60Hz with just the ground connected and the positive floating in the air.)
-
From my understanding of what the code is supposed to be doing, it has set Timer1 to count rising edges from an input. Timer2 has been configured to wait 250ms, then interrupt and read the # of pulses into freq variable from Timer1, reset Timer1 back to 0, and preload to interrupt again in 250ms. Is my understanding correct?
-
Is there something wrong with my TICK_CNT (preload amount) and prescaler selection that is causing me to not count all of the pulses?
Thanks in advance!