Can GPIO measure injector pulse width directly?

A forum for discussing the applications and implementations of the DIY general code template for the GPIO and its 25 user inputs and outputs (+ serial & CAN). The code is 99% complete, the user just has to add their own conditional I/O logic and compile (with a free special Edition of CodeWarrior).
Post Reply
Posts: 5
Joined: Thu Apr 05, 2012 7:19 am

Can GPIO measure injector pulse width directly?

Post by diztroy » Thu Apr 05, 2012 7:21 am

Question: How to use GPIO to measure injector pulse width directly (in non-CAN, standalone mode, for datalogging)

I would like to use GPIO in standalone mode to monitor vehicle speed along with injector pulse width for an instant MPG calculator in TunerStudio. The vehicle speed should not be a problem with the VR inputs available, but I'm struggling with measuring the injector pulse width directly (non-CAN). I understand that normally this information is easily gathered from the megasquirt controller, but I don't have a megasquirt installed in this car just yet, so I would like to find a way to measure/log the injector pulse width before I convert to full engine control. As I understand it, the GPIO could not directly measure pulse width, so it will need some hardware modifications.

I struggle with electronics design, so until I gain more experiance the best I can do is google for a solution. I found this pulse width to voltage converter starting on page 92 of Electronics, Design, Strategy, News .

Is this a viable solution? Would it be a simple matter of hooking up the injector (-) to the PULSE IN and then reading the output of IC3 at a general purpose input of GPIO? Also it seems the circuit would need to be modified for a wider range of pulse widths, what range would be suitable?....what parts would you modify and what values can you suggest?

Thank you for any and all comments and suggestions!!


Bernard Fife
Posts: 1696
Joined: Fri Apr 04, 2008 1:28 pm

Re: Can GPIO measure injector pulse width directly?

Post by Bernard Fife » Thu Apr 05, 2012 9:20 am


The GPIO board can measure pulse widths directly - this is much more flexible than converting the PW to a voltage, etc. I will attach some notes on how to use the timers to capture external events.
(329.25 KiB) Downloaded 342 times
For the hardware, you can connect directly to the injector's negative side (the controller side that grounds the injector to fire it). Some initial considerations are:

- You need to make sure the signal never exceeds 5.6V, so you likely need a voltage divider circuit. To do this, put a resistor (about 1K ought to do it) in between the signal and the processor, and a 1K Ohm resistor between that resistor and ground. These will cut the voltage in half. Then to get the voltage down to safe, stable level, use a 5.1V Zener diode (such as 1N5231BFSCT-ND) connected to ground on the non-banded end, and connected between that resistor and the processor on the banded end.

- For the GPIx circuits (, this would mean:
-- nothing in Rc
-- Ra = 1K Ohm resistor (1/4 watt)
-- Rb = 1K Ohm resistor (1/4 Watt)
-- nothing in Cb
-- Ca = 5V Zener diode

- The pulse width will be the time from going low to going high (i.e. falling edge to rising edge). That's because the controller grounds the injectors to fire them.

- The timer port pins are PT0 to PT7. The default circuit assignments at the 25x2 header are:
-- PT0 - VR1 (this is used as the vehicle speed sensor input in the trans code)
-- PT1 - PWM4 (used as output2 in the trans code)
-- PT2 - PWM3 (pressure control PWM output in trans code)
-- PT3 - PWM2 (torque converter clutch output in trans code)
-- PT4 - PWM1 (speedo output in trans code)
-- PT5 - VR3 (input shaft speed sensor)
-- PT6 - VR2 (upshift button in trans code)
-- PT7 - VR4 (spare output 1 in trans code)
but you can jumper any pin to any circuit you prefer.

"Never wrestle with pigs. You both get dirty and the pig likes it." - George Bernard Shaw

Posts: 5
Joined: Thu Apr 05, 2012 7:19 am

Re: Can GPIO measure injector pulse width directly?

Post by diztroy » Thu Apr 05, 2012 11:31 am


Thanks so much for the background material and circuit information and tips, all very very helpful!

As typical, I was making this harder then it needed to be, glad to know GPIO can measure pulse width as you have show. Now to get the parts and pieces together, I'll report back here when I get it going. :)


Post Reply