How to program an Epson Optical Encoder with Arduino
- By
- On
- Comment
This video is for those who want to make their printer, such as a DTG printer or cake printer, or maybe an industrial printer. When you do a printer, you have to deal with those optical encoders. Today I'm going to show you how to work with an optical disk encoder. You can see on the disk; there are little marks. When the disk rotates, the mark going to deflect the light. Suppose you don't have the background, just Google optical encoder. There are lots of articles that explain how it works. On one side, you have an LED that shines a light, and the other side that receives it.
Today's sensor is from Epson, which is they have two photosensors on each side. You will not only measure the speed; you can also measure the direction of the rotation. Don't worry about the concept right now. I'm going to show you, and it's straightforward to understand later.
This disc is mounted on the other side of a motor. When the motor shaft rotates, the disk also rotates. Here's the encoder. You can see one side is emitter and another one is received light. Notice the output. There are four pins. You get one pin for the ground and one pin for the power for the VCC. Then, you left two more pins. One pin is for sensor A. Another pin is for sensor B. It doesn't matter which one, which is A, which is B. When you program, you're going to find out quickly. All those units are built together, and it uses the Mabuchi FK-130SH motor, and you can find it at bchtechnologies.com.
Go to printer parts, Epson, and motor. Today we're going to talk about this DEI-P91 optical encoder. In the encoder, if you need any files that we're going to talk about today, click Arduino Sketches, and it will bring you all the code that we're going to cover. If you don't have Arduino or never had any experience with it, go to Amazon and search Arduino starter kit and buy the cheapest. Today, we're going to use this one as the mainboard and has that power regulator. For example, I have a new project requiring an RGB sensor to tell the color on the substrate. I need this RGB sensor. It's under $10. A couple more things, maybe $50 later, I can build a machine that right now is selling for about $1,000. To give you some hint, oh, it's more than $1,000. That's how much it costs.
You got your Uno board, and don't worry about you don't understand it. Just pay attention, this side has many numbers, like one, two, three, four, and that's the pins. I made a little bit homemade adapter cable for those four pins coming out from the optical sensor. The line is from the original Epson cable. Epson uses one white wire. They already tell you this is into the ground. Now I have three wires left. The middle one is the power, so the middle one was supposed to plug in some power source. Then went and right, they are pin A and pin B.
For the board, you plug in your computer's USB cord, and those two pins were going to provide power. One is a 3.3 volt. One is 5 volt. You can use any of those grounds as ground. Those number pins, you can use either as input or output. Our first plan is to connect the white wire, the ground, to the ground. Then we link the middle, the red wire, to the three volts. You can connect five volts. I tried five volts works too. Then the two data pins will connect to PIN two and number three, and then we tell the computer we're going to input something and let the computer show us.
White connects to the ground, middle to the power, the three volts, and leftover, one to the two-pin, one to the three-pin. This server is a free download, and you can get it from the kit you bought from Amazon. First, we tell the computer we put pin A in two, pin B in three. The rotary position is the reading that comes from pin A, and I have a variable to hold it. The previous reading of the pin A, I'm saving as previous rotary position. When I compare the two-position, I know if the disk is turned or not.
In the setup, we tell the computer that we're going to read from pin A, and also, we're going to read from pin B. We're going to start a serial communication, so we know what's going on. The rest of the setup is just a couple of prints trying to show us. The loop is the board, going to execute forever, and they keep looping. When I see the previous value and the recent value changed, we know the disk has been rotated. I'm going just to print the output and tell us the position of the two pins. We click upload, and that's going to upload the code into the board.
Remember when the server begins to start a communication with the board to open up the server window, so you can see the first part of the code that is executed. Now, when I turn up the disk, the computer compares two values. If they're different, and only they're other than the previous value. I print out this A B position. If I turn one way, the two always value the same, but the value will be different if I turn the other way. That's how you detect the direction of the turning. Now we can catch exactly when the disk turned, and the direction of turning, and we can calculate speed, and we can do all sorts of things.
That program will work well if your computer doesn't do anything else, just sitting there, waiting for you to turn a disk. In reality, your computer will do a whole bunch of things, so when they want to do another thing, you'll turn a disk, and it's not going to record it. To be able to record all the changes, you'll need something called an interrupt. Here's the second script. It sounds fancy, but it's straightforward to understand. Interrupt means stop whatever you're going and do the thing I'm telling you to do right now.
First thing, we need something called volatile. This number always changes, and we're going to use this number, which is a variable, storing the number marks the disk turned. After that, it's easy. Pin A is two, and pin B is three. Here we define the interrupt function to tell the computer what to do when there's an interrupt. We read from pin A to a variable ii; we read B into it. If the two numbers equal, that's one direction. If it's not identical, that's another direction. Every time I get interrupted, the number of marks increases by one. The setup is still the same set of serial and connection, pin A and pin B, they're input, and here is the function, attach interrupt. That tells the computer I need an interruption.
The zero is port zero. The zero-port always connects to PIN two on the Arduino board Uno. If you use Mega or something, you have to check which zero to go to. Always put a zero there, and physically when you connect, also still connect to your interrupt pin into number two. The ISR is the ISR function we defined. If we described the procedure as another name, like Kevin, we have to write this as Kevin. The word change means I don't care if it's changed from zero to one or one to zero. Just give me an interruption. You can also use the word rising or falling, which only changes the direction.
For example, rising is from zero to one. In this case, we know it can change both directions. We use the word change in the loop, and you can compare it to the last loop. In the loop, it just does one thing and only prints the number marks. That is the whole point. In the loop, you do whatever you want and adjust whenever pin A changes the voltage. The system immediately jumps into the interrupt routine and does the interrupt, and then returns to you. You'll never miss a moment that you turn the disk. Here's how it looks like. If I turn fast between the printing and the number already added. Okay, we did this part. Now we're going to throw in the motor.
You cannot drive the motor directly from the Uno board. However, Uno has a power source. The reverse current from the engine will fry this board in a second. We're going to use a controller chip. This chip takes the little current from the Uno board and controls the significant current on the motor. I'm going to use this L293D as an example, and you can say that 10 of them is like 8 hours. If you do many things, I suggest L298N, which is a little bit better than this. We'll put a chip on the breadboard. Here's how the breadboard works. On both sides, there are power and ground. All those are connected.
You put one in the ground, they are all grounded, and the one below it, the red line, is the power. You put a power source there. Anybody needs power. You can attach to that power, and you can get that power. For example, I need nine-volt here to connect a line from the nine-volt to the nine-volt power. All those columns, the four of those are connected vertically. They're all connected. If I want to connect anyone to this lag, I can join any of those three slots left. Here is how this chip works. The chip has a dent here. Underneath the dent is number one and then number two and number three. Those two lags should go to the ground, and you can connect to a giant heatsink. Number one is the "enable." You're plugging a computer cable, then you can use the computer give a value, for example, 255 is the highest speed, and you give to other speed you get a zero, then the motor can shut off. Which I think is speed control, and then those are two lags that I can connect with the blue line, and there are four directions. You put one of them in high, one of them is low, it goes in one direction. If you look vice versa, if you put another one high, this one low, it'll come in a different direction. I control the movement of the motor turning.
Those three lines, the enable line, and those two lines control the speed and control the direction. The furthest to the right is the one and the red line you connect with the high power. You can connect to nine volts or 12 volts power. We only got the two lags not connected. Those two lags go to the DC motor. It doesn't matter which direction it goes because you can control the movement. You can see we only used half the chip because this chip is designed for two motors. You can control another motor on the other side. You'll find that you need a just use one with a squeaky symbol next to the number for this speed control.
Two wires to give directions, then ground. Then put those two blue ones to the motor, and the last one is for the power to the motor. Your Arduino package comes with a power adapter. We can connect to our nine-volt battery, and you can fit right on the breadboard. Then it powers on both sides of the breadboard and also, you can control the voltage of each one. For example, I want this to be a three-volt. You just move the pin to the three to connect the two pins on the three-volt side. This time, you can use the five-volt. It's a little bit underpowered for this motor, but that's all I have.
Okay, let's start. Pretty easy number marks, and this time, I also calculate several turns because there are too many marks, and they'll be overflowing. The motor "enable" is at pin 11. Enable is the one you control speed and then the two other control directions at ten and nine. Then from the optical pin A is on the two and pin B as a three. Remember the interrupts for the Uno board. You have to put on the two, and here's the interrupt function. I read from optical pin A and optical pin B if they equal, then it is one direction. If they're not equal, there's another direction. If the number of marks is more than 100, that means one full rotation. I just set it to zero.
Then I increase the number of turns by one, and the setup is just a boilerplate. This pin is an input that pin is output. Attach the interrupt and give me serial communication. Motor A and motor B are going to control the direction. One is high. Another one must be low. However, I can just put it in the setup because we do not change the direction in this example. Here, I define a variable ii, which I use the ii to control the speed. I sign ii to the maximum. Then one is larger than 125. I wonder if Y 125 is because I found this motor stalls if it's lower than 125. Yes, maybe I should give a higher voltage so it won't stop, but you can try it with a larger voltage.
It will start with high speed, then get slower, slower every time by step of 10 and output at ii value, which is the arbitrary number you gave to the enable wire. Remember the speed that we use here. It's just that the random number does not turn. It is only an arbitrary number from one to 225 will give to the controller, but the number of turns is the one that I calculated from your interrupt function. Next example, we're going to use this one as a step motor. The step motor is not like RC motors. Step motor has more wires and is more complicated. The advantage of the step motor is it can precisely control the angle it turns.
A step motor is used for 3D printing; however, it's bulkier and more expensive. Here I define an AX to control the marks. I turn the motor off by giving that a wire (enable pin) a zero voltage. Then I say if you're at quarter one mark, stop. Then the next time you're at a quarter two marks, you stop. Then three marks, you stop. That's going to control precisely how much the motor can turn. It works—pretty cool stuff.