Rotary encoders using Gray code

Servos often use potentiometers to measure the position. That works fine if you have a good quality potentiometer. A different alternative is to use an optical or mechanical decoder. You can find examples of 3D printed rotary encoders here:

It's made in two variants.

Natural encoded ( i.e. simply using binary form where a hole represents 1 and no hole = 0). 

Gray code. Gray code has the advanage that only one bit changes for each step of the rotation.

If anyone want to test this type of encoder, I made two methods that can be used to convert from Gray code to decimal, and also from decimal to Gray.

It's based on this algorithm:

You can find an example Python script here:


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hairygael's picture

I have kept some disk

I have kept some disk encoders savaged from paper printers some time ago and I knew it would have a purpose some day. They must be very cheap because it's only a transparente disk with a grey outlining. You can see the black encoder mounted on the brown pcb.

Though, I have no idea how to connect the four white wires to my H-bridge driver and Arduino.

A link to disk encoders made with PET.

GroG's picture

my god the resolution of that

my god the resolution of that is impressive...

its a single channel digital encoder .. there should be "at least" 3 wires

1 power, 1 ground & 1 signal ...

very nice fine .. very fine resolution too, since from the picture it looks like a solid grey color ..

Mats's picture


Yoy probably have direction too. 

GroG's picture

I can't see, usually you have

I can't see, usually you have 2 printed channels for direction ..

but you could have 1 channel and 2 ir transistors which are out of phase

regardless, for 2 direction you should expect 2 signal lines plus ground and power