Go Down

Topic: measuring engine rpm using induction coil technique. (Read 1 time) previous topic - next topic

jay2012

hey all,
i want to calculate the speed of my engine using the coil method,and i need to calculate the frequency of the ignition pulses through the arduino...
i have tested a code which gives me the on and the off time for a pulse graph ( used a hall effect sensor and a magnet)..and i need to calculate the frequency of the total number of "on's"...how should i go about it?..should i just divide the time by 1..(1/time)..for the on time,but the on time is slightly different for every time i pass the magnet around the hall effect sensor.

code for pulses:

int pulsePin = 3;
unsigned long highCounter = 0;
unsigned long duration = 0;
int pulse = 0;
int lastPulse = LOW;
unsigned long timeNow = 0;
unsigned long lastTime = 0;


void setup() {
   pinMode(pulsePin, INPUT);
   digitalWrite(pulsePin, HIGH);   
   Serial.begin(9600);
   Serial.println("start");
}

void loop() {
  pulse = digitalRead(pulsePin);
  if (pulse != lastPulse)
  {
    timeNow = millis();
    duration = timeNow - lastTime;
    Serial.print(pulse);
    Serial.print(" ,");
    Serial.println(duration);
    lastPulse = pulse;
    lastTime = timeNow;
    if (pulse == HIGH)
    highCounter++;
   
  }

 
}

i have tried using the attach interrupt function and written another code..but something ( or manythings) are wrong here...because the output never stops and keeps switching between 1,6 and 8.. :smiley-eek:
code:
int pulse = 3;
unsigned int timenow=0;
unsigned int timelast=0;
int input=0;
int counter=0;
unsigned int duration =0;


void setup()
{
  pinMode(pulse,INPUT);
  digitalWrite(pulse,HIGH);
  Serial.begin(9600);
  Serial.println("start");
  attachInterrupt (0, freq , CHANGE);
}
 
  void loop ()
  {
    timenow = millis();
    input = digitalRead(pulse);
    duration = timenow-timelast;
   
    Serial.println(input);
    Serial.println(duration);
    timelast=timenow;
 
  }
  void freq()
  {
    if (duration == 500)
  { counter =0;
  }
  else
    {
       counter ++;
  }
  }

   

i have to calculate the frequency,so that i can calculate the rpm by multiplying it with 60.
cheers.

dc42

The accurate way is to have the Hall sensor trigger an interrupt, and measure the time interval between interrupts. Something like this:

Code: [Select]

volatile unsigned long lastInterruptTime;
volatile unsigned long interval;

void isr()
{
  unsigned long now = micros();
  interval = now - lastInterruptTime;
  lastInterruptTime =now;
}


The in loop() you calculate the rpm from 'interval'.

Please use the # tool when posting code.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

jay2012

thank you.will try doing with that.
will remember about the # next time.

Go Up