 Hey guys, Kevin here. This video is for those people who want to make your own printer. For example, a DTG printer, or a 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 a disk optical encoder. You can see on the disk, there's little marks. When the disk is routed, the mark is going to deflect the sunlight, not sunlight, it deflects the light. If you don't have the background, just google optical encoders, there are lots of articles explaining how it works. Basically, you have one side, you have LED, that shines the light, and the other side receives it. Today's sensor is from Epson, which is they have two photo sensors on this side. So 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 really easy to understand later. This disk is mounted on the other side of motor. When the motor shaft is routed, the disk is also routed. Here's the encoder, and you can see one side is emitter, and another one is received light, and notice the output, there are four pins. You get one pin for the ground, and one pin for the power for the VCC, and then you have two more pins, and one pin is for the sensor A, and another pin is for the sensor B. It doesn't really matter which one, which is way, which is B. When you program, you're going to find out really easily. And all those units are built together, and you use a Mabuchi FK130SH motor, and you can find it at bchtechnologies.com, and go to printer parts, and Epson, and motor. And today we're going to talk about this DIAP91 optical encoder. And in the encoder, if you need any files that we're going to talk about today, just click the Arduino sketches, and it will bring you all the code that we're going to cover. If you don't have an Arduino, or never had any experience with it, go to Amazon, and search Arduino start kit, and just buy the cheapest. And today we're going to use this one has the main board, and also has the power regulator. For example, I have this new project that I need an RGB sensor to tell the color on the substrate. So I need this RGB sensor, it's another $10. And a couple more things, maybe $50 later, I can build a machine that right now is selling for about $1,000. Just to give you some hint, oh, it's more than $1,000, that's how much it cost. OK, you got your Arduino board, and don't worry about you don't understand it. Just pay attention to the side, it has a bunch of numbers, like 1, 2, 3, 4, and those are the pins. I made a little bit of homemade adapter cable for those four pins coming out from the optical sensor. The cable is from the original Epson cable. So Epson uses one white wire, they already tell you this is ground. OK, into the ground. So now we have three wires left. The middle one is the power. So the middle one was supposed to plug in some kind of power source. Then left and right, there are pin A and pin B. For the board, you plug in your computer's USB cord. And those two pins going to provide power, one is a 3.3 volt, one is a 5 volt. And you can use any of those grounds as ground. And those number pins you can use either as input or output. So our first plan is connect the white wire, the ground, to the ground. And then we connect the middle, the red wire, to the 3 volts. You can connect 5 volts, try 5 volts works too. And then the two data pins, we just connect to pin number 2 and number 3. And then we tell the computer, we're going to input something and let the computer show us. OK, white to the ground, middle to the power, the 3 volts. And left over, one to the 2 pin, one to the 3 pin. This software is free download and you can get it from your kid, the kid, your brother from Amazon. So first we tell the computer, we put the pins A in 2, pin B in 3. The rotary position is the reading come from pin A. I'm just have a variable to hold it. And the previous reading of the pin A, I'm saving as a rotary previous position. So when I compare the two positions, I know if the disk is turned or not. So 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. And then we're going to start serial communication so we know what's going on. And the rest of setup is just a couple of prints show us. The loop is the board going to execute forever and just keep looping. So when I say the previous value and the recent value changed, we know the disk has been routed. So I'm going to just print the output and tell us the position of the two pins. So we click upload and that's going to upload the code into the board. Remember we'll use a story begin to start our communication with the board. So we can open up the serial window. So you can see the first part of the code executed. So now when I turn up the disk, the computer compare two values. If they're different and only they're different than the previous value, print out this AB position. So say if I turn it one way, the two value is the same. But if I turn it the other way and the value will be different, that's how you detect the direction of the turning. So now we can detect exactly when the disk turned and the direction of turning. And we can calculate speed and we can do all sorts of things. OK. That program will work really well if you compare it doesn't do anything else. Just sitting there waiting for you to turn the disk. In reality, your computer going to do a whole bunch of things. So when they want to do other things, you turn the disk and it's not going to record it. To be able to record all the changes, you need something called interrupt. OK. Here's the second script. It sounds really fancy, but it's really easy to understand. Interrupt means stop whatever you're doing and do the thing I'm telling you to do right now. OK. So first thing we need is something called volatile. That means this number always changes. And we're going to use this variable to store the number marks, the disk turned. After that it's easy, PnA is 2, PnB is 3. And here we define an interrupt function to tell the computer what to do when there's interrupt. So we read from PnA to a variable IA, we read into the B into IB. If the two numbers equal, so there's one direction. If it's not equal, there's another direction. Every time it gets interrupted, the number marks increase by one. The setup is the same serial connection, PnA and PnB are input. And here's the function, attach interrupt. That tells the computer, I need an interrupt. And the zero is the poor zero. The zero always connects to PnN2 on the Arduino board, Uno. And if it's like a Mac or something, you have to check which zero goes to. So always suppose zero there. And physically it will connect, always connect to your interrupt Pn into number two. The SR is the SR function we define. If we define the function as an other name, that's Kevin, then here 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, and get me interrupt. You can also use the word rising or falling. And however, that only change the direction, like for example, it's rising, it's from zero to one. So in this case, we know it can change both directions. So we use the word change. In the loop, you can compare to last loop. In the loop, just one thing, just print the number marks, that is the whole point. In the loop, you do whatever you want. And just whenever the PnA changes the voltage, the system immediately jump into the interrupt routine, and do the interrupt, and it doesn't come back to you. So you'll never miss a moment that you turn the disk. Here is what it looks like. So you have to turn it really, really fast, between the printing and the number already added. Okay, we did this part. Now we're going to through in the motor. You cannot drive the motor directly from the Uno board. So Uno has the power source, but the reverse current from the motor going to fly this board in a second. So we're going to use a little controller chip. And what this chip does is it takes the little current from the Uno board and controls the really, really big current on the motor. I'm going to use this L293D, as example, I can send that 10 of them, it's like $8. If you do a lot of 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. So those ones are connected. So you put one in the ground, they are all grounded. And one below it, the red line is the power. So you put the power source there, anybody need the power, you can just attach to that power and you're going to get that power. For example, I need a 9 volt here. I can just connect a line from the 9 volt to the 9 volt power. All those columns, the four of those, they're connected vertically. So they're all connected. So if I want to connect anyone to this lag, I can connect any of those three slots left. And here is how this chip works. The chip has a dent here. Underneath the dent is number one, and then number two, number three. Those two lags should go to the ground, or you can connect to a really large heat sink. So it's not really used, it's used as ground. Number one is enable. Basically you plug in your computer, then you can use your computer to give a value. For example, 255 is the highest speed, and you get zero, then the motor can shut off. Basically I think there's a speed control. And then those two lags that I can connect with the blue line, they are for directions. So you put one of them in high, one of them in low, it goes to one direction. If you go vice versa, if you put another one high, this one low, it goes to a different direction. I just control the direction of the motor turning. So those three lines, the enable line, and those two lines control the speed and control the direction. The furthers to the right is the one, the right line, which you control, or not control, on which you connect to the high power. You connect to 9 volts or 12 volts power. So we only got two lags not connected. Those two lags go to the DC motor. It really doesn't matter which direction it goes, because you can control the direction. You can see we only used half the chip, because actually this chip is designed for two motors. You can control another motor on the other side. For this speed control, you need to just use one with squeaky symbol next to the number. Then two wires to give directions, then ground, then put those two blue ones to the motor. The last one is for the power to the motor. Your Arduino package comes with a power adapter. This is really cool. We can connect to a 9 volt battery, and you can fit it right on the breadboard. Then it powers on both sides of the breadboard. And also you can control the voltage of each one. So for example, I want this to be a 3 volt. You just move the pin to the 3, connect the two pins on the 3-volt side. This time, I'm going to use the 5 volt. It's a little bit underpowered for this motor, but I also have. Okay, let's start. Pretty easy. Number of marks. This time, I also calculated the number of turns, because there are too many marks, and there will be overflowing. The motor enable is at pin 11. Enable is when you control speed. And then the two other like I control the direction at 10 and 9. Then from the optical pin A is on the 2, and the pin B at the 3. Remember the interrupts for the Uno board, you have to put on the 2. And here's the interrupt function. I read from optical pin A and optical pin B, if they equal, it's one direction. If they're not equal, it's another direction. And if a number of marks is more than 100, that means one full rotation, I just set to zero. And then I increase the number of turns by one. And the setup is just a boilerplate. This pin is input, that pin is output. Attach the interrupt and give me a serial communication. And motor A and motor B are going to control the direction, so 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. And here I define a variable ii, which I use the ii to control the speed. So I assign an ii to the maximum, then one is larger than 125. Why 125 is because I found this motor stalls if it's lower than 125. But maybe I should give a higher voltage, so it won't stall, but you can try it with a larger voltage. So it's going to start with high speed, then get slower and slower, every time by step of 10. And I output the ii value, which is the arbitrary number you give to the enable wire. Remember the speed that we use here, adjust that the arbitrary number is not actually turns. It adjusts arbitrary number from one to 225, we'll give to the motor, give to the controller. But the number turns is the one that you actually 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 motor, step motor have more wires and more complicated. And the advantage of step motor is it can precisely control the angle it turns. So it's used for the 3D printing, however it's more bulky and it's more expensive. So here I define an x to control the marks. So basically I turn the motor off by giving that wire, but it enable wire at zero voltage. And then I say, if you can call one mark stop, then the next time you'll record two marks you stop, then three marks you stop. So that's going to control precisely how much the motor can turn. Let's say it works, pretty cool stuff. If you like this kind of video, please click like, otherwise if I say a thousand people watched it, but there's only like 10, 20 likes, I may just stop making those videos. Instead I'll make a video for me eating a pie or do a TikTok dance. I hope you like this video, the emphasis on like. And please visit us at www.bchtechnologies.com or locally, Greensboro, North Carolina. Cheers!