ARDUINO CODE FOR MG996R SERVO MOTOR 360 DEGREES ROTATION TYPE

MG996R servo motor in my 3DOF robot

MG996R servo motor changes the concept of servomotors that I had in my mind. Here you cannot put the angles where you want to rotate the motor (as we do in Tower Pro SG90 servomotor). Here the concept is different.

First of all, we need to select the numbers (explained later) which gives an angular speed now corresponding to this angular speed of the motor we'll find corresponding time within which the motor will go to desired angles. I have written the steps to control MG966R servo motor in this article.

MG996R Specifications: 

This is the motor that I am talking about: MG996R TowerPro







Coding the MG996R (Arduino code and C++ code): 


Concept: Note that 0 means 'on' the motor clockwise (CW), 90 means stop it and 180 means move it CCW. A data near to 0(data>0) give high CW motion and near to 90 (0-90, like 75, 85 etc) will give slow motion (CW). In the same way a data near to 90(data>90) give slow motion in CCW and data near to 180(data<180) given high CCW motion. So, remember this range of operation (0-90-180).

Now let's take an example to understand the Arduino code. Assume I have attached servo 3 to robotic arm 2. I want to do a rotation of 90 degrees (ccw) and then 90 degrees to 0 degree (cw). By observing I found that at data=80, it takes 748 milliseconds to do 90 degrees turn.

Speed can be increased or decreased depending on the data value as I have explained earlier (keep in mind that time (748 milliseconds) will change if you change the data value). I have used two while loops and designed the code such a way that the arm comes to its initial position i.e., the motion will be like 0 degrees to 90 degrees then 90 degrees to 0 degrees.
(Note: In Arduino IDE the time is in milliseconds)

Arduino code for MG996R Servo motor

Arduino code for MG996R servo motor


C++ code to convert angles to milliseconds which is the input to Arduino IDE: 

Here I was using three servos, so I have designed the code according to that. Logic is that: You need to input the desired angles and the code gives the corresponding time (in milliseconds) which you need to give in Arduino code.

Code link (Arduino and C++): shorturl.at/imnrQ


Code Implementation on my 3DOF Manas robot



I hope you have learnt something new today. Do comment if you have any doubt or mention some other info about MG996R in the comment box. You can visit my YouTube channel for robotics, science and astronomy videos. Have a nice day. 

No comments

Please don't put any spam link in comment box.

Powered by Blogger.