Crankshaft angle

Hello, I'm a newbie and this is actually my first Arduino project. I'm trying to make an ignition module for my 300cc bike, single cylinder four stroke. The crankshaft sensor is an inductive sensor. The trigger wheel has 18 teeth but the first tooth (TDC) extends towards the direction of rotation and covers almost half the gap between the last tooth and first tooth. So the gap between the last and first tooth is almost half the normal gap between other teeth. I'm a bit confused on how do I get the angle of rotation using Arduino. This is the oscilloscope reading from the sensor

And here is a code I tried writing to blink an LED when TDC passes and the LED blinks but very poorly. If I put a delay in the code it blinks better but I suppose that's the wrong way to do it.

const byte inputCrank = 3;
const byte ignitionOutput = 12;

void setup() {

pinMode(ignitionOutput, OUTPUT);
pinMode(inputCrank, INPUT_PULLUP);

attachinterrupt(digitalPinToInterrupt(inputCrank), TDC, RISING);
}

void loop(){
digitalWrite(ignitionOutput, LOW);
}

static volatile unsigned long longPulse =0;
static volatile unsigned long lastPulse = 0;

void TDC(){

unsigned long now = micros ();
unsigned lon nowPulse = now - lastPulse;
lastPulse = now;

if((nowPulse >>1) > longPulse) {
 
digitalWrite(ignitionOutput, HIGH);
//delayMicrosends(200);
// with this delay it blinks better

longPulse = nowPulse;
}
else{
longPulse = nowPulse
}
}

Keep your ISR as short as possible... that is to say, set a flag inside your ISR that a TDC event has occurred, then outside the ISR, clear the flag and do the pulse stretching for the LED... in pseudo code:

void setup() {
  attachInterrupt(digitalPinToInterrupt(inputCrank), TDC, RISING);
}

void loop() {
  if (tdcFlag) { // check for flag being set in ISR
    tdcFlag = 0; // reset flag
    blinkLED();
  }
}

void TDC() {
  noInterrupts(); // suspend interrupts
  tdcFlag = 1;
  interrupts(); // resume interrupts
}

I highly recommend against this especially if it will be on the road. The Arduino was not designed for this type of application software wise, mechanically and electronically. You have a bunch of interrupt things happening in the background that will cause timing jitter.

I do not see the missing tooth on your scope display.

This is a common method for determining crank position.

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