WEBVTT

00:00.110 --> 00:04.640
And let's not write the Arduino code to control the servo motor.

00:04.820 --> 00:14.030
So first of all, I am going to, of course, add a define servo pin, which is number 12.

00:14.060 --> 00:19.700
Then to initialize the servo I will need to include the servo library.

00:19.700 --> 00:21.260
So servo dot h.

00:21.320 --> 00:21.460
Okay.

00:21.470 --> 00:26.600
So we need it to include a library for the LCD screen as well for the servo.

00:26.690 --> 00:30.380
And then we are going to create a servo here in the global scope.

00:30.380 --> 00:37.430
So Servo, we use the name of the library and then let's name it Servo with as lowercase.

00:37.460 --> 00:37.910
Okay.

00:38.740 --> 00:40.600
So we just created like this.

00:40.630 --> 00:44.320
No parentheses, nothing in the void setup.

00:44.830 --> 00:49.390
I'm going to do servo dot attach.

00:49.390 --> 00:54.190
So we're going to basically attach a pin to control the servo motor.

00:54.190 --> 00:56.280
And of course, that's the servo pin.

00:56.290 --> 00:58.680
And now the servo is initialized.

00:58.690 --> 01:03.640
So if I want to give a command to the servo, I can do servo dot, right?

01:03.640 --> 01:04.910
And what can I give?

01:04.930 --> 01:06.190
I can give a number.

01:06.190 --> 01:10.330
So an integer number between 0 and 180.

01:10.630 --> 01:11.560
So what is that?

01:11.560 --> 01:16.420
Basically, a servo motor is not unlimited in what it can do.

01:16.450 --> 01:19.690
It can go from 0 to 180 degrees.

01:19.690 --> 01:22.390
So basically it can make half a rotation.

01:22.390 --> 01:24.790
You can't do more than that with a servo motor.

01:24.820 --> 01:25.190
Okay.

01:25.270 --> 01:31.150
So for example, if you want to control a wheel with your Arduino, well, that's not the kind of motor

01:31.150 --> 01:31.690
you want.

01:31.690 --> 01:35.350
You want a motor that can make more than one turn.

01:35.530 --> 01:35.950
Okay.

01:35.950 --> 01:44.090
So here, basically zero is going to be one extreme position and 180 is going to be the other extreme

01:44.090 --> 01:44.690
position.

01:44.690 --> 01:46.460
Let's put 50.

01:46.580 --> 01:46.960
Okay.

01:46.970 --> 01:50.750
So we're going to put the servo motor at 50.

01:50.930 --> 01:52.280
Well, actually, let's go to the middle.

01:52.280 --> 01:53.480
Let's go to 90.

01:53.510 --> 01:57.890
So that's going to be that should be the middle position of the servo motor.

01:57.890 --> 02:01.250
And let's upload that code to the Arduino.

02:01.250 --> 02:06.590
So connected upload and let's see what the servo is doing.

02:08.250 --> 02:09.600
Compiling and uploading.

02:10.320 --> 02:10.770
Okay.

02:10.770 --> 02:12.780
And you can see here the servo has moved.

02:12.810 --> 02:14.850
This is the middle position.

02:14.850 --> 02:19.020
So maybe if I want the middle position here, I can change.

02:19.050 --> 02:22.680
Maybe that plastic part to be around.

02:23.340 --> 02:25.710
Yeah, something like around the middle position.

02:25.710 --> 02:26.250
Okay.

02:26.310 --> 02:33.270
So it's going to be like this on the extreme position and is going to be like that on the lowest position

02:33.540 --> 02:37.140
and maybe your servo can make a little bit of noise.

02:37.170 --> 02:45.840
It's just that well, here the plastic gear is not a great quality and the servo might force a bit,

02:45.840 --> 02:47.850
but that's not really a problem here.

02:47.850 --> 02:51.390
Again, if you want to have a better quality, you can buy another servo.

02:51.420 --> 02:54.270
But for now, that's all we need.

02:54.270 --> 02:58.830
If you have that from your starter kit and then, well, I want to show you something more.

02:58.830 --> 03:04.770
Just because we're going to use that in the following is how to make the servo sweep.

03:04.770 --> 03:06.420
So basically the sweep example.

03:06.420 --> 03:12.880
So we're going to go from 0 to 180 and from 180 to 0 and that indefinitely.

03:12.880 --> 03:16.130
So you can see the servo move from one side to the other one.

03:16.150 --> 03:17.680
So I'm going to remove that.

03:17.680 --> 03:20.620
And in the void loop, I'm going to add a for loop.

03:20.620 --> 03:29.770
So for int, I is equal to zero, I lower than 180 and I plus plus.

03:29.770 --> 03:40.270
So basically we go from 0 to 180 and we do several dot write I and we add a delay of let's say ten milliseconds.

03:40.450 --> 03:45.940
So every ten milliseconds we increment the position from 0 to 180.

03:45.940 --> 03:50.020
And once we reach 180 degrees, we do for.

03:50.990 --> 03:54.230
INT i is equal to 180.

03:55.040 --> 04:00.560
I is greater than let's greater or equal than zero.

04:00.980 --> 04:03.400
I minus minus.

04:03.410 --> 04:07.700
So with this for loop we go from 180 to 0.

04:07.730 --> 04:08.000
Okay.

04:08.000 --> 04:14.430
Because we decrease, we decrement the counter every time servo here.

04:14.450 --> 04:22.160
It's better if it's correctly spelled right I and delay ten.

04:22.160 --> 04:28.550
So this is going to make the servo go from 0 to 180 degrees every ten milliseconds.

04:28.550 --> 04:32.840
This is going to make the servo go from 180 to 0.

04:32.840 --> 04:38.690
And then we come back here and we start at zero again and let's see what it does.

04:38.690 --> 04:43.910
So let's upload and let's check the servo motor, okay?

04:43.910 --> 04:49.820
And you can see the servo is going from the minimum to the maximum position and coming back.

04:49.820 --> 04:55.920
So now, well, if you want to stop this, what you can do is you can upload another program that doesn't

04:55.920 --> 05:00.750
use the servo or what I usually do is quite safe to do.

05:00.750 --> 05:05.040
I just disconnect the wire on the PIN number 12 here.

05:05.040 --> 05:07.020
You can see the command wire.

05:07.050 --> 05:08.130
Okay, that's it.

05:08.160 --> 05:09.570
I don't disconnect the ground.

05:09.570 --> 05:10.730
I don't disconnect the power supply.

05:10.730 --> 05:16.080
I just disconnect the command and I make sure that this doesn't touch anything else.

05:16.080 --> 05:16.620
Okay.

05:16.860 --> 05:22.050
So I can continue with my experiment, and I don't need to upload an empty code.

05:22.530 --> 05:23.190
All right?

05:23.190 --> 05:29.340
And, well, that's the end of this section where we built the Arduino circuit for the following of

05:29.340 --> 05:29.940
the course.

05:29.940 --> 05:36.330
Let's now get some practice with those components which we are going to control from the Raspberry Pi.
