Hall sensor read from engine to a nano

Hi guys

i have tried some stuff now but doesnt work well.
i want to read rpm from a engine (up to 18.000 RPM) via its hall sensor.
i have tried this and have trouble

can you help me with and better or shorter code? thanks

int threshold = 200; 
 int val;//the digital value of the incoming analog signals
 int prev_val = 0;
 int t, cur_t; //time variables
 void setup()
 {
   Serial.begin(115200);
   pinMode(A0, INPUT);
 }
 void loop()//Measure RPM
 {
   int sig = analogRead(A0); 
   if (sig > threshold) val = HIGH; 
   else val = LOW;
   if (prev_val == 0 && val == 1) { //check for rising edge
     cur_t = micros();
     Serial.println(1000000 * 60 / (cur_t - t)); //print the rpm
     t = micros();
   }
   prev_val = val;
 }

Is the hall sensor output digital

pinMode(A0, INPUT);

or analog

int sig = analogRead(A0);

?

Please refer to the datasheet, pick one, and reply.

sorry

the hall sensor is output

its this sensor.
dont have the model type, but should be output sens

https://www.ebay.com/itm/04-05-GSXR-600-750-CAM-CAMSHAFT-POSITION-SENSOR-OEM-FAST-SHIPPING-/121598092455

It will be up to you to figure out if the output is analog or digital, then you can write the program. Currently you are using both analog and digital methods and that isn't going to work.

At 18000 RPM, the camshaft (per your link) is rotating at 300 rev per sec or 108000 degrees per second. (if my math is correct)

thanks for reply

what code do you suggest to use?

Danny1000:
what code do you suggest to use?

The one that matches the output of the sensor. If the output of the sensor is high/low, then use digital. If the output varies 0-5V (or less, as more will blow your Arduino input), then use analog.