1 00:00:00,270 --> 00:00:07,290 Hello. In this video tutorial I 2 00:00:07,290 --> 00:00:08,520 am going to show you how to 3 00:00:08,520 --> 00:00:11,700 use a servo motor this type of 4 00:00:11,700 --> 00:00:14,520 motor that you can see here it's 5 00:00:14,520 --> 00:00:16,440 not the usual type of motor that 6 00:00:16,440 --> 00:00:19,500 keeps turning and turning it works in 7 00:00:19,500 --> 00:00:20,160 a slightly different 8 00:00:20,160 --> 00:00:20,760 way 9 00:00:21,870 --> 00:00:24,900 so using one command block like this 10 00:00:24,900 --> 00:00:25,260 one 11 00:00:26,430 --> 00:00:27,960 to turn the servo by a certain 12 00:00:27,960 --> 00:00:30,330 amount of degrees we can send a 13 00:00:30,330 --> 00:00:33,210 command to the motor this one that 14 00:00:33,210 --> 00:00:34,710 is connected to pin ten 15 00:00:35,940 --> 00:00:38,760 and the command instead of continuously turning 16 00:00:39,180 --> 00:00:40,860 is saying here that the servo has 17 00:00:40,860 --> 00:00:43,170 to place itself in position at zero 18 00:00:43,170 --> 00:00:43,770 degrees 19 00:00:46,050 --> 00:00:48,660 so for instance here the motor is already 20 00:00:48,660 --> 00:00:50,610 in zero degrees it starts there on 21 00:00:50,610 --> 00:00:53,280 zero so the servo is connected with 22 00:00:53,310 --> 00:00:55,380 this green wire on the left side 23 00:00:55,950 --> 00:00:58,350 and it's in pin number ten but 24 00:00:58,350 --> 00:01:00,960 it could be eleven nine whichever you 25 00:01:00,960 --> 00:01:03,210 prefer and there are also two other 26 00:01:03,210 --> 00:01:05,040 connectors the red 27 00:01:05,069 --> 00:01:06,750 one the one in the middle is 28 00:01:06,750 --> 00:01:08,730 connected to the power source 29 00:01:10,140 --> 00:01:11,790 and the one on the right the 30 00:01:11,790 --> 00:01:14,250 black like always is connected to ground 31 00:01:15,870 --> 00:01:17,610 so here we have an extra component 32 00:01:17,610 --> 00:01:20,070 we haven't used before this is the 33 00:01:20,070 --> 00:01:23,790 power supply the servo motor you'll find 34 00:01:23,790 --> 00:01:26,010 here in the basic library but if 35 00:01:26,010 --> 00:01:27,540 you look for the power source you 36 00:01:27,540 --> 00:01:29,580 can't find it here so you will 37 00:01:29,580 --> 00:01:31,920 have to look in the standard library 38 00:01:31,920 --> 00:01:34,650 that contains all of the components listed 39 00:01:38,100 --> 00:01:40,320 and just keep scrolling down until you 40 00:01:40,320 --> 00:01:40,920 find it 41 00:01:48,480 --> 00:01:50,460 here yet here you have the power 42 00:01:50,460 --> 00:01:52,530 source and the reason that we need 43 00:01:52,530 --> 00:01:54,570 a power source is that the arduino 44 00:01:54,570 --> 00:01:57,240 board here can't provide enough energy to 45 00:01:57,240 --> 00:01:59,970 the servo motor and just as a 46 00:01:59,970 --> 00:02:03,120 reminder don't forget the arduino board also 47 00:02:03,120 --> 00:02:05,670 needs to have the ground connection the 48 00:02:05,670 --> 00:02:07,200 ground of the arduino 49 00:02:07,230 --> 00:02:09,150 is connected to the ground of the 50 00:02:09,150 --> 00:02:11,520 motor and the ground of the power 51 00:02:11,520 --> 00:02:13,710 source this way the three of them 52 00:02:13,710 --> 00:02:15,930 will have a common point of reference 53 00:02:16,380 --> 00:02:18,660 this is also their reference of voltage 54 00:02:19,080 --> 00:02:20,520 so yes it is the way that 55 00:02:20,520 --> 00:02:22,170 we close the circuit and that is 56 00:02:22,170 --> 00:02:24,510 important but it's also a common point 57 00:02:24,510 --> 00:02:26,040 of reference for voltage 58 00:02:29,400 --> 00:02:32,280 so now I have briefly explained why 59 00:02:32,280 --> 00:02:34,980 the red wire is connected here and 60 00:02:34,980 --> 00:02:36,870 why the black wire is connected here 61 00:02:38,010 --> 00:02:39,960 because the power to move the servo 62 00:02:39,960 --> 00:02:43,260 is going to be provided by the 63 00:02:43,260 --> 00:02:46,080 power source so now I will explain 64 00:02:46,140 --> 00:02:47,820 the program that you will have to 65 00:02:47,820 --> 00:02:50,340 create but I'm not going to show 66 00:02:50,370 --> 00:02:51,900 you the code I'm just going to 67 00:02:51,900 --> 00:02:54,390 show you how the program should behave 68 00:02:54,690 --> 00:02:55,980 and you will code it 69 00:02:58,920 --> 00:03:00,900 so let's start the simulations you can 70 00:03:00,900 --> 00:03:03,330 see so the program starts here at 71 00:03:03,330 --> 00:03:06,570 zero degrees and it will keep turning 72 00:03:06,570 --> 00:03:08,940 and turning because our code will send 73 00:03:08,940 --> 00:03:11,220 commands to make the motor rotate to 74 00:03:11,220 --> 00:03:12,330 different positions 75 00:03:13,470 --> 00:03:15,210 so we start in zero degrees and 76 00:03:15,210 --> 00:03:17,040 we move up or it looks like 77 00:03:17,040 --> 00:03:17,730 to the left 78 00:03:19,830 --> 00:03:22,080 and then once once we reach ninety 79 00:03:22,080 --> 00:03:25,980 degrees we start moving down or back 80 00:03:25,980 --> 00:03:26,880 to the right 81 00:03:28,110 --> 00:03:31,080 but down in degrees ninety eighty five 82 00:03:31,110 --> 00:03:34,320 eighty and so on until we finish 83 00:03:34,331 --> 00:03:35,970 back in zero degrees again 84 00:03:39,030 --> 00:03:40,950 and this over here is the intensity 85 00:03:40,950 --> 00:03:43,830 that is being read and the intensity 86 00:03:43,830 --> 00:03:46,410 provided by the power source and then 87 00:03:46,410 --> 00:03:48,000 the moment we arrive back to zero 88 00:03:48,000 --> 00:03:50,040 degrees it doesn't move anymore and the 89 00:03:50,040 --> 00:03:51,150 program stops 90 00:03:54,060 --> 00:03:55,200 so the first thing you need to 91 00:03:55,200 --> 00:03:57,360 do is connect the circuit this way 92 00:03:57,360 --> 00:03:59,820 with the arduino board the servo and 93 00:03:59,820 --> 00:04:00,780 the power source 94 00:04:02,190 --> 00:04:03,510 I hope you understand now why the 95 00:04:03,510 --> 00:04:06,120 power source is needed and of course 96 00:04:06,120 --> 00:04:07,620 you have the blocks for the code 97 00:04:08,670 --> 00:04:09,480 and I'm just going to show you 98 00:04:09,480 --> 00:04:10,860 the main idea instead of the whole 99 00:04:10,860 --> 00:04:12,780 thing so the first thing we need 100 00:04:12,780 --> 00:04:14,760 is a block like this one with 101 00:04:14,760 --> 00:04:16,649 this block you're saying that the servo 102 00:04:16,649 --> 00:04:18,959 is connected to number ten and it's 103 00:04:18,959 --> 00:04:21,660 going to start at zero degrees we 104 00:04:21,660 --> 00:04:23,550 always need to start somewhere so we 105 00:04:23,550 --> 00:04:24,900 need to be sure that our servo 106 00:04:24,900 --> 00:04:27,690 will start at zero otherwise we don't 107 00:04:27,690 --> 00:04:28,800 know where it will be 108 00:04:28,920 --> 00:04:30,450 it could be in forty five or 109 00:04:30,450 --> 00:04:31,920 fifty or thirty degrees 110 00:04:33,330 --> 00:04:35,430 so the first command is always starting 111 00:04:35,430 --> 00:04:36,840 at zero degrees 112 00:04:38,100 --> 00:04:40,320 and then we will have to wait 113 00:04:40,440 --> 00:04:43,410 for instance one second every time we 114 00:04:43,410 --> 00:04:46,320 make the servo turn we always need 115 00:04:46,320 --> 00:04:48,270 to wait to give the servo time 116 00:04:48,270 --> 00:04:49,110 to move 117 00:04:50,340 --> 00:04:51,450 so this is the first part of 118 00:04:51,450 --> 00:04:52,980 the code and then there will be 119 00:04:52,980 --> 00:04:55,710 a second part that will look something 120 00:04:55,710 --> 00:04:56,640 like that 121 00:04:58,470 --> 00:05:00,450 now this part is taken from the 122 00:05:00,510 --> 00:05:01,920 previous exercise 123 00:05:04,500 --> 00:05:05,640 and this is not what we are 124 00:05:05,640 --> 00:05:07,620 going to use to move the servo 125 00:05:07,620 --> 00:05:09,390 but it's going to be really similar 126 00:05:09,810 --> 00:05:11,760 to the first time you move upwards 127 00:05:11,790 --> 00:05:14,220 you will for instance turn or jump 128 00:05:14,340 --> 00:05:16,770 every five degrees from zero to ninety 129 00:05:17,520 --> 00:05:19,170 remember this from the simulation that you've 130 00:05:19,170 --> 00:05:19,800 already seen 131 00:05:21,180 --> 00:05:22,710 and then you will send the order 132 00:05:22,710 --> 00:05:25,050 to move and again we will use 133 00:05:25,050 --> 00:05:26,700 a variable the same way we use 134 00:05:26,700 --> 00:05:28,800 the variable to send a frequency to 135 00:05:28,800 --> 00:05:31,530 the buzzer we will use a variable 136 00:05:31,530 --> 00:05:34,110 to send an angle with degrees to 137 00:05:34,110 --> 00:05:36,450 the servo and then again we'll have 138 00:05:36,450 --> 00:05:38,310 to wait for a certain amount of 139 00:05:38,310 --> 00:05:38,670 time 140 00:05:41,820 --> 00:05:43,590 so this one will be the second 141 00:05:43,590 --> 00:05:45,720 part of the program and there will 142 00:05:45,720 --> 00:05:47,670 be a third part really similar to 143 00:05:47,670 --> 00:05:49,710 the second one but instead of moving 144 00:05:49,710 --> 00:05:52,860 upwards with degrees we will move downwards 145 00:05:53,070 --> 00:05:55,590 again jumping every five degrees from ninety 146 00:05:55,590 --> 00:05:58,650 to zero and again we'll send commands 147 00:05:58,650 --> 00:06:00,000 to the server to this 148 00:06:00,000 --> 00:06:02,550 servo using a variable for the angle 149 00:06:03,540 --> 00:06:04,800 and then every time we make it 150 00:06:04,800 --> 00:06:06,720 move we will wait for one second 151 00:06:07,500 --> 00:06:09,030 so this is the program you will 152 00:06:09,030 --> 00:06:12,240 need the first part then something similar 153 00:06:12,240 --> 00:06:13,710 to this one for the second part 154 00:06:14,220 --> 00:06:15,810 and something similar to that for the 155 00:06:15,810 --> 00:06:18,420 third part and if you program it 156 00:06:18,420 --> 00:06:20,160 properly it will behave the same 157 00:06:20,160 --> 00:06:21,150 the way you have seen in the 158 00:06:21,150 --> 00:06:24,960 simulation okay so it shouldn't be too 159 00:06:24,960 --> 00:06:27,150 difficult but just make sure to follow 160 00:06:27,330 --> 00:06:31,710 these instructions with this structure okay good 161 00:06:31,710 --> 00:06:32,070 luck