# FORMULA TO CONVERT ADC VALUES (0-255) TO SERVO POSITIONS (75-225)

Guides you when doing the math inside the code.

The need to convert the result of the ADC pins proportionally to servo positions arose recently in my Pic-Aim project, which in turn was inspired by this comment. In it, user chench shared the following formula to convert one into the other:

b2 = (b1 / 2) + 75; convert from 0-255 to 75 - 203

Which works beautifully well.

However if you are looking for more accuracy -specially when refining and debugging code-, there is some room to improve on the given formula. I gave it to my younger sister who is a lot better at math than I am, and in 8 seconds flat she came up with the following:

y=ax+b

...where:

• Y= is the servo position
• A (constant)=(225-75)/255 or 0.588235
• B (also constant)= 75

A little implementation: Let´s say you want to calculate the servo position that would result from an ADC value of 111. All you have to do is replace the explained components with their values:

y=(((225-75)/255)*111)+75

y=(0.588235*111)+75

y=65.294085+75

y=140

As you can see, the difference with Chench´s formula is minimal, but it is there.

Hope this helps any of you who finds themselves in the same situation I was in last week!

Cheers,

Andrés

NOTE (July 28th, 2011): This formula will only work correctly with exponential potentiometers. If you are using linear ones, the servo response will not be full range.