Timing Hall Effect Sensor on-off

I'm trying to time a hall effect sensor on-off when a magnet passes. All I get from the output is "0". Tried a number of things but no good results. Sensor is a 44E001. Thanks in advance for the help. Sketch is below:

int oldTime;
const byte interruptPin = 2;
int elapsedTime;

void setup() {
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), test, RISING);
  Serial.begin(9600);
}

void loop() {
oldTime = millis();

}

void test() {
  elapsedTime = millis() - oldTime;
  Serial.print(" elapsedTime =");
  Serial.print (elapsedTime);
  delay(50);
}

millis() won't increment in an interrupt routine. Just set a flag and check in main loop.

...and you shouldn't use delay() in an interrupt either.

1 Like

If the suggestions of @red_car do not solve your issues:

Google finds nothing for that number. Post a data sheet for that sensor.

Please post a schematic of the project. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

And here are some tutorials on using millis() for timing:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

Don't use Serial.print in an ISR either. Serial.print depends on interrupts and interrupts are disabled in an ISR.

there's no need for millis() to increment within the interrupt.
won't the interrupt be able to capture the current value of millis()?

consider (compiles, but not tested)

const byte interruptPin = 2;

byte          cnt;
unsigned long msec;
unsigned long msec0;
unsigned long msecLst;

void test()
{
    msec0 = msec;
    cnt++;
}

void loop()
{
    msec = millis();

    if (cnt)  {
        Serial.println (msec0 - msecLst);
        msecLst = msec0;
        cnt     = 0;
    }
}

void setup() {
    pinMode(interruptPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(interruptPin), test, RISING);
    Serial.begin(9600);
}

msec0 and cnt should be declared volatile.

1 Like

@PMLAPL
In addition to the other answers - your function will never gives correct elapse time because you do not store anywhere the oldTime value.

Why use an interrupt at all? Use StateChangeDetection

const byte hallInputPin = 2;

void setup() 
{
  pinMode(hallInputPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() 
{
  static unsigned long prevMillis = millis();
  static int prevHallState = digitalRead(hallInputPin);
  unsigned long currMillis = millis();
  int currHallState = digitalRead(hallInputPin);
  
  if (currHallState != prevHallState)
  {
    prevHallState = currHallState;
    if (currHallState == HIGH)
    {
      // LOW to HIGH transition.  Start timing.
      prevMillis = millis();
    }
    else
    {
      // HIGH to LOW transition.  Calculate Elapsed Time.
      Serial.print("Elapsed time (ms) = ");
      Serial.println(prevMillis - currMillis);
    }
  }
}

I've been trying to learn how to use an interrupt routine since I never used one. I've given up and went back to using state-change that I used to check an IR sensor in a previous sketch. I just switched to a hall effect sensor. Thanks for all the help. Here's my final code that works fine:

unsigned long startTime;
const byte hallInputPin = 5;
unsigned long elapsedTime = 0;
unsigned long Count = 0;
byte Sense;
byte lastSense;

void setup() {
  pinMode(hallInputPin, INPUT);
  startTime = millis();
  Serial.begin(9600); 
}

void loop() {
  Sense = digitalRead(hallInputPin);
  if (lastSense != Sense) {  
    lastSense = Sense;       
    if (Sense == LOW) {      
      Count = Count + 1;
    }
  }
  if (Count == 2) {
    elapsedTime = millis() - startTime;
    startTime = millis();
    Serial.print("Elapsed Time ");
    Serial.print(elapsedTime);
    Serial.println(" milliseconds");
    Count = 0;
  }
}

nope - the calculated time has nothing to do with the interval between pulses, because you save the startTime in incorrect moment
You should store the millis() to the startTiime when count =1

I think you might be right. There are two magnets on the shaft so then if I check time after three pulses, I should get the time for a full revolution. Thanks for the suggestion.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.