HELP NEEDED! Tachometer with magnetic hall sensor

I'm makeing an tachometer-like device.
The exact RPM is not needed.
The only mission of that device is to turn on LED when rpm is 0 and off when it isn't
As a sensor i use A3144 Hall effect sensor

Everything works fine but with one error:
If the magnet is stopped in front of the hall sensor (the sensor pin is pulled to LOW) there is no change on the sensor pin and my nano does not light the LED Up!

Hope that it's not hard to understand me.
How can i fix this in my code?

int hallPin = 2; // A3144 sensori signaali pin
int ledPin = 13; // LED-i pin
int ledPin2 = 12; // Nanol ei ole pin 13, vaid füüsiline led
//siia on vaja joota armatuurlaua led

void setup() {
  pinMode(hallPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int rpm = getRPM();
  if (rpm == 0) {
    digitalWrite(ledPin, HIGH);
  } else if (rpm > 50) {
    digitalWrite(ledPin, LOW);
  }
}

int getRPM() {
  int count = 0;
  unsigned long startTime = millis();
  while (millis() - startTime < 100) {
    if (digitalRead(hallPin) == LOW) {
      count++;
    }
  
  }
  int rpm = count * 600 / 7.5;
  return rpm;
}```

the only way round that is to detect 'state change' and not 'steady state value' as you code currently does by 'polling' the input pin.

none-interrupt example:
https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection/

using interrupt on change examples:
https://electronoobs.com/eng_arduino_tut132.php

hope that helps...

I'd look into the button debounce example:

https://docs.arduino.cc/built-in-examples/digital/Debounce/

and use this part to detect the hall sensor signals and record their times:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
}

...and then turn the LED on or off depending on millis() - lastDebounceTime

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