And let's not write the Arduino code to control the servo motor.
So first of all, I am going to, of course, add a define servo pin, which is number 12.
Then to initialize the servo I will need to include the servo library.
So servo dot h.
Okay.
So we need it to include a library for the LCD screen as well for the servo.
And then we are going to create a servo here in the global scope.
So Servo, we use the name of the library and then let's name it Servo with as lowercase.
Okay.
So we just created like this.
No parentheses, nothing in the void setup.
I'm going to do servo dot attach.
So we're going to basically attach a pin to control the servo motor.
And of course, that's the servo pin.
And now the servo is initialized.
So if I want to give a command to the servo, I can do servo dot, right?
And what can I give?
I can give a number.
So an integer number between 0 and 180.
So what is that?
Basically, a servo motor is not unlimited in what it can do.
It can go from 0 to 180 degrees.
So basically it can make half a rotation.
You can't do more than that with a servo motor.
Okay.
So for example, if you want to control a wheel with your Arduino, well, that's not the kind of motor
you want.
You want a motor that can make more than one turn.
Okay.
So here, basically zero is going to be one extreme position and 180 is going to be the other extreme
position.
Let's put 50.
Okay.
So we're going to put the servo motor at 50.
Well, actually, let's go to the middle.
Let's go to 90.
So that's going to be that should be the middle position of the servo motor.
And let's upload that code to the Arduino.
So connected upload and let's see what the servo is doing.
Compiling and uploading.
Okay.
And you can see here the servo has moved.
This is the middle position.
So maybe if I want the middle position here, I can change.
Maybe that plastic part to be around.
Yeah, something like around the middle position.
Okay.
So it's going to be like this on the extreme position and is going to be like that on the lowest position
and maybe your servo can make a little bit of noise.
It's just that well, here the plastic gear is not a great quality and the servo might force a bit,
but that's not really a problem here.
Again, if you want to have a better quality, you can buy another servo.
But for now, that's all we need.
If you have that from your starter kit and then, well, I want to show you something more.
Just because we're going to use that in the following is how to make the servo sweep.
So basically the sweep example.
So we're going to go from 0 to 180 and from 180 to 0 and that indefinitely.
So you can see the servo move from one side to the other one.
So I'm going to remove that.
And in the void loop, I'm going to add a for loop.
So for int, I is equal to zero, I lower than 180 and I plus plus.
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.
So every ten milliseconds we increment the position from 0 to 180.
And once we reach 180 degrees, we do for.
INT i is equal to 180.
I is greater than let's greater or equal than zero.
I minus minus.
So with this for loop we go from 180 to 0.
Okay.
Because we decrease, we decrement the counter every time servo here.
It's better if it's correctly spelled right I and delay ten.
So this is going to make the servo go from 0 to 180 degrees every ten milliseconds.
This is going to make the servo go from 180 to 0.
And then we come back here and we start at zero again and let's see what it does.
So let's upload and let's check the servo motor, okay?
And you can see the servo is going from the minimum to the maximum position and coming back.
So now, well, if you want to stop this, what you can do is you can upload another program that doesn't
use the servo or what I usually do is quite safe to do.
I just disconnect the wire on the PIN number 12 here.
You can see the command wire.
Okay, that's it.
I don't disconnect the ground.
I don't disconnect the power supply.
I just disconnect the command and I make sure that this doesn't touch anything else.
Okay.
So I can continue with my experiment, and I don't need to upload an empty code.
All right?
And, well, that's the end of this section where we built the Arduino circuit for the following of
the course.
Let's now get some practice with those components which we are going to control from the Raspberry Pi.