 This right here is a servo motor and so is this and so is this instead of a dumb DC motor like this one that just spins Back and forth endlessly the servo motor has controlled positioning and a feedback system to let you know What's happening behind the scenes with your servo in this video? We're gonna go over how these servo motors work how to wire them up how to write the code to control them So you can add them in your own projects servo motors like these are designed for precise control of positioning They're often used in applications where you need controlled movements and you need them to be super accurate Like in robotics a lot of remote control stuff servo is huge and in industrial automation Especially factory work inside the servo motor You'll typically find one of those DC motors that we showed a couple seconds ago connected to a Potentiometer to some control circuitry and a feedback loop so pretty much We start with one of these DC motors and adding a couple of electronics We turn it into this to control a servo motor You need to set it a specific signal and that input signal tells it where to go the position that you require Where you want it to go to you can also tell that there's different sized ones like when we go to the super Small one I could almost break it by just moving it too hard Then we have this medium-sized one where you can hear I'm moving the motor around and then you got this one Which is rated to up to 60 kilograms and I can barely move it with my hands So when we send that signal to the servo motor to tell it where to go it keeps moving until it gets to that desired position So it's kind of doing like a check where it moves and my there yet No keep moving and my there yet No, and it just keeps going until it hits the position that you want it to and then it shuts off and stops The reason we can read that position is because we're using a potentiometer Which I don't know if we've done videos on before but all it does is give you the position of something that's being rotated So we have a motor that's spinning something that's giving us position and when you put them together We can have a motor that spins until it goes into a position that we want it to go and then we can shut it off So if I want to move it whatever amount of degrees left It'll stop when it gets to that point also one nice thing about these servos is that they're pretty good at holding a load So if you put it in a position it has a certain amount of strength it can hold I think that's what this 60 kilogram reading is here then you have this one right here, which has nine grams on it And this one right here doesn't seem to have a rating. Maybe it's on this side. No Nothing on this one. The reason I prefer servo motors whenever I can is because of the precision that they give you versus one of these DC motors It's also that they're accurate and that you can repeat the process over and over and over So you can constantly get back feedback from this thing So sometimes people will actually do projects where the servo is the input to their project Whereas you're moving this you can read that signal and then pump it into something else Like let's say I wanted to make my little nine gram movements move this big servo I could take the position from servo one and pump it into servo two And I can actually be the one that's inputting on this servo the position So I can use it like a potentiometer, but I could still move itself DC motors are usually preferred for their simplicity and continuous rotation And the fact that they're a lot cheaper than these big things from what I remember these 60 kilogram ones are about 25 dollars each And a pack of 20 of these nine grams went for about 40 dollars through aliexpress So they're cheap, but not as cheap as the dc motor So I guess it depends on the kind of project you're doing but if I have the option I'll always take the servo over the dc motor So we're going to do a little demo where we connect the servo up to a microcontroller And then we control the servo and read the information coming from it In my case I'm going to use the small nine gram one because I don't need a power supply for it So I can run it straight off my board But if you do have a bigger one like this you might need a power supply off on the side All we need for the demo is our servo a microcontroller and then some cables to connect it up These nine gram servos usually have the same wiring They have an orange or yellow cable a red cable and then a black or brown cable The orange cable or the yellow cable is going to be the signal The red is going to be the power and the brown or black is going to be the ground For the signal we're going to be plugging that into number nine on the microcontroller We're going to be using an arduino uno then for the power We're just going to be going into five volts on the board and for ground We're going to be going into ground that's all you need to move this nine gram servo pin number nine five volt ground Also one thing I forgot to mention not all servos have the same amount of degrees of rotation that they can do This one is limited to 180 so you could see there All the way to there and then it locks. That's it. You can break it if you push it through Whereas I have ones like this that are 360 Can go all the way around some of them can go endlessly like this one Those tend to cost a lot more though So we're going to have to consider in our code when we're writing for this that it's a 180 degrees on each side The code for this is pretty simple because we get to use a library like in other videos The library is called servo So we're just going to go into our library manager and we're going to go download servo We're going to go right here and download the adafruit pwm Servo driver library and then install everything that needs to be installed once you do that Just try and upload the code to your board and see if it throws an error to make sure you install the library We can see here that everything is installed at the board. We're not getting any errors So we know that this works then what we're going to do is create a servo object to control the servo motor So we now have an object called my servo and that's going to be our servo variable Then we're going to make a little holder variable that is going to hold the value of the position of the servo So ideally this would go between zero and 180 because like I said earlier, this is a 180 degree maximum servo Then in the setup we need to declare our little servo there We're going to attach it on pin number nine, which is the one that we used earlier You can also put this as your own variable outside, but I didn't think it was worth it because it's just a little short piece of code Then inside your loop, we're going to make two different loops. The first one is going to be starting at position zero We're going to increment until we get to 180 and then after that we're going to do the same thing But the other way around so now we're going to go from 180 to zero So the idea is that we go to full tilt and then back to zero and we just keep repeating that over and over and over You can see here We just go servo dot right and then position and that's how we write the position And we put a little delay here to take 15 milliseconds between each Increment, so let's actually upload that to the board I'm just going to turn this to full tilt and I don't know if you can see that on the camera Let me have to get closer But now our servo is rotating 180 degrees and then going back 180 degrees It's as simple as that in our case. We're using a loop, but you can always just comment this out Let's upload that to the board So the servo stops spinning and we can just go and grab this line right here and put it Let's say in our setup and we can say I want you to go to position 30 Or 40 You know upload that and you can see in a second here That servo just moved super quick to that position Now let's say I want to go from 40 to 180 I'm going to bring this close to the camera so you can see how fast this thing moves without that 15 millisecond delay All right, so we'll upload the code and give it about a second or two and Boom, there it goes. So let's say we took those two We went 180 to 0 to 50 75 And then let's say 175 One thing that's cool is not that the servo is powered if I try to move it with my fingers It's fighting that one thing you should definitely try but be careful is if your servo is powered and everything is powered up Try and push your servo. You're gonna feel that it's fighting back It's because you're moving the servo off the position It's supposed to be and then the motor is fighting to move it back to that position So let's say I'm at position zero and I'm trying to move it to one If I get it to one the motor is instantly going to take it back to zero because it's constantly trying to stay at zero So you can feel that motor is constantly fighting your feedback. I don't know why that code didn't work I'm going to try to get in here seems to panic. I think it's because everything is happening too quick So maybe if we do A longer delay, let's see if that works There we go So I guess you just need a longer delay between each movement to make sure it matches up So that's how you tell the servo to go to a specific position But what if we want to read the position from the servos potentiometer? Okay, so to read information off of your servo You're going to need to use the my servo dot read method What we did was add serial dot begin at the top here to activate the console Then down here we added serial dot print line And then my servo dot read and left that empty so that we could read off of that and print it in the console And then down here in my loop I do it again, but I put a one second delay to see if anything changes if we leave it running for a bit So I'm going to upload that to my board I'm going to go my serial monitor and I'm going to clear it out and let's see what happens We can see every second. It's reading 180 now. I want to try something if I move it slightly That's not letting me move. It's just locking me. Okay. Now. What if we go up here? Grab this and move it to 140 let's say We should upload the code It should stay at 180 for a little bit And then within a second or two, there we go. It should go to 140 and now if we go down here We could see it's reading 140 So if you want to get the position of the servo you need to run my servo or whatever you call it Dot read and if you want to tell it where to go you got to use dot write And that's it for today's servo video It shows you how servos work how to use them with some basic code And now you can start using them in your own projects We're definitely going to be doing more servo related projects in the future Where we're going to be using this big boy right here to make a robotic arm that can pick up things move them around and just Have complete accuracy with up to 60 kilograms of holding power pressure Whatever you want to call it Like I said earlier, if you enjoyed these videos and they help you out All you got to do is give the video a like and subscribe to the channel So you get notified when we make new projects like these I guess that's it for this one. See you in the next one