Skip to main content

Minecraft controlled Raspberry Pi Robot Arm

Guest Blogger Hiren Mistry, Nuffield Research Placement Student working at the University of Northampton. Nuffield Research Placement scheme provides students in their first year of a post16 course to work with STEM professionals

How to use Minecraft to move a USB Robot Arm on a Raspberry Pi
By Hiren Mistry
Currently, I am taking part in a Nuffield Foundation Research Placement at the University of Northampton, researching how to control a CBIS Robot Arm using a Raspberry Pi. This part of the project aims to learn more about using Python in Minecraft and how I can control the Robot Arm using Minecraft. When the player hits a specific block, the Robot Arm will move in a specific way.
Minecraft is an adventure game where players are put in a randomly-generated world and can create their own structures and contraptions out of textured cubes.
Here are the main commands used in Python for Minecraft Pi:
from mcpi.minecraft import Minecraft
Import modules from Minecraft folder
import minecraft.block as block
Imports Block Module
Used for creating a connection, modifying players and blocks and capturing events with the Minecraft World. This command uses the default address and port.
Return current position of player (as a float)
Post message in brackets to Minecraft Chat
Sets block (id) at position x, y, z
Moves player to position x, y, z
blockEvents =
for blockEvent in blockEvents:
This returns any block event hits since the last time the command was run.
Returns the position of the block that was hit

Before developing the main program which controls the Robot Arm, I wrote some basic programs which enabled me to learn more about how Python interacts with Minecraft Pi. One of the programs I created leaves a trails of blocks behind the player. Below is the code with explanations for each line. It works by using a While loop and continually checks to know if the player has moved. If the player has moved then a block will be placed. The block number (38) can be changed. 

For a list of block numbers and more information, go to

import mcpi.minecraft as minecraft #imports minecraft modules
import mcpi.block as blocksw #imports block modules
import time #imports time module needed for time.sleep()
mc = minecraft.Minecraft.create()#creates connection to mc game
while True:#Starts while loop
    pos = mc.player.getTilePos()#Gets position of player
    time.sleep(0.1)#Sleeps for 0.1 seconds
    newpos = mc.player.getTilePos()#Gets new position of player
    if newpos != pos: #if the position has changed
        mc.setBlock(pos,38)#Set Block 38 down on previous block

Robot Arm Program in Minecraft
Whilst researching into Minecraft Pi I had the concept that the user would press a button in Minecraft and a Minecraft ‘Sign Post’ would allow the user to know what that Minecraft button does. However, due to Minecraft Pi limitations Buttons and Signposts (with text) do not exist so therefore they cannot be used. As a solution, I produced a model of the Robot Arm in Minecraft (See Picture below), which would enable the user to move specific parts of the Robot Arm.

The torches are the blocks which will activate the Robot Arm movements (Except for the Grip Open/Close and the Rotate Clockwise/Anticlockwise blocks).
Limitations: As only one block can be hit at once, only one command can be out put to the Robot Arm. Therefore, multiple commands are not available.
An easier way of storing data, such as the co-ordinates of each block, was to use a dictionary. A dictionary contains keys which are assigned to values. For example, Dictionary = {"Key1":Value1, "Key2":Value2}would store two keys where "Key1" is assigned to Value1 and "Key2" is assigned to Value2.
A more efficient way of storing the commands and co-ordinates would be to store the commands in another dictionary with the same key name. E.g.
Coordinates = {"Shoulder Up":[77, 67, 1], "Shoulder Down":[74,67,1]}
Commands = {"Shoulder Up":ShoulderUp(), "Shoulder Down":ShoulderDown()}
I could then search through all the keys and if the coordinates of the block hit equalled the value of a key, then the corresponding command would be executed. Using code such as,
For k, v in Coordinates:
If BlockHit == v:
However, this code causes the an error (ValueError: too many values to unpack) as the Python code has to go through all the keys and values until a match is made. Therefore, I have decided to continue with the simpler code which uses many IF statements to decide which command to use.

Final Program

Below is the code which allows the player to hit blocks which control the Robot Arm.
Once Minecraft is running, press "TAB" and run the Python code and then switch back to the Minecraft Window.
Note: When hitting blocks in Minecraft, the sword has to be used to activate commands.
import mcpi.minecraft as minecraft #Imports Minecraft Module
import mcpi.block as blocksw #Imports Block Module
import time
from Maincommands import * #Imports Commands from file
mc = minecraft.Minecraft.create() # Creates connection to Minecraft
mc.postToChat("Robot Arm Control")#Post message to Minecraft chat
pos = mc.player.getTilePos()#Gets position of player

while True:
    blockHits = the block hit events
    if blockHits:# if a block has been hit
       for blockHit in blockHits:   # for each block that has been hit
            xyz = [blockHit.pos.x,blockHit.pos.y,blockHit.pos.z]
            Dict = {"Grip Open":[74,12,-70],"Grip Close":[72,12,-70], "Rotate AntiClockwise":[77,12,-63], "Rotate Clockwise":[77,12,-67],"Shoulder Up":[77, 13, -64], "Shoulder Down":[77, 13, -66], "Elbow Up":[77, 16, -64], "Elbow Down":[77, 16, -66], "Wrist Up":[77, 18, -64], "Wrist Down":[77, 18, -66], "Light On":[75, 19, -65], "Light Off":[76, 19, -65]}# Dictionary with Command and coordinates of block
            # Outputs command depending on block hit
            if xyz == Dict["Grip Open"]:
            elif xyz == Dict["Grip Close"]:
            elif xyz == Dict["Rotate Clockwise"]:
            elif xyz == Dict["Rotate AntiClockwise"]:
            elif xyz == Dict["Shoulder Up"]:
            elif xyz == Dict["Shoulder Down"]:
            elif xyz == Dict["Elbow Up"]:
            elif xyz == Dict["Elbow Down"]:
            elif xyz == Dict["Wrist Up"]:
            elif xyz == Dict["Wrist Down"]:
            elif xyz == Dict["Light On"]:
            elif xyz == Dict["Light Off"]:

All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Popular posts from this blog

Micro:bit, Servo control with Micropython or blocks

You can control servos (small ones) from a Micro:Bit directly. Following a link from the David Whale (Twitter ) , thank you, took me to a Kitronik blog post,, which has the answer.

The code uses Microsoft Blocks taken from the post, runs the servos 180 degrees and back again, when button A is pressed. It does exactly what it should. I am also using the Tower Pro SG90 servo.
Can it be replicated in Micropython? This is a new mini project, there seems to be little out there yet on how do this but the best so far is this video by PHILG2864:

The closest I have is the following, it is essentially there.
from microbit import *
while True:

Setting the time period to 20ms  pin0.set_analog_period(20)seems by experiment (and used in the video above) to be best value so far. The reason for pin0.write_analog(1)  set to 1 i…

LittleBits Star Wars Droid and Swift Playgrounds

On the 1st September 2017, during 'Force Friday', LittleBits launched their new kit Star Wars Droid Inventor Kit an R2D2 shaped robot, though you are encouraged to customise it to form your own designs. It comes a number of tutorials, that take you through building a moving head, a proximity sensor to move away from you, and many others.

So confession time, I am not the target audience for this kit, but I have enjoyed playing with it. The tutorials take you through building and dismantling the kit, doing a range of different activities and in most cases controlling it remotely from an iPad. You can even record your voice and have it played back from the Droid, in my opinion, the wide of sounds is one of the things that lift this from being just a nice kit -  I will get onto the other one soon. Though good fun, I was left with a question can it be programmed?

This was my first time using a LittleBits kit I didn't know what the options were available for programming it, a quic…

my robot BETT2017

I will start with a confession, I only had about 2 1/2 hours at BETT 2017 due to external time pressures so to say I didn't yet a chance for a good (or even a bad) look around is an understatement; so I am not reviewing the show just a few notes on what I did manage to see.

STEAM Village
First and mostly, it was great to talk to so many people, only few I had met face to face previously, about robots, micro:bits, Raspberry Pis and coding. Most of this happen in the relatively small (compared to the event space) STEAM village and nearby stalls. It was great to see the strong presence of both Raspberry Pi and Micro:Bit Foundation, along the variety of different activities and example usage of both, with Code Club (I know it is part of Raspberry Pi Foundation) there was well. This was all alongside some other companies

Four of these stuck in my mind.

1. DFRobot ( with their range of Arduino-based robots and non-programmable kits. The two kits that caught my eye w…