Rotary Encoder to measure distance traveled??


I was wondering if I can use a rotary Encoder to measure the distance something travels linear, I suppose I'm stuck on the sketch here, I hope someone can help me...

Say my encoder counts 20-pulses to equal 360-Deg, and for every 360-deg the object attached to the encoder travels 10mm, how do I show if the encoder has only moved 180-deg or 5mm from a switch?

Does this still have something to do with PID?

Thank you all in advance for any and all help.


If the encoder shaft is attached to a wheel, you can calculate linear distance that the wheel rolls from the counts. distance = (wheel circumference)*counts/(counts per complete revolution)

For accurate results use float variables.

Sorry but can you help me with an example? Also if you have some time and if you don’t mind can you elaborate on float Variables?
Are float variables related to PID?

So the variables I know are:

20-counts = 360-deg
360 deg = 16 mm (circumference)

Arduino is collecting the number of counts from the switch so that’s an unknown variable…

Distance = 16xcount/20 …

How do I fill in the (count) part of the equation?

Sorry I just don’t know how to put the sketch together… Any suggestions or references?

Thanks so much for the help and time.

First you need to connect your encoder to an Arduino, get it working and understand what is happening. Here are two tutorials; google "arduino rotary encoder" for many more.

you can try this sketch, expand it as you like I hope to ear you soon

#define encoder0Pin               2

volatile int count, countold;

void setup()
  count = 0;
  countold = 0; 
  pinMode(encoder0Pin, INPUT_PULLUP); 
  attachInterrupt(0, sensor1, FALLING);

  Serial.begin(115200); // initialize serial communication:


void loop()
  if (countold != count)
       countold = count;

void sensor1()