Hall Sensor Counter works before adding shield on unused pins then erratic.

Hello,

I was setting up a basic hall sensor connected to A0 and 5v/gnd with D2 to A5/D19 for the interrupt with some basic code seems to work fine:

volatile int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis = 0;
const int analogPin = A0;
const int digitalPin = 19;
const int threshold = 700;

void setup()
 {
   Serial.begin(9600);
   pinMode(digitalPin,OUTPUT);
   digitalWrite(digitalPin,HIGH);
   attachInterrupt(0,rpm_inc,FALLING);
 }

void loop(){
  int sensorValue = analogRead(analogPin);
  if (sensorValue < threshold)
   {
     digitalWrite(digitalPin,LOW);  
   }
   else
   {
     digitalWrite(digitalPin,HIGH);
   }
  if (millis() - lastmillis == 1000){
    detachInterrupt(0); //Disable interrupt when calculating
    Serial.print("RPM =\t"); //print the word "RPM" and tab.
    Serial.println(rpmcount); // print the rpm value.
    lastmillis = millis(); // Uptade lasmillis
    attachInterrupt(0, rpm_inc, FALLING); //enable interrupt
  }
}

void rpm_inc(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
rpmcount++;
}

Then I add my DK Electronics Motor Shield 1.0 with no motors yet and no code change and connect the hall sensor to the breakout pins on the DK shield, same A0 5v/GND and D2 to A5/D19 with no code change and the serial output of the sensor does not function the same. Not sure why.

Without the shield on it counts up no problem with each magnet pass to the hall sensor… 1…2…3…4…5…6…7…8…9…10 and so on…

With the DK motor shield, same code and wiring (through the shield now) it has erratic behavior initially counting 1…2… 4… before eveing swiping the magnet in front of the sensor, and then when the magnet is swiped it counts it for multiple erratically changing and often randomly adding 1 or 2 when nothing is moved or near sensor.

Anyone have any thoughts what it may be or what to check?

Just wanted to see if anyone had any suggestions why this may happen?

From the code sample above there are several problems. attachinterrupt operates from different pins depending on Arduino model. On the Uno interrupt 0 is on pin 2 https://www.arduino.cc/en/Reference/attachInterrupt

To use pin A5/D19 you would need to use on of the expanded interrupt libraries such as PinChangeInt http://playground.arduino.cc/Main/PinChangeIntExample

Your interrupt pin should be an input

Hi, Check the reference for attachInterrupt(). It will tell you which pin to use for interrupt depending which Arduino board your are using. On the hall effect sensor you must be careful when using it next to an electric motor. If they are too close the magnetic field from the motor will interfere with the hall effect sensor. I had the same problem and I had to switched to an optical sensor. No magnetic interference with the optical sensor.