# Planning to make a pen-like device which has a roller rolling on a surface and measured its linear distance travelled

I am planning to use a microcontroller to do the brain works of receiving pulses from the encoder and compute it to output linear distance and at the same time indicating the measured distance as short, medium or long (of course that means I'll write a simple program for classifications/ranges of distances).

The encoder I chose is of reflective type.

My idea is similar to the case of a digital vernier caliper where the distance measured are constantly displayed on the lcd screen.

I suppose the way to do it will be something like to count the number of pulses over the entire length of signal? How to make the microcontroller to do that? will the microcontroller reads a binary number of 10101010... as the signal? if so, all I have to do is to translate these binary number to decimal then subsequently extract the angular displacement?

please tell me whether am i right lol

• Keep in mind that with only one pulse stream, you cannot tell which direction the wheel is rotating. Thus, if you roll it 1" in one direction and one 1" back, the wheel is back at the start point but your microcontroller thinks it has moved 2". To handle this you need at least 2 bit streams and preferably 3. Search for "grey code". May 28 '14 at 5:50
• Well, you could look into the designs used for the old mechanical mice. They track in 2D and are pretty precise, if you find some background information you could adapt the technology to your need. May 28 '14 at 12:54

The encoder will give you a pulse (the digital pin will read a 1) after the ball has rotated a specified amount in degrees, so you will have to mathematically calculate exactly how much linear distance the ball has travelled per pulse. It would probably be best to use an interrupt to increment a counter each time a pulse is detected, then just multiply the number of pulses seen by the linear distance travelled per pulse and you will have the total distance travelled.

You will need to setup one of the pins on the microcontroller to use an interrupt and program an interrupt service routine which is the code that will execute each time a pulse is detected, you wont really be reading in 10101010... There is a lot of info online on how to use interrupts, counters, etc.

• This is unlikely to work well as described, unless the encoder is free of "bounce" and the motion is unidirectional. Aug 1 '14 at 12:44

If you're still working on this, this sounds like the perfect case to use the optical sensor in a mouse. You can take apart a mouse you have laying around or buy the sensor separately. Here's a link to one such sensor though there are many more out there: https://www.sparkfun.com/products/12907 Key specs are running at 5V and 400 cm per inch resolution.

You can use an opto-coupler like QVE11233 and a disk like the one in following picture. When you roll the disk, it would simply ON/OFF the signal between QVE11233. If you simply implement a counter, and multiply it with the distance between two counts, you can get the distance travelled.

This might not be the best way as the accuracy is the distance between two solts. You can have a bigger disk with a transmitter and receiver diode implemented separately, to achieve better accuracy. This can be done like

This is the most simplest way to calculate the distance with an accuracy of 2mm.

• That second photo looks like two sensors mounted to get quadrature readout (for direction), with interruptor rather than reflective sensors. May 28 '14 at 4:38
• The photograph is meant for probable representation of final assemble and not for step by step guide. Sorry for not mentioning this before. May 28 '14 at 5:01