non-arduino speed sensor

I have a gps speed sensor that outputs a square wave proportional to the speed the sensor is moving. The gps puts out a 50% duty cycle square wave at 10.115 Hz/MPH. I've tried using pulseIn() to measure the square wave length but it keeps coming back as zero. What am i doing wrong? I don't have an example of my code on this computer but i can post it later if it helps. Right now all i'm trying to do is get the timing of the square wave and go from there.

Here is a link to the spec sheet

How fast were you moving when you called pulseIn()? There is a default timeout of 1 second so if you are moving less than about 1/20th mph you will get a 0 indicating timeout.

"Like all GPS receivers, the Astro requires time to acquire the satellites and stabilize before it begins to output a speed signal. The Astro includes two red lights on the interface module, (see Illustration) to indicate status of power to the Astro and the status of the GPS receiver: Power: turns on when power is applied to the unit Valid GPS: blinks when the GPS receiver is working but has not yet acquired a satellite fix. Once a fix has been acquired, it will be on steadily. If the GPS system fails, the light will turn off.

Typical startup times: • Sky search to acquire almanac = 5 minutes • Cold start = 2.5 minutes • Warm start = 30 seconds IMPORTANT NOTE: If this is the initial hookup or your Astro has been off for 4 days or more you MUST allow for a minimum of 5 minutes for the Astro to acquire a signal."

Did you have a valid GPS signal (solid Valid GPS light) at the time you tried to check speed?

I think i may have found the problem. Would I a pull down resistor? My test speed was 5-10mph. The sensor was on and working. Below is the code i have tried with the sensor. Nothing fancy just trying to see if i'm getting an input.

int inputPin=4;
long gpsPulse;

void setup() {
pinMode(inputPin, INPUT); 

void loop() {

Think I would use an interrupt routine - - and count the pulses in a certain time

(code not tested, but it should get you started)

#define TIMING 1000
volatile unsigned long count = 0;

void irq()

void setup()
  pinMode(2, INPUT);    // IRQ pin 0
  attachInterrupt(0, irq, RISING);

unsigned long lastTimer = 0;
unsigned long lastCounter = 0;
unsigned long temp;
float mph = 0;

void loop()
  if (millis() - TIMING > lastTimer)
    temp = count;

    Serial.print("\t raw counter: ");
    Serial.print("\t last second: ");
    Serial.print(temp - lastCounter);

    mph = 1.0 * (temp-lastCounter) /10115;
    Serial.print("\t MPH: ");
    Serial.print(mph, 3);  // float with 3 digits...

    // update loop administration
    lastCounter = temp;
    lastTimer += TIMING;

co2shaun: I think i may have found the problem. Would I a pull down resistor? My test speed was 5-10mph. The sensor was on and working. Below is the code i have tried with the sensor. Nothing fancy just trying to see if i'm getting an input.

I didn't see anything in the manual that says what the pulses look like. If it's a 12V signal (since the device runs on 12V) you will have a problem. If the output is "open collector" you will need a pull-up resistor. You can use the internal pull-up by adding "digitalWriat(2, HIGH);" after "pinMode(2, INPUT);".

Robtillaart, thank you for the example code.

Johnwasser, I think the signal is a square wave that goes from 0 volts to 12 volts. I don't think it is an "open collector". So my problem would be that the arduino has a max input of 5 volts and my sensor is sending it 12 volts?

Yes, if the output is up to 12VDC, you will need to use a voltage divider to bring that down to a safe voltage. For standard resistor values, I'd consider something like a 10k and 6.8k resistor combination (which makes for 4.85vdc). But if you have a big set of resistors, you can do even better than that.

Place the two resistors in series, where one end of the 10K resistor goes to the pulse output of the GPS unit and the other end is attached to one of the digital pins on the arduino. Then attach one end of the 6.8K resistor to the same digital pin and the other end to GND. You have to share GND between the GPS sensor and the Arduino as well.

While ISRs have great value, I'd consider perfecting the code first before using an ISR. That minimizes your potential points of failure and code optimization (if an ISR will do that) can happen later. Let me give you an example.

I have a power measurement board that samples two analog channels pretty much as fast as the ADC allows. This routine runs about 2x faster than the function allows because I'm addressing the ports and the ADC directly. None of the power measurement code uses a ISR because they gobble up 51 processing cycles every time they are invoked. At my sampling rate, the code would consume 510k cycles per second just to drop into an ISR... and the impact is measurable.

However, I do use an ISR to signal to the code that the time is up and that the previous set of power measurements has to be summarized and sent off to the main CPU (basically, the main CPU pulls a pin high on the measurement CPU and the ISR on the measurement CPU notices the rising edge). But that is one ISR being invoked every second, i.e. only 51 cycles per second lost or 1/10,000th of the above. So yes, ISRs are very useful but understand their costs and benefits before jumping in both feet. The more times a ISR has to be invoked, the greater the potential impact. That is why I only use the ISR as a signaling device.

If the main objective of your code is focused on measuring the pulses, it may make a lot more sense to not use an ISR, especially if the pulse trains are very fast (i.e. thousands per second). If, on the other hand, the pulse trains are slow (fewer than 1000 per second?) then using an ISR can be a great way to keep count of pulses while allowing the Arduino to focus on other things as well.

Thanks again everyone for the help I finally got it working. Turns out it was an open collector and I need to use a pull up resistor to get things working. Right now i'm the pulse in function for my code.