My Name is Ahmad Shamshiri and I am presenting this tutorial from Canada.
Hi, welcome to Robojax. in this video I'm going to introduce the
PCA9685 servo controller by NXP Semiconductor. This is a 16
channel 12 bit module that can control or produce pulse width modulation PWM
that can be used for servo and also it can be used for Bank of LEDs to dim it
so each of these can be controlled individually meaning that you can set
disc separately a different angle or so you can see the other one
or you can set multiple or all of them to do the same thing or different thing
all the codes, the library the manual and all the information that you need to run
a successful projects for this video is available at the description or you can
directly go to robojax.com/learn/arduino to get all
the codes also I'm looking forward to your suggestions and comments. If you
have a robot something like this for example it has six arm and each or needs
two servo, you can have up to 12 use here so this servo controller can do that
for you. Let me explain the board. the board is 25 millimeter by 60
millimeter this is a size it has four screw holes so you can screw it on some
place secure. And it has 16 channels 16 channels meaning you can connect up up
to 16 servo channels so here are the wires and it starts from 0 and also it
has been labeled here 0 to 15 servo always have 3 cables here the lower one
usually with a darker or black this will be the GND then the positive is
always at the middle and then a signal is always a light color the reason they
put the positive or the middle so even if you make mistake to put it in reverse
a positive is always at the middle so it will be safe
not to damage the server and here's fortunately the cable that I have is
matching the color here so that's brown red and yellow and you insert the servo
like that and then you can connect next , next and all the servers. Then we have
here we have these pins we have here VCC this goes to 5 volts you can get it
from Arduino and this is a ground (GND) so these two or five volts you can connect
it directly to or do we know this v+ here at this port this is connected to
the main power so all these servos cannot be powered
with Arduino so they have been independently connected. This is the
ground and this is the positive then we have three more wires here. The DE
you don't need it here for this application we have SDA and then we have
SCL is clock SDA is data you just need to connect these two this will
be connected to pen A4 and this will be connected to pen A5 . SDA will be
connected to pen A4 and SCL clock will be connected to pin A5 here to the
analog input of the Arduino boards.
You must make sure that you provide 5 volts
with higher current for this because first of all if you try to power it from
Arduino you might damage your Arduino board because arduino and does not supply
enough current for all of this. If you're using just one of them maybe that would
work but if you are using multiple of this you have to supply separate 5 volts
high current (like 1A 5V) for the for the board you can connect 4 boards like this and each
will have 16 so 4 x 16 you will get 64 servers can be controlled. So the only
difference would be you have to connect another header here and then you will
connect it to the next one so the same thing you can connect the cable select
SDA to the next one so the data will go and then if you have one extra
just solder this part the next boat will be activated. If you have the third board
so then we can go with binary here 0 1 and 1 0. This is 1 and all of these
are 0 so that would would be board 1. And when if you have board 2 so this is 0 this
will be connected if you have 3, this and this will be connected (soldered) for so for
the 4th you will have only connected this one and it will work. But here I'm
showing you here now the wiring practically so SDA is here the green
one which is connected to point A4 as you can see and then SCL the clock
which is blue I've connected it to A5 and then the VCC is connected to 5 volts and
then ground (GND) is connected to the ground and then here I've connected this wire
with these crocodile clip which I supply separate 5 volts from my power supply so
The link for the Adafruit PWM servo library will be provided you
once you come to this page just click on a clone and click on the download and
save it wherever you are saving your file. I'm just saving it here, after
that just go to folders wherever you have saved it and then right click on
the file using 7-zip if you don't have 7-zip go get it from www.7zip.org
it's free just select extract here. Once that is completed it will create a
folder with the same name called Adafruit PWM servo driver library
here just right-click and select COPY and then go to
wherever you have your Arduino folder, mine is here Arduino->Library and just
somewhere in the blank area right-click and and paste it. Once you paste it here
I have the Adafruit library and close this. To open the file just go click
on File->Examples and then scroll until you see
Adafruit library so here is the servo and then that's PWM test
click on servo and this will be opened. We will go through this but you don't need
it this is for testing and other stuff and I will give you just simple example
so you can run for one servo and then replicate the same thing for multiple
service these two (lines) are required the "Wire" and
then the Adafruit_PWMServoDriver these are the header file that is needed
once you install the library this will be included so you don't have to worry
so these are all comments. Thi is a counter variables of 8-bit but don't worry do
not touch it it's just used internally the setup from here to here, sets
up the Serial Monitor to display the code for you and this is also another
text, this will print that and then this is the main one which says
pwm.begin() this well initializes the library to
prepare communicating with the board and then set pulse width modulation 60 so
that says that as 60Hz update this field is used here when all the
initialization happens we use yield to making sure that the other process that
takes longer will be done and after that it will proceed so this yield waits for
everything else to be completed and then proceed. For now for the system to work
these are the servos pulse which you do not use here the Servo Pulse is a
method (or function) that starts from here to here.
This is just for testing different pulse width.
Here is the main loop that runs the codes for you. The main loop starts
from here to here and it prints the servo number on the screen and then it
goes with a Pulse Width that we set and then the servo minimum and maximum but
this is a value for the servo and it continuously goes and sets for the servo
number this is the value that sets each server so this "servonum" is a value that
goes zero one two three whatever up to fifteen if you have fifteen of those
it means if you don't control one servo just put the server number here so and
this is minimum if it rotates the servo in one direction this will rotate it
into the other direction and wait half a second and then just increment so this
increment means here it goes up to nine so this is the main factor determining
how many if you have sixteen just put fifteen so remember that it starts from
zero to fifteen that's sixteen so if you have 4 servo just set this to 3
The code that I have updated for simplification i've removed the other method the set up is
still stays the same and then the loop. Inside the loop i just still stays the same
number of the servo so if you have connected it to the zero so you put that
as zero and if you are putting at it any of these other number just change it
accordingly. the value the pulse that you're running it can go according to
the documentation of servo it can go from zero to 4096 but the pulse that you
are sending the length that you're sending is very important to see at what
length this can stop or start so now to make it observable I just put a piece of
tape there so now you'll see which side is that one of the sides we will use it
as a reference so that is zero and that is
and that is 180 degrees for the servo. So considering
that we have to set the minimum and maximum of this servo and we have to see
at what pulse length it can go to the minimum and to maximum. Start with
initial set the minimum is 150 but let's see at 150 where it goes
if I upload the code that is a hundred and fifty and it saves here now let me
reduce it and see what happens let me make it 140 let's see if
it goes more to that direction. ... a little more. You saw that it went more so we
have to see if it can go more. 30 so that is setting the minimum we have
to find for each servo that you're using we have to find the minimum. It went more
I'm going 120, it went more. That's very important if I make it a 110
now it makes noise I don't know if you can hear that let me
get it closer to the microphone so this is the noise and that means this servo
can get damaged if I continue using this value so let me go for 120 even 120 is
at the edge so which means I have to go a little higher let me go 125. 125 is the
lowest value that we have. So for the servo you have to find the value for the
upper value here they suggest 600 I was reading (in) this header file that they
included with the CPP and this header file they have a limitation so if you go
higher this will not allow it to go for so that Pulse Width the maximum will
move it this way. (let's ) make this 600 and let's see which way
because now I have a black tape there
so I commented that's the previous one so it it comes up to here. Let me
increase it and see if it can go further because we know the lower value will
move it this way so let's see if it can go the other way
the code is uploaded we didn't see any difference. So we have to go backwards
and tell let's see what value is affecting it and at what value it stops
affecting so 650 didn't do anything so we know 600 is doing something but
how about if we make 550. Because We don't know at what value it's stop 550
moves it back so how about 570 a little so 570 let's make it 580 if it moves we
have to pay attention and listen no difference 570 maybe 575 so 575 brought
that a little back so 575 is the value for this server. Now I found these values
now we can use mapping to say that okay
125 is zero degrees and 575 is 180 degrees. And based on that we
can control this at the exact angle that we want. Okay
I've changed the values here for servo min and for servo max the value that I
found for the servo minimum I put it here at 125 and 575 for the max
and then here. Here is how we do the mapping so let's say you want to move
the servo to 280 degrees integer angle is equal 180 that's my angle stored in
this variable and the map which is a method (or function) in Arduino it gets some value
here for example angle 180 and we say the value of 0 to 180 map it to
125 to 525 so this is a constant variable that I have here so I can put
also a number of map functions or methods is just mapping minimum and
maximum to another minimum and maximum because we want 0 to 180 degrees should
be translated to between 125 - 575 so this will do it and the result will
be stored in the pulse and then we use the pulse here. Now if I make this 90 it
will also print here on the screen so we say don't care. Now it's 90 degrees
so 90 is equal to 350 s you don't have to calculate the pulse because this will
do it for you 20 so 120 is equal to 425 I've created this method I am calling it
angle to pulse so it receives an integer same thing that I've described and at
the end it just says return the type is integer so this will return the pulse
which is integer so it goes back. Now to use it we just use angle to pulse and
then put the value so this was a value that you were putting so now I'm putting
it so inverse 65 and also transits on the screen which I don't know what value
is this so now let's see it it moved to 65 and 65 is equal to 278 so now I have
written this for loop or integer angle starts from 0 initializes and then angle
is smaller than until angle is smaller than weight 181 then angle being
incremented 20 degrees so this "for loop" printing and passing the value of the
angle which is first this will be 20 and then 60 80 and so forth and till 180 it
goes and comes back and star. Here we wait for one second so let me upload the
code
so you can see the value of here and then wait and comes back so that way you
can send any angle that you wish. Code will be provided (below this video in the description). The original code the
main function that I initially created and the changes that I made will be
available at by clicking at the description below the first introduction
to this PCA9685 module thank you for watching please thumb up
the video and also please share it and subscribe I appreciated