Program Arduino Optical Encoder

Program Arduino Optical Encoder

Today's topic will focus on those of you who think you might be interested in building your own printer.

When you build a printer, you'll have to deal with something called an optical encoder. What I'll be showing you today is a disc optical encoder.

If you look closely at the disc, you should see that there are little marks. When the disk rotates, these little marks will deflect light. If you're interested in learning more about how this works, there are a lot of helpful articles that come up when you search "optical disc encoder" on Google.

To put it simply, there will be an LED on one side that shines a light at the optical encoder disc, which will then be received on the other side.

The sensor we'll be working with today is from an Epson printer. This particular model has 2 photo sensors on each side. This means that it can measure the direction that the disc is rotating in, as well as the speed at which it does so.

The disc will be mounted on the side of a motor. When the motor shaft rotates, it will also cause the disc to rotate. When you look at the encoder, you'll see that it has the emitter on one side and the receiver on the other side.

Align the Pins

There are 4 pins you should see. One is for the ground, one is for the power, one goes to sensor A, and the last goes to sensor B. All of those units are built together and it uses a Mabuchi FK-130SH motor, which you can find at

When you're on my website, go to Printer Parts> Epson> Motor. The thing we're talking about today is the DEI-P91 optical encoder.

If you need any files for the encoder we're using, all you have to do is click on Arduino Sketches. This will bring you to a page showing all of the programming we're going to cover.

If you don't have Arduino and/or have no experience with it, go to Amazon and buy the cheapest Arduino starter kit you can find.

Once you get the board, don't worry if you don't understand it. What you want to pay attention to right now is the side that has all of the numbers on it. Those numbers are there to correspond to the pins.

For this project, I made a little bit of a homemade adapter cable for the four pins coming out of the optical sensor. The cable I used is actually the original Epson cable. Epson uses one white wire which they'll tell you is the ground wire. Of the three remaining wires, the middle one is for the power supply, and the left and right wires go to pins A and B. It doesn't matter which is which.

Using the Board

For the board, start by plugging in the computer's USB cord. These two pins will provide power to the board. One is a 3.3 volt, and the other is a 5 volt. You can use any of these as grounds. The number pins can function as either your input or output.

Start by grabbing the white ground wire. This, obviously, is going to be connected to the ground pin. Then find the red middle wire and connect it to the 3.3 volt pin. It would also work if you connected it to the 5 volt pin, based on my testing.

The two data wires will be connected to pins number two and number three. The software you need next can be downloaded for free. We'll use this to tell the computer that we put pin A in two and pin B in three.

The rotary position will be the reading you get from pin A. For now, I just have a variable to hold it. I saved the previous reading of pin A as a rotary previous position. This will allow me to know if the disc has turned or not when I compare the two positions.

In the setup, we're going to tell the computer that pin A is where we're going to get the reading from. We'll also be reading from pin B. Now we'll start a serial communication so that we know what's going on.

For more information about the rest of this process, watch my more detailed video on my YouTube channel, Kevin at BCH.

Happy printing!

Jun 22nd 2022

Recent Posts