Hall effect sensor

hello

i got a code for a Hall effect speedometer which gives me the rpm
but when i pass the magnet(with my hands) over the sensor the value of rpm are too high more than a billion, i wonder why

i want to make a speedometer for a bicycle

thanks

const int maxCnt = 100;

void setup() {
  Serial.begin(9600);
  pinMode(hallPin, INPUT);

}

void loop() {
  unsigned long start = micros();
  int old = 1;
  int cnt = 0;
  while (cnt < maxCnt) {
    int val = digitalRead(hallPin);
    if (!val && val != old) cnt++;
    old = val;
  }
  float seconds = (micros() - start) / 1000000.0;
  float rpm = cnt / seconds * 60.0;
  Serial.print("rpm: ");
  Serial.println(rpm);
}

The hall sensor produces a voltage indicating "a magnetic field nearby." Your (unsteady) hand is slow compared to a spinning motor, and your code is reading the sensor state "more than a billion times" a minute. Make your code read when a pulse does not exist, exists, then does not exist again.

Thank you for trying to use code tags but you got it wrong. I have edited your post and corrected them

Here is the easiest way to tidy up the code and add the code tags

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Can you please provide a link to the data sheet for the hall sensor you are using?
What magnet are you using and in what orientation to the sensor?

pinMode(hallPin, INPUT);

Is there an external pull up or pull down on this pin?

Kilometers per hour? Miles per Hour? Wheel diameter (inches or cm)? Number of magnets on wheel? Which Arduino?

Not define hallPin

Try this code and tell us if it works.
I put the hall on pin 2.

( I used an Arduino UNO.)

#define hallPin 2
const int maxCnt = 100;
unsigned long start = 0;
int cnt = 0;
int val = 0;
float seconds = 0;
float rpm = 0;
bool flag = false;
//--------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  pinMode(hallPin, INPUT);
  start = micros();
}
//--------------------------------------------------------------
void loop() {
  if (digitalRead(hallPin))        // Count at hall = HIGH
  {
      cnt++;
      flag = true;
    while (digitalRead(hallPin)) {} // Do not repeat the count while HIGH
    }
  if (flag == true)
  {
    if (cnt > maxCnt) {             // Calc RPM
      seconds = (micros() - start) / 1000000.0;
      Serial.print("seconds: ");
      Serial.println(seconds);
      rpm = (cnt / seconds) * 60.0;
      Serial.print("rpm: ");
      Serial.println(rpm);
      flag =  false;
      cnt = 0;
      start = micros();
    }
  }
}

sorry for the late, i didn't had access to the computer

i tried the code adding hall pin but the rpm is still too high about thousands

i am using an arduino uno and a hall effect sensor KY-024

Many of the low cost hobby sensors have comparators based on the LM393 without any hysteresis feedback, and it is not uncommon to experience multiple interrupts/noise troubles with these basic lm393 comparator modules.

Performance is often improved with a cap between D0 and ground. That solution and other possible circuit mods for hysteresis improved response are discussed here

https://forum.arduino.cc/index.php?topic=342650.0

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