# Calculate speed from encoder counts

I'm using the Arduino Encoder library to keep track of my motor's encoder counts. I need to calculate the motor's speed so it can be compared to a target speed and subjected to PID.

I've thought of one approach:

1) Calculate counts per timestep - Let's say we have a timestep of 100ms and we desire a speed of 50cm/sec. Determine speed in timestep units, 50/10.

To calculate speed motor is traveling at, simply calculate how many counts since last timestep, which will give speed in timestep units, which can be directly compared with desired speed;

speed = counts - prevCounts;
prevCounts = counts;


The advantage I can see with this approach is that it doesn't require us perform division (i.e. speed = (counts - prevCounts) / 0.1 ) in each timestep.

Is this a sensible? Are there any flaws in my method?

• "1) Calculate counts per timestep - Let's say we have a timestep of 100ms and we desire a speed of 50cm/sec. Determine speed in timestep units, 50/10." I don't really understand how you can link counts per timestep and desired speed... Isn't there a homogeneity issue? Maybe it is just really simple, but I don't understand how do you obtain this formula. – user191567 Jun 18 '18 at 10:14