Introduction: Using MATLAB to Control the MePed V2 Walking Robot

The mePed v2 is a quadruped robot, which comes in a base kit and a complete kit. We will be using the complete kit in this tutorial, which includes an Arduino Nano controller.

Step 1: Set Up Arudino Compatibility With MATLAB

Make sure you have the Arduino IDE downloaded, as well as the CH340 USB to Serial Drivers found here under "Source Files." Download the appropriate driver for your device.

Now you need to download and install the MATLAB Support Package for Arduino Hardware.

Step 2: Connecting Your Robot

Open MATLAB and type "arduinosetup" into the command line. If you have the Arduino support library setup properly, a user interface should pop up prompting you to "Choose Connection Type." In this tutorial, we'll be using USB, but feel free to explore the "Wifi" and "Bluetooth" options in the future!

Under "Choose Board" click "Nano3", and choose the subsequent port that is listed under "Choose Port." If you only have the robot hooked up, it's likely that only one port will be listed. By default, the libraries that will be ticked will be "I2C," "SPI," and "Servo."

Click "Program." It may take a second to upload server code to the robot. Once you see the green success message, click Next.

Step 3: Declare Robot Objects in MATLAB

On a MATLAB file, you should now be able to declare an Arduino object. This is how you are able to access the pins and functions associated with the Arduino.

To declare the object:

a = arduino(port,'Nano3')

The port is the same port shown on the "Test Arduino Connection" page.

Now you'll want to declare the servos.

s2 = servo(a, 'D2')

s3 = servo(a, 'D3')

s4 = servo(a, 'D4')

s5 = servo(a, 'D5')

s6 = servo(a, 'D6')

s7 = servo(a, 'D7')

s8 = servo(a, 'D8')

s9 = servo(a, 'D9')

Step 4: Move Your Robot

To move the servos on the robot, you need to utilize the "writePosition" function.


Where s is the servo object, and position is a value from 0 to 1.

writePosition(s2, 1) % moves s2 servo to position 1

You can find out what position a servo is in by using


The code to make the robot walk forward can be found here.

(Note that this code may not work if your servos are wired differently).

Step 5: Using the Ultrasonic Sensors

The mePed v2 comes with HC-SR04 ultrasonic sensors, which allows your robot to read distance.

First, you need to download and install the MATLAB add-on library for the sensors. Make sure to follow the full instructions on the page, and place the zip file in the correct directory.

Once installed, you will need to type "arduinosetup" in the command line again. This time, when you get the choice of including libraries, you need to tick "JRodrigoTech/HCSR04." Program the code onto the Arduino.

Declare an arduino object. Now you should be able to declare a sensor object.

a = arduino('/dev/cu.wchusbserial1440','Nano3')

sensor = addon(a, 'JRodrigoTech/HCSR04', 'A3', 'A2')


You can now detect how much distance is between your robot and the object it is looking at!

Step 6: Creating a GUI

If you want to create an easy way to control your robot, you can create a GUI using MATLAB. To get started, type "guide" in the command line.

A user interface should pop up, and prompt you to create a new GUI. You can use one of the templates, but the blank GUI may serve the robot the best.

You can resize your GUI by clicking the bottom right corner of the gridded area and dragging. To add a button, click push button from the lefthand menu, and create a button of your preferred size in the gridded area. Double click on the button you created to edit the button's settings. The "string" field changes the text you see on the button, and the "tag" field changes what the function will be referenced by in the m file.

Click the save button, and an m file will be created for your GUI.

Step 7: Coding Your GUI

In order for the same arduino object to be accessed by all the functions in the GUI, you must create a handle for it. This is best done in the function "guiname_OpeningFcn" which executes just before the GUI is made visible.

The button that you just created will have its own function called "tag_Callback." This function will execute whenever you press the button. I created a button to make the robot walk forward, and simply pasted the walking code into this function.

Note that when you use writePosition(s,position), if you are writing to servo s2, you'll need to put handles.s2.

Make sure to put "guidata(hObject, handles)" at the end of the function to update the structure.

Step 8: Using Your MePed to Control an LED

You can control an LED attached to the robot.

You'll need:

A breadboard (preferably a mini one)



1 10k ohm resistor

Wire the circuit as shown above. The LED has two wires coming out of it - the longer wire is the positive side. The positive side should wire to one of the A pins (we used A0). The other side will be wired to GND.

Declare an arduino object. To turn the LED on and off:


Where a is the arduino object, pin will be the A pin you wired the LED to ('A0') and value is 0 for off, and 1 for on.