robotic arm – 6 DOF

Disclaimer:  I am neither an EE major or electrician.  What follows is based upon my understanding of the task which may or may not be correct.  This applies to wiring “design”/implementation as well as how the software interfaces.  By following my examples for wiring and by downloading and using the software you assume all responsibility.

Assembly of robotic arm purchased from Aliexpress.  This arm consists of 6 servo motors featuring metal gears.  The motors are:  Tower Pro – MG996R.  This kit provides more parts then are required.  I suppose this is because the base is multifunctional and depending upon how you assemble it those additional parts might be employed.

Pictures may be double clicked for expanded view.

Baseplate assembly with first servo motor.  Horizontal rotation.  Multiple angles. IMG_0605IMG_0606IMG_0607
Second servo motor.  1st Vertical lift. IMG_0608IMG_0609IMG_0610IMG_0611
It is important to note here that I have made a mistake.  In the picture above the bolt is in the wrong hole.  In the picture below I have placed it in the correct hole.  The alignment of this bolt must be opposite of the motor shaft on the other side.
This bolt will receive a bushing like the one pictured below.  IMG_0614IMG_0615
Building the vertical lift arm.  Multiple views. IMG_0616IMG_0617IMG_0618IMG_0620
Building the “claw” assembly staring with vertical lift servo.  Various views. IMG_0621IMG_0622IMG_0623IMG_0624IMG_0625
Again I point out the postion of the bolt opposite the motor shaft.  All servos have this bolt with bushing configuration.  IMG_0626
Notice how the bolt in picture below is opposive servo motor shaft.  If you don’t do this correctly you will spend more time disassembling the robotic arm then assembling it.
Fully assembled arm with claw attached. IMG_0628IMG_0629IMG_0630IMG_0631IMG_0632IMG_0633IMG_0634
View from base looking up. IMG_0635
Side view – arm expanded manually. IMG_0638
End result (lots of work remaining).  Tank design to incorporate arm and software to control it all. IMG_0639

The following table will attempt to show/explain the wiring configuration for the above arm (6 servos) and the Adafruit motorHAT.  Because of the attributes of the motors employed it was not possible to connect the motor’s servo cable to the 3 output pins on the HAT.  These motors would overload the HATs capability so the motor’s signal pin was connected to the HAT’s signal pin and the motor’s +/- wires are connected to a common +/- busbar (in this case a beefed up Adafruit 1/2 siezed Perma Proto board.  The wiring for a single motor would look like this:


For six servo motors the 6 V batter pack’s + lead would run to a + bus bar and then to each motor.  There are some photos later of this wiring setup.

First lets talk about the Adafruit ServoHat board.  It requires soldering.  Here is a mistake I made.  In the picture to the right I have mounted two servo motor HATs to show my error.  The very top HAT is incorrect.  The cables from the motor’s cable will fit but not stay attached (Notice the second picture-wrong).  I soldered the wrong end into the board.  The second HAT (one with the black and white wires) is correct.  All 3 pins are of the same length and will accepts the motor’s cable and stays put.  Also note the position of the plastic coupler in both boards. IMG_0663IMG_0664
Normal wiring configuration for a single servo and the HAT.  The battery pack (2AAX4) is powering the HAT and providing power to the servo.  Note how the servo’s cable (orange, red, black (orb)) plugs into the HATs output pins. IMG_0665
Here is the reinforced Perma Proto.  The top picture shows a 40 pin header – the top is neg(-) over pos(+).  The top row is all + the second row is all – .  To the far right is a couple of screw downs that connect to the battery pack which powers the servos.
The second 40 pin header will connect to the servo motor’s signal cable (organge) on the front and to the HAT’s signal cable on the back.  Note that only a few pins a soldered on this hat because they are straight thru.  Also the each vertical row of two pins is inter connected.The second picture show the rear of the proto.  The top is the top header consisting of two rows of pins that have additional wire connecting them to carry the load.  Make sure the solder does not connect these two rows or there will be a short.   The second header is female on the backsite with male pins on the other.  Believe it was an extender header used for staking HATs.  The pic make it look like the pins on the ends extend beyond the top pins but they do not.
One motor connected to the front.  Top two are power to the motor and the bottom one is signal to the motor. IMG_0673
This pic shows all motors and their signal lines connected to the front of the bus.  On the rear is the signal pins connected to the straight thru header connected to the signal pins on the motorHAT.  The two wires to the right and to the battery pack to power the servos and the single black wire connected to the green screw down connects to a ground on the motorHAT – it is the reference line.  The rear of the “bus” showing the signal wires from the motorHAT connected. IMG_0674IMG_0679Below the reference wire to the motorHAT – a more secure connection is required but for now this worked.  Make sure it does not come loose or extend too far below the board into the Pi or….
This is the setup for the leds that will be controlled (bright/dim) by the servoHAT.Second pic shows connection from board to servoHAT to output pins 15 (zero relative).  Note that the hat and NOT the bus bar (perma proto) will power the leds.  The +/- from the board run to the +/signal on the servoHAT IMG_0681IMG_0682IMG_0683

The video below is based upon the Adafruit 16-Channel PWM / Servo HAT for Raspberry Pi.  I have written a short python app to interface to the HAT and control 6 servo motors between their min/max servo positions.  For those of us new to servos I feel that this app will help understand how to control the servo in real time.  This should help also with defining your servo’s “servoMin and servoMax” parameters.  Just be sure to move slowly.  If your are not careful you can push the servo beyond in min and max offsets and possibly strip your servo’s gears or cause other damage.

If your servos have a total swing of 180o then you can move the servo using degrees once you have determined servoMin/servoMax.  You need to convert degrees to a value between servoMin and servoMax.  Here is the formula

rawrange = servoMax – servoMin

pulseLength  = (degrees / 180) * rawrange + servoMin

so if you want to move your servo to 30o:

servoMin = 150 (this would be 0o)

servoMax=450 (this would be 180)

rawrange = 450 –150

pulseLength = (30/180)*450 + 150 = 225 (moving your servo to this value would be 30o from the servoMin position

channel# will be the 3pin connector you have your servo connected to on the Adafruit HAT

issue the command:  pwm.setPWM(channel#, 0, pulseLength)

Regarding the videos and software.  I have removed the video for single servo control and replaced it with a newer video showing the control of all 6 motors and two leds.  I have removed the software for single motor control and replaced it with the new software for all 6 motors and leds.  You can use the 6 motor software to test a single motor.  In fact I would recommend this until you figure out your servoMin/servoMax values and frequency setting.  If I had to to this again I would have had the servos free standing (no arm) to check out their functionality.


Two downloads follow (they require the use of an Adafruit servoHat):

  1. the first will help you determine your servo motors minimum/maximum displacement.  It sets the following values:  servoMin and ServoMax to 100 and 600 and calculates the middle offset, ServoMiddle.  With the gui you will be able to control the servo and determine the values of servoMin and servoMax for your servos.
  2. NOTE:  Regarding the led control portion.  You cannot plug a led or two directly into the servo.  You must design your own small pcb with resister.  ALSO:  the program set channel 15 for the led control.  Be sure to use this channel only for your leds.  If you plug a servo into this channel then you will mostly likely overdrive your servo and damage it.
  3. the second will control a robotic arm with 6 servos and brighten/dim two leds.  This second download set values for my setup.  You will need to look at servoMin, servoMax. frequency, the initialization values, etc. and adjust as necessary for your setup.

servo_min_max.py_.gz (1097 downloads)

servo_control.py_.gz (900 downloads)

The following table attempts to show current loading on the arm for:

  1. all servos as measured on the common + bus
  2. a single servo.  I chose #2 because it has to support the entire arm and I wanted to know the current required for this effort.  The other servos with the exception of #6 would have less work to do. 

IMG_0684 bus_diagram

The diagram above shows the wiring for one motor using two buses.  The only connection of the servo to the servoHAT is with the signal pin between the motor and the servorHAT and the single “Reference Ground”.


The images to the left represents the robotic arms in various positions.  The numbers in each image represent the servo motor numbers in the first image.  Images “A” through “F” provide current loads on the arms positive bus, i.e., all servos under power while images “G” through “I” provide current requirements for a single servo.  I chose servo 2 for this measurement since it has to support the entire arm regardless of its configuration.  Note image “E”.  It show the arm horizontal with a small screw driver be held.

Applying power to the servoHAT and to all servos in their initial position show a current rating on the + bus of .681 amps.  To sustain the arm in the positions A-I the following current loads were noted:

Image Current
A 1.427 Amps
B 0.835 Amps
C 1.501 Amps
D 0.776 Amps
E 2.315 Amps
note:  1. that servo six is under heavy load gripping the object and 2.  servo two could NOT lift the extended arm with object, and  3.  servo 6 gets quite hot gripping the object over time.
F 0.502 Amps
G 0.005 Amps
I 0.816 Amps

The following table shows voltages across the Servo5’s lines – Orange, Red, Brown at various slider settings

Servo5 slider values 120 200 300 400 500 530 after “quit”
RB 4.99 5.03 5.03 4.98 4.71 4.53 5.17
OR 4.89 4.83 4.80 4.74 4.45 4.36 5.17
OB 0.92 0.156 0.235 0.233 0.231 0.233 0
+/- busbars 4.99 5.04 5.03 4.98 4.68 4.51 5.17
2 LEDS – slider values 0 200 300 1400 3100 4095 after “quit”
OR 2.72 2.70 2.70 2.62 2.51 2.435 2.72

Note:  OR + OB = RB seems to be true.  Some variations occur. Could be interpretation of meter or not waiting for meter to settle.

LED values are coming strictly from the ServoHAT, i.e., the signal pin (O) and 5V+ (R) pin at various slider settings.

11 Responses to robotic arm – 6 DOF

  1. Michael says:

    Hello. I have download both the codes and have attempted to run them on Python 2.7. However, most of the lines get error messages that there are indent errors or names like self are not defined. What am I doing wrong?

  2. ron says:

    This code was written and tested on Python 5, but I don’t believe it would have a problem on Python 7. Python code sets up it programming blocks like “if” using indentation. If the block of code is not correct it will complain about indentation problems. I downloaded the code and do not get any indentation problems. It is possible that your editor is reformatting the code resulting in “indent errors”. The code contains one “self” definition and many references. That failure could be due to an improper “indent” on that “self” definition. Which editor are you using?

    Try downloading the code and running: ./ without viewing in an editor. Note that you need to make it executable: chmod +x

  3. Michael says:

    Thanks – I’ll give it a go….Michael

  4. ron says:

    Let me know. I no longer have a test bed set up for servos at this time so it would be hard for me to test, but I suppose I could come up with something to test with should you continue to run into trouble. It will take a few days though.

  5. Michael says:

    I am getting a syntax error in line 72 with ” servo1_rotation_angle_label” being highlighted. I can’t for the life of me seem to find anything wrong with it.
    I am sorry to keep worrying you but I would love to have this operating properly as I wish to control about 8 servos to operate some turnouts on my s scale layout.

    • ron says:

      So are you saying that “indent and self” errors are now fixed? and that you are having just this syntax error?

      Line 72? Do you mean line 171: servo1_rotation_angle_label = Gtk.Label(label=”Servo 1 Rotation Angle”) Make sure the line looks exactly like what I have printed here. If you are a perl person you may have added a trailing “;”.

      This line will Place a slider header title (label) “Servo 1 Rotation Angle” at the top of the display. It is part of the gui build. You say you are getting a syntax error but do not tell me the error. Cut and paste the error so I can see it. Look at line 59: “from gi.repository import Gtk” . Make sure this line is not causing a problem, i.e., proper indentation, no typos, or extra characters.

  6. Michael says:

    Thanks once again. I will be away from my pc for a week or so, I will get back to you then.

  7. Michael says:

    Just one more thing. see below:
    Traceback (most recent call last):
    File “/home/pi/Downloads/servo_control.py_-1”, line 129, in
    class ServoWindow(Gtk.Window):
    File “/home/pi/Downloads/servo_control.py_-1”, line 139, in ServoWindow
    self.connect(“delete_event”, self.delete_event)
    NameError: name ‘self’ is not defined
    The check module does not show any areas but run module shows the above.

  8. Prakash says:

    Good detail, Thank you for sharing. I came across this voice controlled 6DOF Robotics if anyone interested.

  9. pierra says:

    my myy.. thanks sooo much its working perfectly

    I just had to get the files for Adafruit_PWM_Servo_Driver and Adafruit_I2C from the link ( all in one folder as the downloaded files.

    Bless up man!!

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.