Strange behavior

I’m writing an app to record data from an anemometer and am getting some strange behavior. For some reason whenever the input signal gets low enough in pulse width to generate a MPH of 0.0, the “peak” value is set to zero! this makes so sense. :-[


* Windspeed measurement sketch using NRG 40C anemometer whose signal has been
* massaged into a TTL square wave.
* Coded by: Joseph Lane. 06/30/2010

#include <EEPROM.h>

// takes an address and a generic type and writes to eeprom
template <class T> int EEPROM_writeAnything(int ee, const T& value)
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        EEPROM.write(ee++, *p++);
    return i;

template <class T> int EEPROM_readAnything(int ee, T& value)
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        *p++ =;
    return i;

double sensorOne, peak = 0.0;
const int PEAKADDY = 0;

void setup()
  // start serial port at 9600 bps:
  pinMode(2, INPUT);   // digital sensor is on digital pin 2
 // EEPROM_readAnything(PEAKADDY, peak); // Get stored highest

void loop()
  sensorOne = pulseIn(2, LOW); // LOW because square wave signal is inverted
  sensorOne = 1 / ((sensorOne / 1000000) * 2); // half period to hz
  sensorOne = (sensorOne * 1.711) + 0.78; // hz to mph

  if (sensorOne > peak) {
    peak = sensorOne;
 //   EEPROM_writeAnything(PEAKADDY, peak); // write new peak to eeprom
    Serial.println("NEW PEAK!");
  // send sensor values:
  Serial.print("MPH: "); 
  Serial.print("  PEAK:");
  //delay(1000); // pause 1 second  


MPH: 2.54  PEAK:2.54
MPH: 2.60  PEAK:2.60
MPH: 2.69  PEAK:2.69
MPH: 3.00  PEAK:3.00
MPH: 2.88  PEAK:3.00
MPH: 3.03  PEAK:3.03
MPH: 3.42  PEAK:3.42
MPH: 3.68  PEAK:3.68
MPH: 4.15  PEAK:4.15
MPH: 4.48  PEAK:4.48
MPH: 4.87  PEAK:4.87
MPH: 5.27  PEAK:5.27
MPH: 4.87  PEAK:5.27
MPH: 4.80  PEAK:5.27
MPH: 4.17  PEAK:5.27
MPH: 4.00  PEAK:5.27
MPH: 3.55  PEAK:5.27
MPH: 3.46  PEAK:5.27
MPH: 3.04  PEAK:5.27
MPH: 2.72  PEAK:5.27
MPH: 0.00  PEAK:0.00
MPH: 2.09  PEAK:0.00
MPH: 0.00  PEAK:0.00
MPH: 2.38  PEAK:0.00
MPH: 2.40  PEAK:0.00
MPH: 0.00  PEAK:0.00
MPH: 2.42  PEAK:0.00

I dont see anything wrong that is obvious. Weird indeed.

There is one tip however, notice that when it gets that 0.0 value, it also detects a new peak.

My guess would be that this new peak is probably so big, that it cant be printed correctly, as the Arduino does not print in exponential format.

So choose a sensible maximum, like 10000 perhaps, and discard any of those 'bogus' values, and just take the next sample.

Hope this helps :)

Thank you for your reply and i will investigate further but my hope is and first instinct is that should not be the problem. Its not very windy outside so even a gust shouldn't generate a value faster than a few double digits.

It could be caused by anything.

I think it's like a bouncing button.

Perhaps add a test is code to check if returned value is 'out of expected range'.


Once it hits the peak 0.00 state it seems pooched. The next loop when you get the 2.something the peak isn't reset to 2.something. weird.

I would be interested in what the values for peak and sensorOne are before the compare sensorOne > peak.

Either way I probably would just do your process if sensorOne > 0 after the sensorOne = pulseIn(2, LOW); line.

Figured it out. Thanks to all that helped. It was indeed a divide-by-zero issue.

Now that I fixed the dbz I found a new problem. Random jumps in wind speed that are not gusts. It will be steady at 5 mph then jump once or twice to 30-ish mph. My circuit is the anemometer going into a low pass then to a 2 diode limiter and then into an op-amp as a comparator with a threshold. Then finally a hex inverter to get the output to a full ttl level. So is the problem likely hardware or the pulsein function?

Here's a circuit diagram:

I was talking with some people and they seem to believe that it's most likely RF noise. Anyone care to take a stab at improving my circuit?