Simple Robot Arm

3D print a simple robot arm you can control over WiFi using Phew! and the Pimoroni Inventor 2040 W

25 September 2022 by Kevin McAleer


Table of Contents


Video

Here is a link to the YouTube video that covers the operation and programming of the Simple Robot Arm.


Grippy-Bot

Grab the 3d printable STL files for the Grippy-Bot from Cult 3d - https://cults3d.com/en/3d-model/gadget/grippy-bot. This model is a free to download, and is pretty quick to 3d print. I used the Cura slicer with ‘Standard’ print settings and these worked out fine. I managed to print all the items on the build plate at the same time.

Picture of the completed 3d printed robot arm


Bill of Materials

Part Description Qty Cost
SG90 Servo These are the motors that move the parts of the arm 5 £4.00
Inventor 2040 W This is the brains of the Robot arm and will provide WiFi access. Available from Pimoroni 1 £34.50

You’ll probably find packs of Servos online cheaper than buying them individually.


MicroPython Code

This project enables you to control the robot arm over wifi, using the amazing Phew! library from Pimoroni. Phew provides simple functions for creating an Access Point, serving Webpages from the Pico W and a templating engine for embedding variables in those web pages.

Download the code from here: https://www.github.com/kevinmcaleer/inventor_arm

You’ll need to install Phew! on the Pico W, and copy over the index.html file too. You can upload files using Thonny (Right click on the files and folders to upload from the thonny files menu). If you want to know more about how to install Phew! Watch this part of this video

There are two MicroPython programs in the repository:

  • test01.py - A simple test program to check the motors are working - they will all move from their minimum to maximum values
  • test02.py - This is the main program, it will display a web-based user interface on the IP address logged to the console. Just type that address into the web browser of any device, phone, table or computer on the same network to access the Robot Arm webpage.

Overview of the code

There are two parts to the robot arm code - the MicroPython that runs on the Inventor 2040 W, and the Javascript that runs on the browser, from the index.html file.

MicroPython code

The Wifi SSID and Password are defined in a config.py file on the Pico W - if this doesn’t exist you’ll need to create it with the following content:

wifi_ssid = '<enter your WIFI name here>'
wifi_password = '<enter your WIFI password here>'

The first block of code brings in all the software libraries needed for this project, including the phew! library, and the Inventor 2040 W library.

from phew import *
from phew import connect_to_wifi, server, logging
from phew import render_template
from config import wifi_ssid, wifi_password
from inventor import Inventor2040W, SERVO_1, SERVO_2, SERVO_3, SERVO_4, SERVO_5, SERVO_6 
from time import sleep
import math

The next block of code connects to the wifi network, creates a board variable and sets each of the servos to its middle position.

# Connect to WiFi
ip = connect_to_wifi(wifi_ssid, wifi_password)

# Create a new board
board = Inventor2040W()

# Set all servos to mid position
for servo in board.servos:
    servo.to_mid()
    print(f'servo: {servo}, value {servo.value()}')
    sleep(0.1)

We create a position function to take values passed to it from the webserver and sets the servos accordingly.

def position(arm=None, wrist=None, elbow=None, finger=None, base=None):
    """ Set the servo positions """
    if finger is not None:
        board.servos[SERVO_2].value(finger)
    if wrist is not None:
        board.servos[SERVO_3].value(wrist)
    if arm is not None:
        board.servos[SERVO_4].value(arm)
    if elbow is not None:
        board.servos[SERVO_5].value(elbow)
    if base is not None:
        board.servos[SERVO_6].value(base)
    sleep(0.01)

The next block of code listens to any web requests to the / url and either send the rendered index.html file back to the users browser, or processes the servo slider positions, one for each of the servos.

@server.route('/', methods=['GET','POST'])
def index(request):

    if request.method == 'GET':
        return render_template('index.html')
    elif request.method == 'POST':
        
        elbow = request.form.get("elbow", None)
        arm = request.form.get("arm", None)
        base = request.form.get("base", None)
        finger = request.form.get("finger", None)
        wrist = request.form.get("wrist", None)
        if elbow is not None:
            position(elbow=int(elbow))
        if arm is not None:
            position(arm=int(arm))
        if base is not None:
            position(base=int(base))
        if wrist is not None:
            position(wrist=int(wrist))
        if finger is not None:
            position(finger=int(finger))

        # Try without the line below to speed up the response
        return render_template('index.html')

The last block of code shows the IP address of the Inventor 2040 W after it connected to wifi earlier, and then starts the Webserver.

# Show the IP Address
logging.info(f'IP: {ip}')
logging.is_disabled = True

# Start the server
server.run()

Javascript

The other code we need runs on the users browser and is therefore written in Javascript:

function post_values()
{
  $.post(
  "",
  { elbow: elbow.value, base: base.value, arm: arm.value, wrist: wrist.value, finger: finger.value },
  function(data) {
    
  }
);
};

This code takes the values from each of the sliders and posts them to the webserver. It uses JQuery to simplify the code required to post values back to the webserver. Javascript can look a bit esoteric to the uninitiated - I’m not a massive fan of the language as its now obvious whats going on from the code itself, and it requires a firm understanding of the grammar and syntax to have the vaguest of clues as to what is going on.


Wiring up the Servos to the Inventor 2040 W

Wiring up the robot arm is pretty simple - just plug each of the servo connectors into the Inventor 2040 W using the connections as shown below:

Servo Inventor 2040
Finger Servo 2
Wrist Servo 3
Arm Servo 4
Elbow Servo 5
Base Servo 6

Picture of the Wiring diagram


The Web UI

The Web UI has 5 sliders - one for each of the servos. The middle position corresponds to the Servos middle position.

Picture of the Web UI

If you move the slider and then release the mouse button, the servo will then move to the position selected. The Pico Phew! library will find it difficult to move the servo in realtime sync with the slider - ask me how I know!


Conclusion

This is a great starter project to experiment with robotics. There are lots of area to improve upon this project including:

  • stronger servos
  • servo position feedback (requires a higher quality servo with feedback wiring)
  • more robot 3d design
  • longer arm for further reach
  • double jointed servo claw to enable better and more precise handling of objects with the claw

Did you find this content useful?


If you found this high quality content useful please consider supporting my work, so I can continue to create more content for you.

I give away all my content for free: Weekly video content on YouTube, 3d Printable designs, Programs and Code, Reviews and Project write-ups, but 98% of visitors don't give back, they simply read/watch, download and go. If everyone who reads or watches my content, who likes it, helps fund it just a little, my future would be more secure for years to come. A price of a cup of coffee is all I ask.

There are a couple of ways you can support my work financially:


If you can't afford to provide any financial support, you can also help me grow my influence by doing the following:


Thank you again for your support and helping me grow my hobby into a business I can sustain.
- Kevin McAleer