Click here for MegaSquirt® MegaManual™ Information, Guides, and Links

MegaSquirt-II GPIO General Purpose Output Circuits

In addition to dedicated pulse width modulation and high current ignition driver outputs (and a variety of input circuits), the General Purpose I/O (GPIO) board has four general purpose output circuits. These can be configured by the builder to operate as grounding circuits (with or without flyback), light emitting diode drivers, grounding circuits with 'pull-up' voltage. Clever users can find other ways to configure these output circuits, of course.

Circuit
CPU Port
AMP pin
number
GPO1PM410
GPO2PM37
GPO3PM58
GPO4PB49

Any of the output circuits can be configured in any way: they don't all have to be the same, and not all have to be installed.

The default transistor for the GPOx circuits is the ZTX450. This transistor is capable of a continuous current of up to 1 Amp. Larger capacity transistors can be used, such as the ZTX651 (NPN, 2 Amps continuous current), however the current may ultimately be limited by the ability of the board's traces to carry the required current (they are designed for up to 2 Amps continuous current).

Simple Ground Circuit

This circuit consists of:

IDDefault
Value
Install?Notes
Q1yestransistor, see below for advice on selecting an appropriate transistor and base resistor.
R1yesbase resistor, see below for advice on selecting an appropriate transistor and base resistor.
R2nonot used
D1nonot used
D2nonot used
J1nonot used

This circuit simply grounds a device through a transistor when commanded by the processor. It is suitable for low current devices that provide a voltage signal which can be 'pulled low', such as some ignition modules, external LEDs, etc. Note that the circuit has two states: grounded (active), and floating (inactive). See below for advice on selecting an appropriate transistor and base resistor. If you need a 5 or 12 Volt signal that switches from high to low (5 or 12 Volts to ground), use the pull-up circuit instead.

Flyback Ground Circuit

This circuit consists of:
IDDefault
Value
Install?Notes
Q1yestransistor, see below for advice on selecting an appropriate transistor and base resistor.
R1yesbase resistor, see below for advice on selecting an appropriate transistor and base resistor.
D1yesflyback diode
J1yesjumper
R2nonot used
D1nonot used

The flyback ground circuit is used for connecting devices that have moderately high inductance, typically a relay or solenoid. 12 Volts is supplied to the relay/solenoid, and it is grounds through the GPIO flyback ground circuit. These devices create a flyback voltage spike when the current is stopped, and in this circuit, this voltage spike is passed to the 12 Volt supply through the flyback diode (D1), reducing the potential for damage to the transistor. (Note that if your device is powered by a 5 Volt supply, you should jumper J1 to the 5 volt supply.)

LED Driver

This circuit consists of:
IDDefault
Value
Install?Notes
Q1yestransistor, see below for advice on selecting an appropriate transistor and base resistor.
R1yesbase resistor, see below for advice on selecting an appropriate transistor and base resistor.
R2yescurrent limiting resistor for the LED
D2yeslight emitting diode
J1yesjumper
D1nonot used

This circuit is used to operate a light emitting diode (LED). There are no external connections, however the LED can be moved off the board simply by extending the leads to th LED with wires. See below for advice on selecting an appropriate transistor and base resistor.

Pull-Up Circuit

This circuit consists of:
IDDefault
Value
Install?Notes
Q1yestransistor, see below for advice on selecting an appropriate transistor and base resistor.
R1yesbase resistor, see below for advice on selecting an appropriate transistor and base resistor.
R2yescurrent limiting resistor for the LED
J1yesjumper
D1nonot used
D2nonot used

The pull-up circuit is used to provide a 5 or 12 Volt signal that switches from high to low (5 or 12 Volts to ground). This is used for things like some ignition modules, and for connecting to some other electronic devices. See below for advice on selecting an appropriate transistor and base resistor.

Choosing a Transistor for an Output Circuit

The GPIO general purpose output circuits are set up for NPN transistor in a TO-92 package.

A transistor has three connections:

  1. Base: current through the base connection (which goes to the emitter) controls the current from the Collector to the emitter.
  2. Collector: in the GPOx circuits, the collector is connected to the device to be grounded, and conducts all the current from that device, and
  3. Emitter: the emitter is connected to ground, conducting both the collector and base current.

Note that this means that the transistor can do two things:

In most cases, the GPOx circuit will be used to switch external devices. To select a transistor for this purpose:

  1. Determine the maximum current to be controlled (switched). The transistor's maximum collector current Ic(max) must be greater than the current through the switched device (Ic). The load current (Ic) is equal to the supply voltage (Vs) divided by the load resistance (R):

    Ic = Vs/R (this is Ohm's Law - the most fundamental relationship of electrical circuits)

    Typically, for external automotive devices, the supply voltage is ~12V.

    For example, suppose our device has a measured resistance of 48 Ohms. The Ic = 12/48 = 0.250 Amps = 250 milliAmps

  2. The transistor's minimum current gain (hFEmin) must be at least five times the load current Ic divided by the maximum output current from the chip.

    hFE(min) > 5 × (load current Ic / max. chip current)

    For the 9S12 processor on the GPIO board, the absolute maximum output current is 25 milliAmps, and it's better to keep it to 10 milliAmps. So you would need:

    hFE(min) > 5 × (load current Ic / max. chip current) = 5 * ([Vs/r])/(.010) = 500 * [Vs/R]

    For a 12 Volt supply, this works out to approximately:

    hFE(min) > 500 * [Vs/R] = 6000/R

    With our previous example, this works out to:

    hFE(min) > 6000/R = 6000/48 = 125, so hFE must be at least 125

  3. Choose a transistor which meets these requirements and make a note of its properties: Ic(max) and hFE(min). In addition to the hFE and Ic characteristics, the transistor 'package' (it's physical dimensions and lead arrangment) must match the GPIO board layout (unless you are willing to jumper the leads, etc.). The GPOx output circuits have a TO-92 package. Here are some common TO-92 transistors:

    Common NameIchFEDigi-Key
    Part Number
    -ND
    -ND

  4. Calculate the nominal required base resistance:

    Rbase = (Vc × hFE) / (5 × Ic)

    where

    Vc = chip supply voltage (5.0 for the 9S12)

    So from our previous example:

    Rbase = (Vc × hFE) / (5 × Ic) = (5.0 x 125) / (5 * .250) = 125/.250 = 500 Ohms

  5. Then choose the next lowest standard value for the base resistor. Resistors generally come in 10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, and 82 values (and multiples of 10, 100, 1000, etc.) in 10% tolerances. Finer differences are available at higher tolerances. However for switching purposes, 10% is fine, just use the resistor with the next lower value from what you calculate.

    For our example, if you calculate 500 Ohms for the base resistor, use the 470 Ohm resistor (1/8 watt is fine for the base resistors likely to be used on the GPIO board).

Finally, remember that if the load is a relay or solenoid, you must use the flyback circuit, which has a protection diode (D1).

By the way, a Darlington transistor is two transistors packaged together to give a very high current gain (hFe). The current amplified by the first is amplified further by the second transistor. The overall current gain, hFE, is equal to the two individual gains multiplied together. As a result, hFE > 10000 isn't unusual, and these are suitable for controlling larger currents using the processor port.

Choosing a Light Emitting Diode for an Output Circuit

Light emitting diodes do two things:

  1. They allow current to pass in one direction only (from the anode to the cathode), like all ordinary diodes,
  2. They emitting light at a specific wavelength (color) while they are conducting a current in the forward direction.

The light emitting property allows us to use LEDs as indicators, controlled by the CPU (and a transistor).

  1. Choose an LED. Light emitting diodes have a number of parameters you need to consider:

    The LED hole spacing on the GPIO board is for T3/4 LEDs (this is the package size that defines the physical dimensions of the LED). You may be able to use other LEDs by bending and/or jumpering leads.

  2. Choose a resistor. A light emitting diode must have a resistor connected in series with it to limit the current through the LED, otherwise it will burn up very quickly.

    The resistor value, R2 on the GPOx circuits, can be calculated as:

    R2 = (Vs - Vl) / I

    where

    If the calculated resistor value is not available, choose the next higher standard resistor value. Then the current will be a little less than the maximum (which will dime the LED slightly), which is preferable to too much current.

    For example, on the GPIO board, the supply voltage Vs = 5V (unless you have jumpered to the 12V supply), for a red LED (Vl = 2V), requiring a current I = 20mA = 0.020A, you would use:

    R2 = (5 - 2) / 0.020 = 150 Ohms

    You can use higher resistances to dim the LED, or to reduce the current draw.

Choosing a Flyback Diode for an Output Circuit

The flyback diode has two important parameters:

  1. Peak Repetitive Reverse Voltage: this must be greater than the supply voltage
  2. Non-Repetitive Peak Forward Surge Current: this must be able to handle the flyback current surge.

The GPIO board is set up for diodes in a DO-41 package. These have a body that is approximately 4 mm to 5mm long and 2.0 to 2.7 mm in diameter, with axial leads (that are 0.7 to 0.9 mm in thickness). Other diodes can sometimes be used by bending leads, etc.

The 1N4001-1N4007 series of diodes are very common. They have a reverse voltage rating of at least 50 Volts (1N4001), all the way up to 1000 Volts (1N4007). Their surge current can be up to 30 Amps. Any of them is suitable for the flyback diode.

Choosing a Pull-Up Resistor for an Output Circuit

The pull-up resistor is used to limit the current flow from the pull-up voltage source when the transistor (Q1) is grounding the circuit. When the transistor is 'open' (i.e., not allowing current to flow), the pull-up voltage will be applied to the circuit, as long as the external device is not grounded (is very high impedance). So the pull-up resistor can be relatively high resistance, limiting the current draw.

  1. Determine the current draw. The current drawn from the pull-up voltage source will be approximately:

    I = Vs/R2 (Ohm's Law applied to the pull-up circuit)

    (ignoring the voltage drop in the transistor)

    For example, if Vs = 12 V, and we have a pull-up resistor of 1K Ohms (1000 Ohms), then:

    I = 12/1000 = .012 Amps

  2. To determine the wattage requirement of the resistor, we calculate the power dissipation of the resistor, which is:

    P = I * Vs, in Watts

    and since V = 1/R2, P = I²/R2 as well

    So in our example:

    P = 0.012*12 = 0.144 Watt

    Since this is more than 1/8 Watt (0.125 W), we should use a 1/4 Watt resistor for continuous duty. However, if the external device is not ON for long periods, but instead is switched regularly, it may be possible to use a 1/8 Watt resistor. Different physical size resistors can be installed by bending the leads appropriately.



©2006 Al Grippo and Bruce Bowling - All rights reserved. MegaSquirt® and MicroSquirt® are registered trademarks.