The power of code: Building a robot


I decided to have a go at building a robot for a STEM session last weekend, to show the power of code (see my last post about learning to code) and how it can be used to control something.  A moving robot is a great visualisation to demonstrate this.

After some searching I settled on a quadruped shown on thingverse (a 3d printing site). This is a build based on a robot called Chopstick Junior by Lumi.

Parts being printed, I set about thinking about the controls and motors. The parts are compatible with an SG90 servo. I needed 8 of these, for the knee and hip joints of the quadruped.

The assembly of the parts was a bit of a challenge, as the instructions said you needed 3mm x 6mm screw/nuts to connect the shoulders together. You really need 3mm x 8mm screw/nuts as I have to countersink the holes a bit more to get the nuts to attach to the 6mm screw length.

Inserting the servos into the holes in the hips and legs required the unscrewing of the 4 small screws in each servo and temporarily removing the gear top. Once inserted this can be replaced and screwed down.

Once the servos were in place I centered the servos the best I could to allow backwards and forwards movement in each joint. I know that later adjustment may be needed! This can easily be achieved by removing the servo arm and hip/leg, then re-positioning and attaching the arm again.

The power is provided by 4 x AA batteries in a square battery holder. This was glued down onto the frame and fitted well into the build.

The power bus is provided by a small piece of circuit board. I used two lines for -v and +v. Each servo was then wired into the lines and the control wire to an Arduino Nano which is used as the brains of the robot.

The following pins were used on the Digital I/O on the Arduino Nano:

  • D2 – Hip 1
  • D3 – Knee1
  • D4 – Hip 2
  • D5 – Knee 2
  • D6 – Hip 3
  • D7 – Knee 3
  • D8 – Hip 4
  • D9 – Knee 4

I also wired in a sensor on the front of the robot to D10 and D11.

Wiring is grouped together to allow movement, but cut to a length that reduces excess that could get in the way of the legs.

A toggle switch provides an on/off for the power fixed to the rear of the robot. Remember to break the lines on the circuit board so it does not join any of the lines.

Once assembled I then moved to coding. I decided to use the opensource code for the Chopstick Junior as a base and make alterations. The quadruped responded well to the code and several physical servo adjustments were needed. It’s now in a reasonable state; however, balance is an issue in terms of walking steadily, so I’ll need to work more on this.  I have not yet coded movement to respond to the sensors yet, this will need to be done later.

The code is available via my Github:

The robot went down well at the STEM session. Now to tweek that code……

Top view

Side View

Front View

This entry was originally posted in Max’s blog.

Max Hemingway — Senior Architect

Max is a senior architect for DXC in the United Kingdom. With more than 25 years of experience, he has a broad and deep range of technical knowledge and is able to translate business needs into IT-based solutions. Currently the chief architect of the BAE Systems account in the UK, Max has a proven track record acquired through continual client engagement and delivery of leading edge infrastructures, all of which have delivered positive results for end-clients, including IT cost reduction, expansion of service capability and increased revenues.


  1. Rajesh Hari Krishnan says:

    Thanks for the interesting write up. Very interesting project that I could extend for the summer kids project


  1. […] on code, get some tips on where to start, then try testing your coding skills to the next level and build and power a robot with code. In my next post, I’ll dive a bit deeper into Python code.  If you’re a beginning or […]

  2. […] Until next time, what are you working on? If you’re interested in taking your skills, building something and then powering it through code, check out this post. […]

  3. […] The power of code: Building a robot […]

Speak Your Mind


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