NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Measuring time between 2 triggers.

 July 01, 2010 by huzbum I've been out of the programming game for a long time, and I'm used to higher level languages like Java and Visual C++. If someone steers me in the right direction, and I can get started, I'm sure I'll take off from there. What I'm trying to do is make a spark box for my motor cycle, but for starters all I need to do is make a fancy ignition tach, and I can go from there. The input is a low voltage (20v) sin wave pulse from a magneto. I intend to use the high and low peaks from the pulse to measure engine speed without waiting for the full revolution to take place (the next pulse.) I assume I could measure this with some diodes and mosfets for the triggers. This will happen between 50 and 7000 times a second, and I need timing accurate to around 1/360th a revolution. So around 2.5MHz or better. So... what I need to do is measure the time between 2 triggers and report it. I can figure out the rest from there. If someone could show me where to start I would be grateful. oops... mistake in my figures there... it's 50-7000 RPM not RPS... So it'd be between roughly once every 1.2 seconds to 117 times a second... big difference. So a time measurement of 42KHz or better should work. Hi huzbum, It sounds like you have a pretty interesting project in mind. On the software side timing spikes should not be too difficult. What you basically want to do is setup a counter/timer and a pin change interrupt. Your pin change interrupt will fire whenever the voltage on your pin spikes, and you can record the timer/counter value in the interrupt. You can read about pin change interrupts in our interrupts tutorial. You will need to think a bit about how to set up the timer, since you have a relatively slow signal and a pretty wide range want to measure over you probably want to keep track of how many times a counter timer rolls over, and use that to time the time between pules. You could set up an 8-bit timer with no prescaler, which would overflow at about 57Khz. That should be enough to let you time your RPMs with enough accuracy. Getting the signal into your MCU is going to be a bit tricky, since you have to make sure you only hit the input pin with 5V, but it sounds like you were asking more about the software side of things. If you run into any problem there (or in setting up the timers) let us know, I can't wait to see this project working! Humberto