Click on our menu buttons immediately below to find MegaSquirt® information quickly:


MegaShift™ ADC Calculations

When setting up the MShift™/GPIO ADC channels for 'non-standard' usage, there are a number of considerations:

  1. If you are using the sensor as one leg of a voltage divider, then you need to consider what the resulting voltage signal is. If the pull-up "bias" resistor is Rb, and the sensor resistance at a given time is Rs, then with supply voltage Vs (generally this is Vref = 5V), the voltage signal V to the CPU is:

    V = Vs×Rs/(Rb + Rs)

  2. If the supply voltage is more than 5 Volts, then you have to make sure the signal voltage is never more than 5V. If the supply voltage is 5V, then you are safe.

  3. The signal voltage is divided into 1024 ADC counts (for the default 10-bit conversions of the GPIO). 0 counts is zero volts, 1023 counts is 5.00 V. So the ADC count you will get from a given voltage signal is:

    ADCcount = V×1023/5

  4. TunerStudioMS gets the ADC count directly in most cases. In the INI, you can convert the ADC count to anything you like with the appropriate conversion math. For example, if the above calculation show that at 60 psi you get a signal of 2.5V, and at zero psi you get 0V, then you know:

    So if the relationship is linear, the conversion is OilPress = 60/512 = 0.117 × ADCcount

    In the INI you find this in the [OutputChannels] section. There are lines like:

    OilPress = scalar, U16, 21, "volts", 0.00488, 0.0
    

    Where 0.00488 and 0.0 are the factors you would change (0.00488, 0.0 changes a 1024 ADC count value to a 0-5V reading).

    And where "OilPress" is the name TunerStudioMS will use for the value calculated from the information returned from the controller. ('scalar' means the magnitude is important, not necessarily the individual bits; and 'U16' means it is an Unsigned integer with a length of 16 bits - i.e. a value between 0 and 65535 = 216).

    21 is the offset, in bytes, that the relevant value is from the start of the outpc structure (aka. the "Output Channels"). Other offsets for ADC channels are:

    Channel
    Offset
    clt19
    linepressure21
    aux_volts
    (already returned as Volts, not an ADC count)
    27
    swBDC39
    swCDC41

    The exact line you change depends on which ADC input you used for the oil pressure sensor (if you used the linepressure input, the line has offset of 21, but if you used the line pressure input, you must set the software to return the ADC count and not the pressure). You can call it anything you like by changing the 'OilPress' label. You can edit the INI with any plain text editor, such as Notepad. The calculator below will help you to determine the appropriate values to enter.

  5. Then under [GaugeConfigurations] in the INI, you can add a gauge directly, something like this:

    ; Name  = Case-sensitive, user-defined name for this gauge configuration.
    ; Var   = Case-sensitive name of variable to be displayed, see the
    ;         OutputChannels block in this file for possible values.
    ; Title = Title displayed at the top of the gauge.
    ; Units = Units displayed below value on gauge.
    ; Lo    = Lower scale limit of gauge.
    ; Hi    = Upper scale limit of gauge.
    ; LoD   = Lower limit at which danger color is used for gauge background.
    ; LoW   = Lower limit at which warning color is used.
    ; HiW   = Upper limit at which warning color is used.
    ; HiD   = Upper limit at which danger color is used.
    ; vd    = Decimal places in displayed value
    ; ld    = Label decimal places for display of Lo and Hi, above.
    
    ;Name                 Var             Title                   Units     Lo     Hi     LoD    LoW   HiW   HiD vd ld
    
    ...
    oil_pressure       =  OilPress,       "Oil Pressure",         "psi",    0,     100,   5,     15,   80,   90, 0, 0
    

    The conversion is already taken care of when OilPress is returned from the controller by the equation we used above.

  6. You can also add the new value to your datalogged values. In the [Datalog] section of the INI, enter a new line like:

    ;       Channel            Label          Type    Format
    ;       --------------     ----------     -----   ------
    ...
    entry = OilPress,          "OP",          float,  "%.2f"
    

    anywhere you like (the datalog will contain the values in columns in the order you list them. The entry tells TunerStudioMS to datalog the variable OilPress with the heading OP as a floating number (not an integer) with 2 decimal places (%.2f).

Here is a calculator to assist in calculating the resulting voltages, ADC counts, and INI factors (it assumes a linear relationship):

Inputs:
Voltage, supply (Vs): Volts
Resistance, Rbias Ohms
ADC resolution: (10-bits for GPIO)
Resistance, Rs1 Ohms Resistance, Rs2 Ohms
Pressure 1psi/kPa/... Pressure 2psi/kPa/...
Outputs:
(Your can enter the sensor's output volts for P1 & P2 directly, and bypass the resistance calculation if you are using a voltage based sensor.)
Voltage signal 1Volts Voltage signal 2Volts
ADC count 1 ADC count 2
INI factors: Multiplier: (aka. "slope")Constant: (aka. "Y-intercept")

For the INI factors, the format is :

Name = scalar, U16, offset, "units", multiplier, constant

or in the case of the default values from the calculator:

OilPress = scalar, U16, 21, "psi", 0.175, -29.825



MegaSquirt® and MicroSquirt® controllers are experimental devices intended for educational purposes.
MegaSquirt® and MicroSquirt® controllers are not for sale or use on pollution controlled vehicles. Check the laws that apply in your locality to determine if using a MegaSquirt® or MicroSquirt® controller is legal for your application.
© 2011 Bruce Bowling and Al Grippo. All rights reserved. MegaSquirt® and MicroSquirt® are registered trademarks. This document is solely for the support of MegaSquirt® boards from Bowling and Grippo.