Linear Mapping of Input Range to Output Range

Whenever you mix sensor input values and electronics, you always get a situation where the data read from the sensor needs to be interpreted or changed or scaled to be used by your control software.

Mapping or scaling a linear range of input values to a linear range of output values is as easy as: $y=mx+c$

Let us look at an EXAMPLE:

You want to control a hobby servo, but your control program uses values from 0 to 10 (0 being the left most position and 10 being the right most position). However a hobby servo uses pulse width modulation (PWM) at a specific frequency (50Hz ie: 20ms) to control the position of it's horn. A pulse width of 1ms will turn the horn completely to the left and a pulse width of 2ms, completely to the right.

You need to map your control input (0 to 10) to pulse width (1ms to 2ms). Since the relationship between input and output is linear this is easily accomplished by using the straight line formula: $y=mx+c$

First establish the values on the x-axis, which will be your input values. This will be the range: 0 to 10

Next, establish the values you want as an output, which will be on the y-axis and the range: 1ms to 2ms.

This gives you 2 points on a cartesian plane: Point 1: $(0,1)$ and Point 2: $(10,2)$

Use these points to calculate the slope of the straight line (m):

$m=\frac{(y_2-y_1)}{(x_2-x_1)}$

and calculate the y-axis intercept (c):

$c=y-mx$

Substituting the values from Point 1 and Point 2 into the m and c equations give us:

$m=0.1$

and

$c=1$

and a final formula of:

$y=0.1x+1$

This is the formula you will now use to map your input value range (0 to 10) to your output value range (1 to 2)

[CP_CALCULATED_FIELDS id="1"]