Interrupt Hall Sensor Rotation Speed Calculation

Dear all,
i am currently trying to setup a rotation speed calculation for a small DC motor. I am using:

  • ESP8266 nodemcu v3
  • 12 V DC Motor
  • Hall sensor ("Hall switch")
  • Small magnet in the DC Motor shaft

I wrote / copied the following code:

const uint8_t interruptPin = 5; //D1
volatile byte interruptCounter = 0;
volatile unsigned int isrMillis;
int numberOfInterrupts = 0;
int rpm = 0;
unsigned int timeold;
void ICACHE_RAM_ATTR handleInterrupt();

void setup() {

  Serial.begin(9600);
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);

}

void handleInterrupt() {
  interruptCounter++;
  isrMillis = millis();
}

void loop() {

  if(interruptCounter>0){

      interruptCounter--;
      numberOfInterrupts++;

      Serial.print("Total: ");
      Serial.print(numberOfInterrupts);
      Serial.print(" // Time: ");
      Serial.print(isrMillis-timeold);
      Serial.print(" // RPM: ");

      //rpm = 1/((isrMillis-timeold)/(60*100));

      Serial.println(rpm);

      timeold = millis();
  }

}

This code seems to work fine:

14:31:48.545 -> Total: 2 // Time: 528 // RPM: 0
14:31:49.062 -> Total: 3 // Time: 527 // RPM: 0
14:31:49.609 -> Total: 4 // Time: 527 // RPM: 0
14:31:50.120 -> Total: 5 // Time: 525 // RPM: 0
14:31:50.665 -> Total: 6 // Time: 524 // RPM: 0
14:31:51.179 -> Total: 7 // Time: 524 // RPM: 0
14:31:51.688 -> Total: 8 // Time: 526 // RPM: 0
14:31:52.237 -> Total: 9 // Time: 528 // RPM: 0

However when i do uncomment the engine speed calculation

rpm = 1/((isrMillis-timeold)/(60*100));

i am getting this:

16:19:13.006 -> Total: 1 // Time: 80247 // RPM: 0
16:19:13.517 -> Total: 2 // Time: 536 // RPM: 
16:19:13.552 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
16:19:13.621 -> 
16:19:13.621 -> Exception (0):
16:19:13.655 -> epc1=0x4000e25d epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
16:19:13.723 -> 
16:19:13.723 -> >>>stack>>>
16:19:13.757 -> 
16:19:13.757 -> ctx: cont
16:19:13.757 -> sp: 3ffffe00 end: 3fffffc0 offset: 0190
16:19:13.791 -> 3fffff90:  3fffdad0 00000000 3ffee360 402010e8  
16:19:13.860 -> 3fffffa0:  3fffdad0 00000000 3ffee360 40201afc  
16:19:13.894 -> 3fffffb0:  feefeffe feefeffe 3ffe84e0 40100d31  
16:19:13.963 -> <<<stack<<<
16:19:13.963 -> 
16:19:13.963 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
16:19:14.064 -> ⸮B⸮⸮⸮⸮

Can someone help?
I got a message like this already at the beginning of playing with interrupts and was able to solve it by this line:
void ICACHE_RAM_ATTR handleInterrupt();
Now it seems to be broken again...

Looks like division by zero. Integer division of 524/6000 is zero, so 1/0 = crash!

Hello wildbill,
Thank you for the hint. The value is even smaller.
524/(60×1000). Which type would you recommend? Long or float?

float.

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