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.
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