Connect Minecraft and Arduino - Connect Your Minecraft World With Reality

 

 

 

 

 

We have developed a set of tools to make it possible to connect your mods with the real world using Arduino. This can be a neat thing for a weekend project or school projects and can also be an introduction to world of microcontrollers and electronics. On this page, you can find getting started instructions, sample projects and downloads required to start working on your Minecraft-Arduino projects.

 

Installation instructions

  1. Download and install MCreator
  2. Download and install Arduino IDE
  3. Download MCreator's Arduino Toolkit below
  4. Follow the instrucions below to make your first project and to setup Arduino

 

[MCreator's Arduino Toolkit download]

 

Hardware setup

For our demo projects, you need to make a circuit similar to the one shown below. Of course, you can modify it later as you become confident with our sample projects and understand what they do :)

 

For our demo Arduino to Minecraft projects, you need to make a circuit similar to this one

The resistors we used are 220ohm for the RGB LED and 10kohm for the pushbutton.

 

Programming Arduino

First, you need to connect your Arduino (Uno, Mega 2560 or similar) to the computer. Open Arduino IDE, open sketch file located in the shetch sub-directory of the downloaded toolkit and upload it to arduino. You can find a tutorial on how to do this on the Arduino's official howto page. You can of course modify the sketch to your needs if you wish.

Upload sketch from MCreator's toolkit to the Arduino board.

 

After you upload the sketch to the Arduino you are ready to connect it to your MCreator project. Each time you want to connect your Minecraft with MCreator for Arduino mod, you need to start the MCreator to Arduino connector which will forward your requests from the MCreator's mod to the Arduino.

 

Upload sketch from MCreator's toolkit to the Arduino board.

Upload sketch from MCreator's toolkit to the Arduino board.

 

You need to select the serial port to which the Arduino is connected and click the Connect button. After you do this, you can start Minecraft with your Arduino compatible mod and start using it. If you don't run this connector, your mod will still load but won't affect or control the Arduino.

 

Code snippets

After you have connected your Arduino, it's time to make your mod. You can either send data to Arduino (control things like lights or motors) or receive data (status of sensor, a button, ...).

 

SENDING DATA TO THE ARDUINO

This code sample shows how to send the control code to set the color of the LED on your hardware setup you previously made. You can see that this code will affect the Arduino sketch you uploaded previously so if you make changes to it, you might need to send your custom commands here. You can put this code in any event. Use Custom code event result and pase the code you see below in it. Don't forget to modify the value of the data message you send to your needs.

String msg = "[your data]";
try{java.net.DatagramSocket socket = new java.net.DatagramSocket();socket.send(new java.net.DatagramPacket((msg+""+(char)13).getBytes()
,(msg+""+(char)13).getBytes().length,java.net.InetAddress.getByName("127.0.0.1"),5050));socket.close();}catch(Exception e){}

For the sample needs, replace [your data] with red, green, blue, magenta, cyan, yellow or black to set the color of the RGB LED on the circuit.

 

RECEIVING DATA FROM THE ARDUINO

The code to receive the data is a bit more tricky. You need to put this code in the Custom code result of the update tick event of the block. This block needs to be present in the Minecraft world at least once in order to receive data from the Arduino. In this code, you need to add custom conditions and with them, you can set your custom variables and use them in your mod later.

if(System.getProperty("udp.server")==null){
Thread t = new Thread() {
public void run() {try {for (;;){
java.net.DatagramSocket serverSocket = new java.net.DatagramSocket(5051);
java.net.DatagramPacket receivePacket = new java.net.DatagramPacket(new byte[128], 128);
serverSocket.receive(receivePacket);
String data = new String(receivePacket.getData()).trim();

if(data.contains("[customStringOne]"))
    mcreator_VarList%MAINMODNAME%.[varNameOne] = !mcreator_VarList%MAINMODNAME%.[varNameOne];

serverSocket.disconnect();serverSocket.close();
}} catch (Exception e) {e.printStackTrace();}}};
t.start();System.setProperty("udp.server", "run");}

Replace the [customStringOne] with the data you want to be received in order to change the variable with name [varNameOne] (you need to replace this with the actual name of the variable). For our example, replace [customStringOne] with button_high. This string is sent from Arduino each time you press the button. The line below the if condition requires the boolean type variable to be defined on the MCreator's Variable list. Make sure to set the proper name. This variable will then toggle from true to false and back on each button press on your circuit. You can use this variable in other mod elements.

 

Tutorials

 

Further work

If you would like to extend the functionality of this, you are welcome to do so as all the code of this project is open-source. If you have problems, you can ask a question in the Advanced modding section of our forums or you can contact us to help you out. We will also periodicaly upload sample tutorials on our YouTube channel.