PulseIn() and Nuelectronics ENC28J60 Ethernet Shield

Hi folks,

I’m currently tinkering with some weather sensors and have found a strange problem when I try to add the ENC28J60 EthernetShield to my setup.

I’ve the following block to calculate the frequency of the MTF3223 (HS1101) humidity sensor:

long getFrequency(int pin)
  long freq = 0;

  for (unsigned int j = 0; j < FREQUENCY_SAMPLES; j++)
    long f = pulseIn(pin, HIGH, 250000);
    if (f == 0)
      return false;
      freq += 500000 / f;

  return freq / FREQUENCY_SAMPLES;

It works OK on a barebone Uno and 2009, but as soon as I add the enc28j60 shield the reporting frequency increases (+1khz) :cold_sweat:

This happens even if I don’t add / initialize the ethernet library.

The shield is one of these: http://cgi.ebay.com/ENC28J60-Ethernet-Shield-Arduino-Duemilanove-/250771510445

Any clues what I’m doing wrong? I’m not using any of the pins that are used by the Shield (2,10,11,12,13)

Could load or interference from the shield be making your signal asymmetric. Those shields draw a lot of current (just feel the voltage regulator).

I think your code is dependent on the duration of the ‘mark’ and ignores the space duration.

Try counting the number of pulses in a time period, rather than use the duration of the pulse. Square waves are rarely perfectly square.

Thanks for the tip, I was able to get it done via Timer1 / interrupts.