** HELP ** Engine Speed Signal Outputs for Crankshaft and Camshaft Sensors

Hi everyone!

I’m new to the Arduino world and the C language. I need a little guidance on something I’ve been struggling with the past week or so.

What I’m Trying To Accomplish: Replicating synchronized crankshaft and camshaft signals to be read by an ECM (engine control module) to simulate an engine running.

What I’ve Done So Far: Originally I experimented with the delay() function which was seemingly easy at first with only 1 signal at a time. The issue I had was of course trying to create both signals simultaneously which is when I discovered the millis() and micros() timing functions. After playing around with the millis() function I think I’ve figured out the crankshaft signal since it’s the easier of the two. The crankshaft timing for this specific engine is a 60-3 sequence which essentially just means it has a total of 60 spaces for small teeth with 3 of those spaces reserved for 1 wide tooth (see attachment below for a drawing of the actual crankshaft timing wheel for reference).

What I’m Struggling With: Since I’m a bit new to the coding language, I feel like I’ve maybe over complicated my code just to get the crankshaft signal I currently have. I’m hoping there’s an easier logic to do what I’m trying to achieve here. Hopefully some of you can help!

I can’t seem to figure out where to start with generating the camshaft signal since the HIGH and LOW signals aren’t as simple as the crankshaft’s. I’ve attached a screenshot of both signals from a running engine that was given to me by another member who had previously posted back in April with a similar question with no solution. The crankshaft signal is in blue and the camshaft is in red. The camshaft signal looks like it has 1 small tooth. 1 medium tooth , followed by 5 large teeth.

I have no idea where to start with coding this alongside the crankshaft signal. I’ve attached some files to help you all understand a bit more. I’m a bit rushed typing this at the moment as I’m about to have to leave work so if I’ve left anything out please let me know.

Thank you all in advance!

Here’s the code I’ve written so far:

// Variables
const int ledPin = LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 20;
unsigned long count = 0  ;


void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long currentMillis = millis();

  
  if (count < 57){
    if (currentMillis - previousMillis >= interval) {
      Serial.print("Current count is: ");
      Serial.println(count);
      Serial.print("Led State: ");
      Serial.println(digitalRead(ledPin));
      // save the last time you blinked the LED
      previousMillis = currentMillis;
  
      
      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        ledState = HIGH;
        
      } else {
        ledState = LOW;
        count++;
      }
     
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
   
  }
  else if (count>56 && count<63) {
    if (currentMillis - previousMillis >= interval) {
      Serial.print("Else if #1: ");
      Serial.println(count);

      previousMillis = currentMillis;
      digitalWrite(ledPin, HIGH);
      count++;
    }
    
  }

  else if (count == 63) {
    Serial.println("Count has reached 63!");
    digitalWrite(ledPin, LOW);
    Serial.println(digitalRead(ledPin));
    count = 0;
  }
}

Engine_Speed_Millis_Test_6-17-20.ino (1.44 KB)

Please read the first topics telling how to use Forum, how to attach code, pictures etc.
Many users use smartphones, tablets etc. and then downloading code is impossible, and You get no answer from them.

Any code thats does the job is good, or okey, code. I've seen hot guys messing up in an advanced code that doesn't work

Railroader:
Please read the first topics telling how to use Forum, how to attach code, pictures etc.
Many users use smartphones, tablets etc. and then downloading code is impossible, and You get no answer from them.

Any code thats does the job is good, or okey, code. I've seen hot guys messing up in an advanced code that doesn't work

Oops. I'll take a look at it tomorrow and edit my post then. Thanks for the info!

You might want to take a look at ardu-stim. This is a crank, cam, wheel simulator program.

cattledog:
You might want to take a look at ardu-stim. This is a crank, cam, wheel simulator program.

LibreEMS Suite / ardu-stim · GitLab

I have but it's a bit too complex for me to follow at the moment

On the crankshaft display, I see 57 pulses then a 3 pulse blank space, so 60 total with 3 missing for (I'm guessing), top-dead-center indication, can't make out camshaft display, too faint.

JCA34F:
On the crankshaft display, I see 57 pulses then a 3 pulse blank space, so 60 total with 3 missing for (I’m guessing), top-dead-center indication, can’t make out camshaft display, too faint.

Yes, except the longer pulse would actually be a wider tooth (nothing missing there). I’ve attached a drawing of the crankshaft timing wheel for reference

The camshaft spins at half the speed of the crank so as you can see in the graph there’s 1 small, 1 medium , and 5 large pulses for the camshaft.

In this picture, you are seeing different width pulses because the engine speed is varying, what does the camshaft wheel look like?

I presume the camshaft runs at 50% of the crankshaft speed. Is so there should be a frequency (I think it is the Highest Common Factor between the pulse rate for the camshaft and the pulse rate for the crankshaft) which can be used to generate both trains of pulses.

Suppose (for a simple example) the crankshaft produces 100 pulses per second and the camshaft produces 45 pulses per second. If you generate a counter at 4500 per second then every 45 counts it will be time for a crankshaft pulse and every 100 counts it will be time for a camshaft pulse.

...R

JCA34F:
In this picture, you are seeing different width pulses because the engine speed is varying, what does the camshaft wheel look like?

Unfortunately, the camshaft doesn't have a separated timing part. It seems to be attached the the middle of the camshaft. See the following photos for reference:

Robin2:
I presume the camshaft runs at 50% of the crankshaft speed. Is so there should be a frequency (I think it is the Highest Common Factor between the pulse rate for the camshaft and the pulse rate for the crankshaft) which can be used to generate both trains of pulses.

Suppose (for a simple example) the crankshaft produces 100 pulses per second and the camshaft produces 45 pulses per second. If you generate a counter at 4500 per second then every 45 counts it will be time for a crankshaft pulse and every 100 counts it will be time for a camshaft pulse.

...R

I'm almost certain the camshaft should run at half the speed of the crankshaft so you're presumption is probably correct.

I understand your logic here with the pulses but I'm having trouble figuring out how to implement the camshaft signal code with my current crankshaft code. I was planning on just measuring the camshaft signals by using the screenshot by drawing segmented lines to get a better idea of the timing intervals.

Is there an easy way to tie both of the signals in somehow where it wouldn't be difficult to edit or phase the camshaft signal?

Thanks!

Hi,

I'm almost certain the camshaft should run at half the speed of the crankshaft so you're presumption is probably correct.

[soapbox]Poor show with the graphs, my maths teacher would shoot me for producing a graph without ANY documentation as to the Axes and what each channel is connected to?[/soapbox}

What are the three traces?
Are they crankshaft and camshaft traces off the vehicle you wish to simulate?

What is the motor/model you are trying to simulate?

No two different ECU models are the same, car manufactures will make sure that will never happen.
So you will have to be specific, not just on 60-3 on the crankshaft, but what signal and reference pulse comes off the camshaft, and if there is an offset between the reference pulses of the crankshaft and camshaft.

Tom... :slight_smile:

TomGeorge:
Hi,
[soapbox]Poor show with the graphs, my maths teacher would shoot me for producing a graph without ANY documentation as to the Axes and what each channel is connected to?[/soapbox}

What are the three traces?
Are they crankshaft and camshaft traces off the vehicle you wish to simulate?

What is the motor/model you are trying to simulate?

No two different ECU models are the same, car manufactures will make sure that will never happen.
So you will have to be specific, not just on 60-3 on the crankshaft, but what signal and reference pulse comes off the camshaft, and if there is an offset between the reference pulses of the crankshaft and camshaft.

Tom... :slight_smile:

Yeah I wish I had a better graph of the signals. I'll see if I can get a better screenshot from the shop teacher I've been chatting with haha. Yes, these are the signals off of the engine I need to simulate. It's a diesel Cummins ISX engine.

It's difficult to decipher the camshaft signal based on how it looks since the timing wheel portion of it isn't its own separate part like the one off of the crankshaft. Best info I have as of right now is the screenshot that was provided to me. I was considering just using Photoshop to duplicate the signal to recreate 1 cycle of all of the 6 of the cylinders being fired.

samirakle:
I'm almost certain the camshaft should run at half the speed of the crankshaft so you're presumption is probably correct.

It would be very very strange if it is not exactly 50% (assuming you are modelling a 4 stroke engine). And, before you go any further with your project you must find out because the output from the crank sensor and the output from the cam sensor will be expected to be exactly in sync

I have a bent inlet valve as a souvenir of an occasion when they were not in sync :slight_smile:

Is there an easy way to tie both of the signals in somehow where it wouldn't be difficult to edit or phase the camshaft signal?

I'm not sure what you had in mind when you wrote that but the whole purpose of my suggestion is to be able to replicate the very tight synchronisation between the outputs from the two sensors.

I expect the phasing to be set by the number of counts at which the missing pulse(s) occur(s). That could easily be changed.

...R

Robin2:
It would be very very strange if it is not exactly 50% (assuming you are modelling a 4 stroke engine). And, before you go any further with your project you must find out because the output from the crank sensor and the output from the cam sensor will be expected to be exactly in sync

I have a bent inlet valve as a souvenir of an occasion when they were not in sync :slight_smile:

I'm not sure what you had in mind when you wrote that but the whole purpose of my suggestion is to be able to replicate the very tight synchronisation between the outputs from the two sensors.

I expect the phasing to be set by the number of counts at which the missing pulse(s) occur(s). That could easily be changed.

...R

Well, since I don't quite have the camshaft timing down yet I guess I should get a little guidance on how to even use the millis() or micros() functions to create more than 2 different time intervals for the HIGH and LOW 5V+ signals.

The only tutorials I can seem to find only show how to create signals with a max of 2 different time interval (1 for HIGH and 1 for LOW).

Can anyone assist me with the correct logic to create more than 2 time intervals using the millis() or micros() functions?

Looks like Cummins changed the basic design of the engine over the years. Dual cam shafts to single, etc. What year is the engine you wish to emulate?

Paul

samirakle:
Well, since I don't quite have the camshaft timing down yet I guess I should get a little guidance on how to even use the millis() or micros() functions to create more than 2 different time intervals for the HIGH and LOW 5V+ signals.

If you follow the scheme I suggested you only need to time one counter and the two sets of pulses can then be derived from that. The idea works. I have used if for the coordinated movement of 3 stepper motors.

...R

Paul_KD7HB:
Looks like Cummins changed the basic design of the engine over the years. Dual cam shafts to single, etc. What year is the engine you wish to emulate?

Paul

Early 2000's. Probably between 2002-2008 or so.

samirakle:
Early 2000’s. Probably between 2002-2008 or so.

Is it before or after they went to a common rail for the injection system?

Paul

Paul_KD7HB:
Is it before or after they went to a common rail for the injection system?

Paul

I believe this model was for the common rail