Detecting Top Dead Centre of crankshaft using STM 32

Thread Starter

xuraax

Joined Jun 10, 2025
6
Hi,

This is my first message to this forum so I hope I have landed in the correct place.

The below shot is of a cleaned up signal coming out of my motorbike crank shaft. this signal is made up of 23 pulses with a gap between each group of 23. The centre of that gap signifies when the piston connected to the shaft has reached the top of its stroke. The pulse width is determined by the current RPM of the engine. in the picture this was around 1700. The engine can rev up to above 10000 so pulse widths would be significantly shorter at these speeds. I would like to use a microcontroller such as an STM32 to be able to determine this TDC condition.

I would appreciate any suggestions to techniques of how to achieve this.

regards.ShaftSignal1.jpg

ShaftSignal1.jpg
 

MrChips

Joined Oct 2, 2009
34,626
STM32 can measure the pulse-width of each pulse using a Timer module and interrupts.
It can then detect the wider pulse and then synchronize to the 23 pulses. Now it has two ways to stay synchronized.
It can also display the RPM live.
 

crutschow

Joined Mar 14, 2008
38,316
Supplement:
After you determine the width of the wider pulse, you can then output a signal at 1/2 that time (1-bit binary right shift) after the leading edge of the wide pulse for the TDC point.
 

Ian0

Joined Aug 7, 2020
13,097
Use the timers - I know how Renesas Cortex M4 timers work and you will most likely be able to do the same thing with STM32 timers.
Set the timer to CAPTURE and to RESET on rising edges. Trigger an interrupt on the capture.
In the CAPTURE ISR multiply the captured value by 1.25 and write it to the MATCH register.
Trigger a different interrupt on MATCH. This interrupt will trigger at TDC. (Normally it won't trigger because the timer will have been reset before then)
To multiply by 1.25 in ARM code do ADDS R0,R0,R0,ASR#2

One thing to think about: How much variation in angular velocity is there over a cycle?
 

Thread Starter

xuraax

Joined Jun 10, 2025
6
One thing to think about: How much variation in angular velocity is there over a cycle?
Thank you all for your suggestions. I have not used the STM32 timers to date so I guess i will have to do some further reading.

@Ian0,

why multiply by 1.25?

Also I am worried about the effect of variation of angular velocity over a cycle since this parameter is totally controlled by the speed pedal. Even at constant speed, during one cycle there would be a compression phase followed by a combustion phase. Although there is of course a flywheel to smooth things out I tend to think that the crank would be rotating faster during the latter phase of the cycle.

Finally i am wondering why the designers chose this method of pinpointing TDC. Standard motorbike ECU's don't appear particularly powerful to me.
 

Ian0

Joined Aug 7, 2020
13,097
Thank you all for your suggestions. I have not used the STM32 timers to date so I guess i will have to do some further reading.

@Ian0,

why multiply by 1.25?

Also I am worried about the effect of variation of angular velocity over a cycle since this parameter is totally controlled by the speed pedal. Even at constant speed, during one cycle there would be a compression phase followed by a combustion phase. Although there is of course a flywheel to smooth things out I tend to think that the crank would be rotating faster during the latter phase of the cycle.

Finally i am wondering why the designers chose this method of pinpointing TDC. Standard motorbike ECU's don't appear particularly powerful to me.
Isn’t it exactly 1.25 complete cycles from the rising edge of the last real cycle to the centre of the “missing” cycle?
The missing rising edge is 1 complete cycles from the after the last rising edge, the missing falling edge is 1.5 cycles, so the centre of the pulse must be 1.25 cycles.
I was wondering whether there might be two outputs from the sensor, the other one being a single pulse for TDC!
How many cylinders does the engine have? Is it 2-stroke or 4-stroke?
A single cylinder 4-stroke will slow down a bit, but you are only taking the most recent pulses to find the missing one, not the ones from a rotation earlier.
 

John P

Joined Oct 14, 2008
2,051
It looks as if there are lowgoing pulses, and what's of interest here is the time spent in the high state. What I would do is use a clock that counts when the signal is high, and when the signal goes low, the count is transferred to an element of an array, and then the total gets cleared ready for the next high state. The array elements are each loaded in turn, recycling when 23 have occurred.

After 23 pulses, add up all the "high" times and divide by 23. That gives you the average size. Then look at each one in turn and compare that single one to the average. The TDC indicator will be significantly larger than average. (But note that no use is made of fixed numbers, because the engine speed is variable.)

Knowing which high time (among the 23) represents the TDC, you know which point among the 23 pulses represents TDC, but defensive programming would involve continuing to check that it's occurring at the expected place. You wouldn't want to risk repeatedly getting it wrong by just counting blindly, when there might be an occasional extra pulse or a missed pulse.
 

Thread Starter

xuraax

Joined Jun 10, 2025
6
I was wondering whether there might be two outputs from the sensor, the other one being a single pulse for TDC!
How many cylinders does the engine have? Is it 2-stroke or 4-stroke?
A single cylinder 4-stroke will slow down a bit, but you are only taking the most recent pulses to find the missing one, not the ones from a rotation earlier.
Crudely put the crank has a disk which is divided into 24 sectors. At the tip of each sector except one, there is a small magnet. The sensor is simply a coil fixed to the housing. As the magnets fly by they generate a voltage in the coil which increases in amplitude and frequency as the RPM goes up.

My cleanup circuit cuts off half of this ac signal and drives the output transistor into saturation to force the output to stay within TTL range.

The engine itself is actually a 4 stroke twin cylinder engine with the cylinders places at about 285 degrees to each other. This means that the TDC point is only valid for one of the cylinders. I guess the other is determined by counting a number of pulses away from TDC.

I am wondering why the designers went into the trouble of placing 23 magnets instead of just 2 at the TDC point of each of the 2 cylinders. Wouldn't that have made the calculations a lot simpler?

My gut feeling is that the designers must have stumbled on some simple technique of finding the TDC point on each rotation and then just simply counting off a number of pulsed to get to the other TDC point.
 

Ian0

Joined Aug 7, 2020
13,097
Crudely put the crank has a disk which is divided into 24 sectors. At the tip of each sector except one, there is a small magnet. The sensor is simply a coil fixed to the housing. As the magnets fly by they generate a voltage in the coil which increases in amplitude and frequency as the RPM goes up.

My cleanup circuit cuts off half of this ac signal and drives the output transistor into saturation to force the output to stay within TTL range.

The engine itself is actually a 4 stroke twin cylinder engine with the cylinders places at about 285 degrees to each other. This means that the TDC point is only valid for one of the cylinders. I guess the other is determined by counting a number of pulses away from TDC.

I am wondering why the designers went into the trouble of placing 23 magnets instead of just 2 at the TDC point of each of the 2 cylinders. Wouldn't that have made the calculations a lot simpler?

My gut feeling is that the designers must have stumbled on some simple technique of finding the TDC point on each rotation and then just simply counting off a number of pulsed to get to the other TDC point.
At a guess, it's for ignition timing (assuming it doesn't have fuel injection or variable valve timing). As ignition has to be BEFORE tdc, knowing when you have got to tdc is a bit too late, and timing it from the last tdc isn't as accurate as it might be. As it is on the crank how does it know if it is at the end of a compression cycle or the end of an exhaust cycle?
The 22nd and 23rd pulses after tdc are 30 degrees and 15 degrees before tdc, which is probably used to time the spark.
 

Thread Starter

xuraax

Joined Jun 10, 2025
6
At a guess, it's for ignition timing (assuming it doesn't have fuel injection or variable valve timing). As ignition has to be BEFORE tdc, knowing when you have got to tdc is a bit too late, and timing it from the last tdc isn't as accurate as it might be. As it is on the crank how does it know if it is at the end of a compression cycle or the end of an exhaust cycle?
The 22nd and 23rd pulses after tdc are 30 degrees and 15 degrees before tdc, which is probably used to time the spark.
Be aware that the statement that the middle of the wide pulse is exactly when the piston is right at the top is really my assumption. From what you said, it would make logical sense that point occurs at some angle before the piston gets to the top to allow the ECU to calculate when the fuel injector opens and for how long. Then it has to do the same for the ignition coil.

Your question of how does it knows that it is at the the end of the compression stroke or exhaust stroke is also very valid. In fact there is another question to complicate matters which is how does it determine if this is for cylinder 1 or 2?

This bike has a pressure sensor placed in line with the carburetor for cylinder 2 only. I am guessing the ECU measures the drop in pressure when this cylinder is in suction mode and then works out an answer to the above 2 question based on this figure.

This bike is a 2013 design which would probably make the ECU at least a 14 year old design. Given the above and the many other parameters it has to monitor I am quite impressed at what this old processor is achieving.
 

Futurist

Joined Apr 8, 2025
721
Why not use a single magnet/hall effect switch, positioned in such a way that a pulse is generated right at the time that ignition should take place rather than when at TDC
 

Ian0

Joined Aug 7, 2020
13,097
This bike is a 2013 design which would probably make the ECU at least a 14 year old design. Given the above and the many other parameters it has to monitor I am quite impressed at what this old processor is achieving.
We had STM32s in 2013, and they would do the job. And we've had TMS320s a lot longer than that.
 

Futurist

Joined Apr 8, 2025
721
I think the OP is correct, the wide pulse denotes the "start" of cycle, by counting subsequent pulses one can detect when each piston is positioned appropriately for ignition.

How many short pulses, follow a long pulse?
 

Ian0

Joined Aug 7, 2020
13,097
I think the OP is correct, the wide pulse denotes the "start" of cycle, by counting subsequent pulses one can detect when each piston is positioned appropriately for ignition.

How many short pulses, follow a long pulse?
23 (see post #1). They are 15° apart. The offset between the two cylinders is 285° which is 19 pulses.
 

Thread Starter

xuraax

Joined Jun 10, 2025
6
I think the OP is correct, the wide pulse denotes the "start" of cycle, by counting subsequent pulses one can detect when each piston is positioned appropriately for ignition.
The problem is you have no control of the pulse width as the bike may be accelerating or decelerating whilst you are trying of find the wide pulse.
Imagine you have determined the width of 2 pulses to be similar ( you need at least 2 to ensure that you have not carried out the measurement on the wide pulse itself) and then as you are looking for the wide pulse the bike accelerates and the width of the wide pulse is not twice that of your measured pulse any more.
 

Ian0

Joined Aug 7, 2020
13,097
The problem is you have no control of the pulse width as the bike may be accelerating or decelerating whilst you are trying of find the wide pulse.
Imagine you have determined the width of 2 pulses to be similar ( you need at least 2 to ensure that you have not carried out the measurement on the wide pulse itself) and then as you are looking for the wide pulse the bike accelerates and the width of the wide pulse is not twice that of your measured pulse any more.
All that you need to detect is when a pulse has not occurred within 125% of the time since the previous pulse. I doubt it could increase speed by 25% within a 24th of a revolution.
 

Thread Starter

xuraax

Joined Jun 10, 2025
6
All that you need to detect is when a pulse has not occurred within 125% of the time since the previous pulse. I doubt it could increase speed by 25% within a 24th of a revolution.
That is a very good observation Ian. Thank you.
 

Futurist

Joined Apr 8, 2025
721
The problem is you have no control of the pulse width as the bike may be accelerating or decelerating whilst you are trying of find the wide pulse.
Imagine you have determined the width of 2 pulses to be similar ( you need at least 2 to ensure that you have not carried out the measurement on the wide pulse itself) and then as you are looking for the wide pulse the bike accelerates and the width of the wide pulse is not twice that of your measured pulse any more.
I understand, those are real concerns. You'd need to look at a continuous multi-second sample of the signal to really quantify the problem I think. I mean at 6000 RPM, how long does it take to reach 7000 RPM? how many revolutions does that take at max acceleration?
 
Top