• Sensor Scaling

    Scaling an analog sensor is not a difficult as it may seem. Even when zero units is not zero volts.




    Start off by using the formula:
    y = mx-b
    where
    y = output Volts
    m = slope
    x = input units
    b = offset

    Let's use a pressure sensor that has a range of 0 psi to 100 psi, with 1 volt output equaling 0 psi and 5 volts equaling 100 psi.

    m = slope
    There is a 4 volt span. psi/volt 100/4 = 25
    m = 25

    x = input units
    This is the output from the sensor, in this case it will be between 1 and 5.

    b = offset
    Since the output starts at 1 volt, there is an offset. We calculated a value of 25 psi/volt.
    b = 25

    Plug all this into the formula and lets say the input is 5.0 volts.(full scale)
    y = (25 * 5) - 25
    y = 100 psi

    Convert this to an 8 bit PIC® resolution.

    8bits ADC = 0 to 255, 256 steps.
    At 5.0 volts one volt of input will will read 51.

    New value for "m"
    Spanning 4 volts or 204 steps (4 * 51).
    100 / 204 = 0.49
    m = 0.49

    New value for "b"
    The new offset starting at 51, (1 * 51).
    51* 0.49 = 24.99
    b = 24.99

    ADC is from the sensor.
    ADC = 255 Full scale
    y = (ADC * 0.49) - 24.99
    y = 99.96 psi

    #############

    Convert this to an 10 bit PIC® resolution.

    10 bits ADC = 0 to 1023, 1024 steps.
    At 5.0 volts one volt of input will will read 204. (204.8 actually)
    But we will use 205 for accuracy.
    204 * 5 = 1020
    205 * 5 = 1025

    New value for "m"
    Spanning 4 volts or 820 steps (4 * 205).
    100 / 820 = 0.1219
    m = 0.1219

    New value for "b"
    The new offset starting at 205, (1 * 205).
    205 * 0.1219 = 24.9
    b = 24.9

    ADC is from the sensor.
    ADC = 1023 Full scale
    y = (ADC * 0.1219) - 24.9
    y = 99.8 psi

    Now for some code. You will see some rounding for integer math.
    8 bit
    Code:
      '  m = 0.49
      '  b = 24.99
      '  y = (ADC * 0.49) - 24.99
      '  ADC = 255
      '  y = 99.96
        M   CON 49       'm = 0.49 * 100
        B   CON 24       'b = 24.99 
        ADC VAR WORD     'INPUT FROM SENSOR
        Y   VAR WORD     'PSI
        Z   VAR WORD     'DUMMY VAR
        Z1  VAR WORD     'DUMMY VAR
        
        START:
        ADC = 255  'FULL SCALE HARD CODED, NO SENSOR
        Z = ADC * M
        Z1 = DIV32 100
        Y = Z1 - B
        LCDOUT $FE,1,"PSI= ",DEC Y
        PAUSE 250
        GOTO START
    10 bit
    Code:
      '  m = 0.1219
      '  b = 24.9
      '  y = (ADC * 0.1219) - 24.9
      '  ADC = 1023
      '  y = 99.8
        M   CON 1219     'm = 0.1219 * 10,000
        B   CON 249      'b = 24.9 * 10
        ADC VAR WORD     'INPUT FROM SENSOR
        Y   VAR WORD     'PSI
        Z   VAR WORD     'DUMMY VAR
        Z1  VAR WORD     'DUMMY VAR
        
        START:   
        ADC = 1023  'FULL SCALE HARD CODED, NO SENSOR
        Z = ADC * M
        Z1 = DIV32 1000
        Y = Z1 - B
        LCDOUT $FE,1,"PSI= ",DEC Y/10,".",DEC2 Y//100
        PAUSE 250
        GOTO START