Let's Make Robots!

Hexapod Robot using Arduino - Full Source Code Available

Arduino Mega controlled Hexapod robot with 9g Servos
ServoShield.jpg372.56 KB




I have seen quite a lot of Hexapod robots on the internet with high torque servos with ATOM processors, but not the amazing Arduino with 9g servos, so I thought it might be a good project building one!

I don't usually have time during the week, so I do most the theory and calculations at nights, and leave the testing, building at weekends.

I am very happy to share my code, but I don't want you to turn to me after downloading my code and say, "hey you code won't work!" So I won't publish it until I am happy with it. it's kind of messy also, and it requires a lot of tidy ups!

This robot is based on the Arduino Mega 2560, currently controlled by a WII nunchuck controller. It's got 18 3-dollar 9g servos, and is using 2 packs 4xAA batteries 4.8V. I will post any update from my project here, in the meantime don't hesitate to ask me any question you might have. I will reply if I have an answer. thanks

Project Main page: http://blog.oscarliang.net/hexapod-robot-with-3-dof-legs-degree-of-freedom/

If you want to know How the Inverse Kinematics algorithm works on this robot, please check out my website here: http://blog.oscarliang.net/inverse-kinematics-ik-implementation-for-3dof-hexapod-robot/


Inverse Kinematics Tutorial:





I wanted to make a servo interface with the shield I bought off ebay, which would making it so easy to install the servos without making a mess. In theory i could use 48 servos on a Mega board, but I only soldered 20 servo ports, just to keep wires tidy and compact. I need only 18 servos for the legs and possibly 2 for the sensors anyway. I am leaving some space on the right hand side of the board to put a adjustable voltage regulator in, as I am planning to use 8xAA batteries, or 3 lipo 11V batteries in the future.

check ServoShield.jpg






Found a coupld of bugs in the algorithm, thanks to my Excel spread sheet simulation! Now the pitch and roll movements finally work properly, although the legs are still quite unstable. I still don't know why that is, maybe the dimension is still not correct, or maybe there are still bugs in the algorithms.








I have now adapted rotation matrix in Body IK algorithm, instead of estimating coordinate changes, i will be using formula derive from rotation matrix to calculate these changes (detail could be found in my last post, IK implementation). I have also started to use micro seconds instead of degrees to write servos. I have also had a major modification on the legs. It's now doubled the length of the original. Found a problem with pin22 and pin32 when using them as servo pins. These two pins interference with each other, ie, control one of them would control the other one too. Luckily I made 20 pins, so I can just easily move up all the pins by 1, ie, now using 23 - 41. We had a water tank overflow on the roof tonight, was a bit scary and we spent hours looking at the problem, so didn't do much testing on the new setup. will try tomorrow. The main problem with it at the moment remains to be leg-end position accuracy. I don't know whether it's the hardware or the software. But since I have move all the calculation into floating point, it should give relatively good numerical results. I have started looking at Gait, might implement 6 step ripple gait first.







Last night, I implemented a simple 6 step ripple gait, which I will explain in a minute. Let me explain what's the problem I discovered first with the IK algorithm. When the legs are meant to move along Z axis, the front and rear legs are actually moving toward Z-X direction, which is not right. So I wrote a test program to test the leg IK algorithm by checking the actually physical change of the leg position, versus the coordinate I entered manually. Again the legs won't move solely along one particular axis (X or Z), but tend to go somewhere in between. Spent an evening thinking about it, realize the original idea of calculation IK isn't perfect for my type of hexapod robot, which each leg are in a different frame (frame - Axis system), but I have always been assuming they are in the same frame as the center of body. for example, the frame of the front right leg is rotated -30 degree from the frame of the center of body (see below picture), so before we calculate the change of angles from leg IK algorithm, we need to do coordinate transformation first, to convert it from body frame to the coxa frame. A common way would be using rotation matrix. detail please go to my IK implementation post. after the modification, the robot can finally walk like one. ; )







Good news from Robotshop! My parts have finally arrived their warehouse and has been already shipped to me. So I got to hurry up finishing body balance algorithm this week (probably my last implementation on IK)

What I have done This weekend I:

1. tidied up the code a lot!

2. implemented fast SIN, COS, ACOS functions (about 10 times faster than standard C trig functions! body rotation is so much smoother now)

3. implemented 7 more gaits (thanks to Jeroen Janssen's source code, I copied most of the stuff over)

  (1) Ripple 12 steps

  (2) Quadripple 9 steps

  (3) Tripod 4 steps

  (4) Tripod 6 steps

  (5) Tripod 8 steps

  (6) Wave 12 steps

  (7) Wave 18 steps

4. modified code to avoid using 'delay()', but to get the time using 'millis()', so processor can do other stuff while waiting rather than just freezes there.Also brings a bit more smooth movement there.

All looking great! however because I am getting so many functions and gaits now, I am having trouble with controls... nun-chuck is simple and handy, but it's very limited in terms of control options. I might need to come up with a menu system that I can switch between modes (body rotation, walking mode), and under walking mode i can also switch between gait types... I just love the nunchuck and don't want to replace it!







In the April 2012, I bought some metal legs and body, and implemented 8 different gaits, shown in the video: 

you can find the detail here


To be honest, it's not worth t get the legs and body, too expensive and too time consuming to asseble, just get some cheap styrene and it will just work as good as the expensive hobbist shop parts.

I might sell them soon, if you are interested please let me know! ^.^





In the last few weeks I have been learning about Android Java programming, and finally wrote a android app to control the hexapod robot via bluetooth.

This gives so much more options than the Nun-chuck, I can use it for a bit calculation for inputs as well to improve smoothness. 

If you are interested in the source code for the app please let me know! ^.^





I have been very busy recently, so it will be the end of the Mini Hexapod :( so I decided to give away my source code.

Full Arduino Source Code for V1.3 is available to download - Scroll down to the bottom:





Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

very cool :)

Thank you! ^.^

Hi oscar,

Great work, smooth moves, cool ! And thank you for your blog about IK, it will soon be very useful to me I think :)

Good luck with the next steps !


Need to watch this :-) Good project. 


Any code you have and are willing to share would be great to look at. I am no where near programming a robot with legs but store all code samples I can for future experiments. I defiantly interested in the Android program. Looking at adding blue tooth to my project soon. The robot is looking nice. Did a good job cleaning up the wires with out restricting movement. Thanks Shon

Thank you Shon

Hi... you asked for the source code a while ago, it's now available in my blog:


Full Arduino Source Code for V1.3 is available to download:



Thanks for the source code! one question, where did you buy the arduino servo shield?