The general purpose I/O logic in MegaSquirt-II code version 2.3+ allows for using up to seven pins as user-programmable 'spare' outputs. You can use the spare ports to drive a transistor that runs a relay, etc. using programmable settings, and you can use your tuning software to indicate and datalog the spare ports.
Note that the LEDs, and especially the FIdle and IAC1,2 outputs already have transistors capable of conducting considerable current (check the transistor datasheets to be sure they are sufficient for your application). For other circuits you will have to add a transistor and associated components (see below).
Fast Idle Spare Port
The fast idle spare port is often the most convenient to use (especially if you have a stepper IAC, in which case this port is not used). The fast idle port can flow up to 1 Amp, has a connection on the DB37, and is already wired in most harnesses.
LEDs and Knock Enable Spare Ports
Note that you can use the LEDs 'as is' to indicate certain conditions, if you prefer (like shift lights, etc.). They can be relocated by extending the leads.
The LED and knock enable outputs need a transistor to drive a relay or a solenoid unless you just want to activate the LED, such as for a shift light, no changes are required to the hardware). The LEDs already have small transistors in place on the PCB, and these are capable of up to 0.5 Amps maximum.
In MS-II these go to actual LEDS through a transistor, so the current limit is relatively small - about 0.5 Amps. But on MicroSquirt® (at least on the V2 MicroSquirt® PCBs), the WARM-UP and ACCEL outputs (Ampseal pins 16 and 17) go through FET drivers with 5 Amp capability (IPS022G or VNS3NV04D). So these MicroSquirt® pins, when used as spare ports, are capable of driving more than the 0.5 Amp current than is specified for MS-II.
You can use any suitable NPN transistor, some possibilities are:
The transistor can be installed in the proto area, or externally. The transistor(s) is typically wired like this:
Transistor Max Current Digi-Key PN Cost 2N2222A 0.6 Amp 497-3106-5-ND $1.25 ZTX450 1.0 Amp ZTX450-ND $0.84
DB37 Pin |
Jumper |
Notes |
3 |
SPR1 |
Usually CANH |
4 |
SPR2 |
Usually CANL |
5 |
SPR3 |
Free |
6 |
SPR4 |
Free |
IAC Spare Ports
The following IAC outputs are capable of driving over 0.5 Amp directly, sufficient for most automotive relays. You can connect to them using the IAC pins on the DB37 (25, 27, 29, and 31). Also, note that IAC1A and IAC1B are inverted relative to each other, as are IAC2A and IAC2B.
The IAC pins come out at:
NOTE: PT6 (IAC1) and PT7 (IAC2) setting are reversed in MegaTune 2.25 (as of May 15, 2006), so PT6 in MegaTune actually affects IAC2, and vice versa. This will be corrected, so later versions of MegaTune may be correct. As always, you should TEST your configuration before using it in a car.
Also, after setting the second condition's value, you may have to hit the 'Tab' key to get the value to register.
The two spare port "T" pins (PT6 and PT7) are normally used to drive the stepper motor chip (IAC1,2). When you set pin PT6 high, it will make 1 of the 4 stepper output pins high and the other low, and no effect on the last two - which are controlled in the same way by pin PT7. So, by picking 2 of the 4 IAC outputs, you have two 12V spare pins that will directly drive about 0.5 Amps with no transistor needed. This is more than
enough to drive a relay directly. If you are going to use port PT6 or PT7 as spares (IAC1,2), be sure not to set the idle control algorithm to any of the stepper control options (''IAC Stepper Moving Only', 'IAC Stepper Always On', or '15 Minute IAC'). This will keep the the stepper chip 'always enabled' and not turn it on and off, which would prevent the port from working as intended.
The spare pins have generic logic based on the values of up to two of the real time display variables. The user can specify these in MegaTune and they will be passed to MegaSquirt-II as offsets. The user can also specify:
In the code itself, there are the following generic spare port parameters:
The other thing to note is that there is also the '=' condition. This probably won't be used much, but it means that if var1 = thresh_1, then var1 is in the range thresh_1 ± hyst_1. So for hysteresis it would be outside this range. This means if you were on the edge it could fluctuate in and out of range.
The last thing to note is if the condition which connects the two main conditions is left blank (instead of & or |), then it is assumed that only one condition, the first, is to be used.
To illustrate, the spare pin logic looks like this for combined greater than and less than conditions, linked by an OR:
if[(var1 > thresh_1) OR (var2 < thresh_2)] then set the pin to 'set pin value'; else if [(var1 < thresh_1 - hyst_1) AND (var2 > thresh_2) + (hyst_2)] then set the pin to (1 - 'set pin value)'; end if;
This says,
You set these conditions in MegaTune, under 'Settings/Port Settings':
For the Power-on value and Triggered value, these will depends on how you want the output to respond. For example, if you are using the LEDs as indicators:
So the triggered value can have the same effect as inverting the logic.
The power on value is the setting of the port when the power is cycled to MegaSquirt® EFI Controller, before any conditions are applied. It's value is in the same sense as the triggered value, but either 0 or 1 can be active, depending on the conditional settings, the MegaSquirt® port and hardware, and the external circuit (such as a grounded or powered relay, NPN or PNP transistor, etc.). Always test to be sure you have the settings correct for your situation!
For the FIdle circuit, the trigger value is the 'same' as the LEDs, setting it to one means the FIdle pin (DB37 pin #30) will be grounded when the condition(s) is true (and grounded means that current will flow through the device you have connected, turning it 'ON'). For the other circuits, the proper trigger setting will depend on how you have wired them (which transistors, etc.), you should determine the appropriate settings directly by testing the ports.
You must reinitialize the processor by cycling MegaSquirt's power in order for the settings to take effect, although things like the thresholds and hysteresis can be changed on the fly as much as you want with no problem.
Note: you should always test the operation of the port on the stim BEFORE using it in your vehicle. Failure to do so can result in damage to your engine or vehicle.
For var1 and var2 you can substitute rpm, map, tps, CLT temp, etc.
So you can specify something like:
This could be used to drive a water injection unit, for example.
This allows the TCC to engage only if the engine is warmed up (CLT>160°F) AND the load on the engine is low (kpa<75 kPa). Otherwise, the TCC is unengaged (if the hysteresis conditions have been met). (Note that this would NOT prevent the TCC from engaging in drive while stopped, and external gear selector and brake switches would be needed, as in any TCC installation.)
For example, if you wanted to use the FIdle output as a TCC control, and the accel LED as an indicator that the TCC is active, you would set:
Note that the FIdle circuit on the relay board is limited to 1.1 Amps by the polyfuse. The relay is capable of handling much more current, but the traces on the relay board are NOT. If you wish to use the FIdle relay, do not use it to drive a device that requires more than 1.1 Amps (though you can use it to drive a second relay and control much more current - like an electric fan relay)
Example: Torque Converter Clutch Control
Suppose you want to engage OD (overdrive) over 50 kPa MAP, and engage the LU (torque converter lock-up) clutch at 75 kPa MAP, provided temp is over 140 and RPM is over 2000. For a torque converter clutch control (TCC) using FIdle with 3 conditions (rpm > 1500, CLT > 160, map < 75), set:
PA0 - Knk Enable:
and
PM2 - FIdle:
Then use the FIdle output to drive the TCC relay (if you have a relay board, you are all set). Not that you also need a 3/4 gear indicator switch and a brake switch in the external circuit:
Example: Electric Cooling Fan Control
Another example is electric electric cooling fan control. We will turn it on 180°F and off at 170°F, using the relay board FIdle output to control a separate cooling fan relay (the traces on the relay board may not have enough current capacity to control your fan directly):
Of course you can change the conditions to suit your vehicle.
Note that changing the FIdle port settings (PM2) does not affect the operation of the stepper IAC settings and ports. You can still use the IAC stepper functions for controlling idle speed during warm-up (just select one of the stepper options for idle control).
Note that the settings will depend on whether you have an NC (normally closed) or NO (normally open) relay, so you must check that the fan works as you think it should.
Other conditions could be used to drive a nitrous solenoid, a shift light, or a water injection system, for example.
This won't cover every condition people can think of, but it should be easy for people to add more code for custom applications.
Chaining Spare Port Conditions
You can use the spare port variables port0 to port6 to link the status of one spare port to another, allowing you to implement control strategies that have more than 2 conditions. The port0 to port6 status is returned as a single variable representing a 7 bit field, that has a value. For example:
32 decimal is 0100000 binary, and
45 is 32) + (8) + (4) + (1 = 0101101
The trick is that while MegaSquirt-II uses the bits (0's and 1's) to set the individual spare ports (and indicate their status to MegaTune), it returns the fiels of bits as a decimal number (sort of like the datalog engine bit field).
For example, the PA0 - Knock enable spare port is bit 6 (numbered with 0 as the right-most), so to see if it is high, we check if port0 >63.
The spare port variable (spare_port) is initially set to 0 and whenever a spare port is set or unset, the program goes thru a set_spare_port subroutine in which the status of the bits in outpc.spare_port are updated, as well as setting the pin itself. You only call set_spare_port with port n as an argument if you have selected n as a spare port. If not, then spare_port does not reflect the value of port n.
If you want a condition that triggers when bit 6 is high, you can't use port_status > 0, you have to use port_status > 2**5.
You can chain spare ports in simple cases by making the condition be whether port 5 > 2**5 - 1 = 31 (or something like that). So regardless of the values on ports 0-4, the condition would only be met or not met if the 5th bit were set or not set. (This assumes port 8 is not used. You just have to order the highest ports to be the triggers.
The spare port status is logged in the datalog and can be used for indicators in the tuning software. The spare ports are numbered as follows:
CPU Port | SW Port |
PM2 | 0 |
PM3 | 1 |
PM4 | 2 |
PM5 | 3 |
PT6 | 4 |
PT7 | 5 |
PA0 | 6 |
Port_status is the combined one number status in binary. The port_status, this is calculated by putting the 0/1 state of each port in a single 8-bit memory location going from right (port0 = bit0) to left (port 6 is bit 6 and leftmost bit 7 is always 0 since there are only 7 not 8 spare ports).
You evaluate a binary number like this as follows:
(bit7×128) + (bit6×64) + (bit5×32) + (bit4×16) + (bit3×8) + (bit2×4) + (bit1×2) + (bit0×1)= (0×128) + (PA0status×64) + (PT7status×32) + (PT6status×16) + (PM5status×8) + (PM4status×34) + (PM3status×2) + (PM2status×1)
Bit1, bit2, ... are 0 or 1 (off or on) so all you do is add up the numbers for the bits that are on. So if only bit 6 (i.e. PA0 is on) was on you get 64, hence the condition > 63. Since we are dealing with whole numbers, > 63 means 64 or higher.
In the INI there should already be a section like this:
portStatus = scalar, U08, 70, "bit", 1.000, 0.0 ; Spare port status bits port0 = bits, U08, 70, [0:0] port1 = bits, U08, 70, [1:1] port2 = bits, U08, 70, [2:2] port3 = bits, U08, 70, [3:3] port4 = bits, U08, 70, [4:4] port5 = bits, U08, 70, [5:5] port6 = bits, U08, 70, [6:6]This allows use to use indicators of the form:
indicator = { port0 }, "Port 0 Off", "Port 0 On", white, black, red, black
Note the you can change "Port 0 Off" and "Port 0 On" to read however you like ("TCC ON", "FAN on", "SHIFT NOW!!!!!", etc.).
You can decode the portStatus variable in the datalog,, or you can add the portX variable to the datalog. To add port6 (PA0) for example, in the [datalog] section of the INI add:
entry = port6, "pt6", int, "%d"
The values will appear in the datalog labeled as pt6 (you can change this to anything you like), located in the sequence you put it into the entries in the [Datalog] section of the INI.