In this section you can find information about robotics and illustrations of how to build robots.

How to build a wheeled robot and control it with your iPhone

In this tutorial you will learn to build a 2WD robot that can be remotely operated via bluetooth using a smartphone equipped with gyroscope sensor, such as an iPhone.

Note that the tutorial can be simply adapted to Android. In that case you will need to write the app for sending commands to the robot by your self.

Here is a list of components that you will need to complete the project:

  • An iOS device: iPad, iPhone or iPod Touch. The important thing is that your device has a gyroscope, because we are going to use it for the control of the robot. As I already mentioned before, you can also use an Android smartphone. The only thing that changes is the last part of the tutorial, in which you will have to write by your self a software that sends the velocity values of the wheels to the robot.
  • For iOS device you need a Mac and to be member of the Apple Developer Program. Link
  • Arduino Uno R3 Board. Link
  • Magician Chassis (which includes two DC motors and a caster ball wheel). Link
  • Adafruit Motor Shield v2.0. Link
  • A 400 points prototyping breadboard. Link
  • Jumper Wire Kit. Link
  • The bluetooth 4.0 module RedBearLab Ble Mini v1.1. Link
  • 1 Battery Holder 4xAA. Link
  • 1 Battery Holder 6xAA. Link
  • 1 DC Barrel Jack Plug. Link
  • 10 AA rechargeable batteries at least 2400mAh. I have used 6 Duracell to power Arduino and 4 Sanyo Eneloop Pro to power the DC motors. The best are Sanyo Eneloop Pro, but Duracell is also good.
  • Essential tools for soldering. If you don't have a solder you can buy the Sparkfun Beginner ToolKit. EU Edition Link or US Edition Link
  • Break away headers with right angle. Link

The 2WD robot I built has 2 IR sensors mounted on the front. The two Sharp IR sensors are able to sense obstacles between 10 and 80cm. You don't need them for the purpose of controlling the robot with the iPhone, but you can use them to give the robot some autonomous behavior.

The tutorial has an optional part that shows you how to connect and use them with the robot.

If you intend to follow this optional part you also need:

By the way, you can buy all the components wherever you want. I personally didn't bought them on Sparkfun because of their international tax policy, but I was able to find everything in a local reseller.

Mounting the Magician Chassis

The Magician Chassis Kit is a very basic chassis, but it's cheap and it includes motors, wheels and a 4xAA battery holder. Everything you need to get started. Below the part list of what you will find in the kit is shown.

The kit comes with printed instructions. If you don't have them you can download them by clicking here.

The instructions are very clear. I just want to point out that you don't need to install the speed board holders. I think they are used just in case you want to use an encoder to measure the distance traveled by the wheels.

The battery pack 4xAA

Before moving on, we need to mount the 4xAA battery holder on the top part of the Magician Chassis. Mount it as shown in the picture so that the the top part of the chassis remains free and so that the battery holder stays in between the top and the bottom part of the Magician Chassis.

You need to fix the battery holder to the chassis, in some way. An idea can be to use a cable tie, like the yellow one in the picture.

Remove the batteries from the holder or make sure that the red and the black wires don't touch each other.

Mounting Arduino Uno and the Breadboard

In the next step, we will mount Arduino Uno on top of the Magician Chassis.

First you have to place three L10 spacers as shown in the picture. Fix them with screws and be sure they are well aligned, so that Arduino can be mounted on top of them.

Place the Arduino on top of the spacers so that the holes in the spacers are aligned with the holes in the Arduino Uno board.

As you will notice a hole in Arduino will not have a correspondent spacer aligned. Take the fourth L10 spacer and screw it on Arduino in that particular place. Now place again the Arduino board on top of the L10 spacers which are mounted on the chassis and screw the screws.

Next you have to mount the breadboard in front of Arduino Uno. For now, ignore the fact that there's a blemini on the breadboard. Try to fix the breadboard in some way. Here, again, I used a white cable tie.

Before fixing the breadboard with the tie, be sure that the breadboard and Arduino are near as in the picture. This is crucial because in the next step we are going to mount the Adafruit Motor Shield on top of Arduino and we are going to need 4 cables going from the motor shield to the breadboard.

Assembling the Adafruit Motor Shield

The Adafruit Motor Shield comes disassembled. You will need to use a solder to install the headers and the terminals. You can follow the tutorial on the official website of Adafruit.

When you finish assembling the headers and the terminals the result should be like the one in the two pictures below.

Now you need to solder 4-6 wires to the Adafruit Motor Shield board. You can use the wires on the Jumper Wire Kit

Solder 2 long wires on the pins: 5v and Gnd. If you want to use the IR sensors you need to solder 2 other wires respectively to the Analog 0 and Analog 3 input pins. In truth, you can use any among analog pins 0, 1, 2 and 3. Unfortunately, analog input pins 4 and 5 can't be used because they are reserved for the motor shield.

You can ignore the fact that in the image there's a wire soldered on the Vin pin. You don't need it.

Two wires should be soldered on the digital pin 1 and 0, which are connected respectively to the TX, RX pins of Arduino Uno. The two red wires will be connected to the BleMini bluetooth module through the breadboard, so make sure they are long enough to reach the breadboard.

Merging Arduino with Adafruit Motor Shield

After you finish soldering the wires on the Adafruit Motor Shield, next step is to insert the board into the Arduino Uno board. Make sure all the male header pins of the motor shield are aligned correctly with the Arduino Uno's female pins. Then make a small pressure on the top of the motor shield, until the boards are merged together.

If the two boards are merged correctly, then they should look like in the next picture. Please double check that the shield is inserted correctly because uncorrect connection can burn your components once connected to a source power.

Connecting the motors to the Adafruit Motor Shield

In order to control the velocity of the motors you need to connect them to the Adafruit Motor Shield. Find a convenient path for the cables and connect the left motor to the M3 terminal and the right motor to the m4 terminal. Use the exact order shown in the pictures.

Black on the external terminals, Red on the internal ones. Leave the center terminal empty. Be sure that terminal on the left, marked as M3, is the one to which left motor's red and black cables are connected, while terminal on the right, marked as M4, is the the one to which right motor's red and black cables are connected. Double check this!

Assembling the BleMini

BleMini is a bluetooth module that allows serial communication between Arduino and a device capable of transmitting/receiving bluetooth 4.0 signal, such as an iPhone or a Android phone. The BleMini is manufactured by RedBearLab and it comes disassembled.

Don't worry, despite the fact in the package there are many components, we will need just one: the headers. Actually we are not going to use the headers that comes bundled with the BleMini, because they are straight.

Since we want to be able to place the BleMini vertically on the breadboard, we are going to need the break away headers with right angle.

Take the break away headers and cut a piece with 6 pins. Then insert the headers like in the pictures and solder it on the board. Be careful to solder on the side of the BleMini without circuits (first image). Refer to the pictures I posted to better understand the correct placement of the headers.

Once you finished soldering the headers on the BleMini board you are ready to use it, but first let's verify that the firmware is updated.

Before continuing I just want to point out that you should never connect USB power and Vin power at the same time. Doing so will damage the board.

You need a Microsoft Windows PC and an USB cable with micro USB connector for the upgrade process. Unfortunately you can't use OSX or Linux for the upgrade process. Download from here the Biscuit-UART binary file. Press and hold the push button on BLE Mini while connecting it to your PC, it should show a mass storage drive (e.g. E:). After that you can release the push button. Inside the mass storage device delete only the existing .bin file (e.g. Biscuit.bin) and copy the new firmware into the drive.

It could be possible that the BleMini is factory blank, in which case you will not find any .bin file inside the mass storage drive. Then, what you have to do is to ignore the step of deleting the file (because there isn't a file) and copy the new firmware inside the mass storage drive.

If everything goes well the green led on the BleMini will light up.

Connecting BleMini to the Adafruit Motor Shield

Now we are ready to connect the BleMini to the Adafruit Motor Shield. Remember to remove the USB power before proceeding.

First of all insert the BleMini in the breadboard with the circuits facing the Arduino and the motor shield. Then, using two jumper wires like the blue ones shown in the picture, connect Vin pin from the BleMini to the (+) in the breadboard, and Gnd from the BleMini to the (-) in the breadboard.

We can now connect the digital I/O pins 0 and 1 on the Adafruit Motor Shield, respectively to the TX and RX pins of the BleMini. To do so you just have to insert the cables that you soldered on the digital I/O pins of the motor shield into the breadboard. The picture below may help you to understand the connections. Note that red wires are crossing each other, so that 0 is connected to TX and 1 is connected to RX in the BleMini. The connection is done like that because 0 is the default receiving (RX) pin of Arduino, so BleMini should transmit (TX) on pin 0 because this is the digital I/O port dedicated to receive the signal. On the other hand 1 is the default transmitting (TX) pin of Arduino, so BleMini should be connected to it through it's receiving pin (RX).

Now we need to bring power to the breadboard. Connect the 5v (yellow wire) coming from the MotorShield to the (+) in the breadboard, and connect the Gnd (green wire) coming from the motorshield to the (-) in the breadboard.

Installing IR sensors in the robot (Optional)

As I already told you before, you can install 2 or more IR sensors in the robot. I will now show you how to install 2 IR sensors and connect them respectively to the analog pin 0 and 3 of the Arduino.

First thing you have to do is to find a way to fix the sensors to the chassis. Believe it or not I was not able to find any angle brackets on the italian stores. So I fixed the sensors using two pieces of wood. Anyway you can use the angle brackets on the Sparkfun website, and the result will be cleaner.

Besides the way you attached them to the chassis, you will need to solder the yellow cables of the IR sensors to the cables coming from the Analog Input pins 0 and 3, that you previously soldered on the Adafruit Motor Shield. You should solder the yellow cable of the left sensor to the green cable coming from Analog In 0, and the yellow cable of the right sensor to the green cable coming from Analog In 3.

The black part in between the cables is just an insulator I used to protect the soldering. It's a insulation plastic tube that shrinks with heat of fire. Link

If you prefer you can avoid soldering and use the breadboard to connect the cables.

The sensors must be attached to a power source. We can use the breadboard for that. Connect them to the breadboard (+) and (-) like in the pictures. Be careful to connect the red cables on the (+) and the black cables on the (-).

Then connect the two sides of the breadboard so that the power coming from arduino reaches the sensors.

Loading the software on Arduino

Now we are finally ready to load the software on Arduino. Before attaching the Arduino to USB be sure to disconnect the breadboard from power. You must disconnect from the breadboard the wire coming from the Adafruit Motor Shield +5v pin, so that the BleMini can't conflict during the upload phase of the code. Doing so, you can leave the motor shield stacked with Arduino without any problem.

Before loading the code you need to install the BleMini and the Adafruit Motor shield libraries.

Read the quick start for the BleMini and download the file marked as ReadBearLab Library. Then unzip the file and copy the "RBL_BLEMini" subfolder located in BLEMini/Arduino/libraries to Arduino's libraries folder.

As regard Adafruit libraries follow the official guide on the Adafruit website.

Now attach the Arduino to the computer using USB and load the following program using the Arduino IDE.

Get the code on Github.

#include "ble_mini.h" 
#include "Wire.h"
#include "Adafruit_MotorShield.h"
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *leftWheelMotor = AFMS.getMotor(3);
// You can also make another motor on port M2
Adafruit_DCMotor *rightWheelMotor = AFMS.getMotor(4);

/* Uncomment if you are using the 2 IR Sensors
int leftSensorPin = 0;
int rightSensorPin = 3;

int leftSensorValue = 0;
int rightSensorValue = 0;
int sendSensorData = 0;
*/

void setup() {
  BLEMini_begin(57600);

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
}

void loop() {

  while ( BLEMini_available() == 3 )
  {
    // read out command and data
    byte data0 = BLEMini_read();
    byte leftWheelVelocity = BLEMini_read(); //left wheel speed
    byte rightWheelVelocity = BLEMini_read(); //right wheel speed

    if (data0 == 0x71)  // Command is to control digital out pin
    {
      leftWheelMotor->setSpeed(leftWheelVelocity);
      rightWheelMotor->setSpeed(rightWheelVelocity);      
      leftWheelMotor->run(FORWARD);
      rightWheelMotor->run(FORWARD);
    }
    else if (data0 == 0x72)
    {
      leftWheelMotor->setSpeed(leftWheelVelocity);
      rightWheelMotor->setSpeed(rightWheelVelocity);
      leftWheelMotor->run(FORWARD);
      rightWheelMotor->run(BACKWARD);
    }
    else if (data0 == 0x73)
    {
      leftWheelMotor->setSpeed(leftWheelVelocity);
      rightWheelMotor->setSpeed(rightWheelVelocity);
      leftWheelMotor->run(BACKWARD);
      rightWheelMotor->run(BACKWARD);
    }
    else if (data0 == 0x74)
    {
      leftWheelMotor->setSpeed(leftWheelVelocity);
      rightWheelMotor->setSpeed(rightWheelVelocity);
      leftWheelMotor->run(BACKWARD);
      rightWheelMotor->run(FORWARD);
    }
    else if (data0 == 0x75)
    {
      rightWheelMotor->setSpeed(rightWheelVelocity);
      leftWheelMotor->run(RELEASE);
      rightWheelMotor->run(FORWARD);
    }
    else if (data0 == 0x76)
    {
      leftWheelMotor->run(RELEASE);
      rightWheelMotor->run(RELEASE);
    }
    else if (data0 == 0x77)
    {
      rightWheelMotor->setSpeed(rightWheelVelocity);
      leftWheelMotor->run(RELEASE);
      rightWheelMotor->run(BACKWARD);
    }
    else if (data0 == 0x78)
    {
      leftWheelMotor->setSpeed(leftWheelVelocity);
      leftWheelMotor->run(FORWARD);
      rightWheelMotor->run(RELEASE);
    }
    else if (data0 == 0x79)
    {
      leftWheelMotor->setSpeed(leftWheelVelocity);
      leftWheelMotor->run(BACKWARD);
      rightWheelMotor->run(RELEASE);
    }
  }
/* Uncomment if you are using the 2 IR Sensors
  if (sendSensorData == 1)
  {
    //Read the distance analog sensors
    leftSensorValue = analogRead(leftSensorPin);
    rightSensorValue = analogRead(rightSensorPin);

    //compute the left distance
    if ( leftSensorValue < 80 )
    {
      leftSensorValue = 80;
    }
    else if ( leftSensorValue > 500 )
    {
      leftSensorValue = 500;
    }
    float leftDistance = 4800.0/(((float)leftSensorValue) - 20.0);


    //compute the right distance
    if ( rightSensorValue < 80 )
    {
      rightSensorValue = 80;
    }
    else if ( rightSensorValue > 500 )
    {
      rightSensorValue = 500;
    }
    float rightDistance = 4800.0/(((float)rightSensorValue) - 20.0);

    // if the value is not -1 then it is between 10cm and 80cm
    // I have to translate this into bytes

    float normalizedLeftDistance = (leftDistance - 10.0)/70.0;
    byte leftDistanceByte = (byte) (min(255, (byte) (normalizedLeftDistance*255.0)));

    float normalizedRightDistance = (rightDistance - 10.0)/70.0;
    byte rightDistanceByte = (byte) (min(255, (byte) (normalizedRightDistance*255.0)));

    byte data[] = {0xBC, leftDistanceByte, rightDistanceByte};
    BLEMini_write_bytes(data, 3);

    sendSensorData = 0;
  }
  else
  {
    sendSensorData = 1;
  }
*/
  delay(100);
}
										

Once you load the program on Arduino, you can disconnect the USB power and connect to the breadboard (+) the cable coming from the +5v pin of the Adafruit Motor Shield. Now we are ready to connect the battery power supply. Follow the next paragraph to understand how to properly do that.

Connecting the battery power supplies and everything else.

If you reached this point of the tutorial you are almost done. In a couple of minutes you will be up and running, but first you have to give power supply to the robot.

Prepare the 6xAA battery pack. It must have a DC barrel jack plug like the one shown in the picture. If the battery pack does not include it you can solder the adapter to the black and red wires. Just remember to solder the red wire to the center of the jack and the black wire to the external part of the jack.

Now that you have the 6xAA battery holder ready, you must find room for it.

To place it as in the picture you must increase the Magician Chassis height by mounting 4 spacer extensions (the silver ones) on top of the original spacers (the gold ones).

Now we have everything for powering up Arduino.

The first thing to do is to remove the Vin jumper from the Adafruit Motor Shield (circled in green in the picture), because we are going to power up the motors with a separate source power: namely the 4xAA battery pack that we mounted at the beginning of the tutorial.

Connect the red and the black wires, coming from the 4xAA Battery pack, respectively on the (+) and (-) poles of the blue terminal placed on the Adafruit Motor Shield. Use a screwdriver to fix them in place.

Look carefully to the pictures and connect the cables. If everything is correct a green led indicating the motors are powered on should light up.

If it's not lighting up check that the battery holder is working and that batteries are correctly inserted on it. You can check the voltage between the red and black wire using a multimeter.

Let's finish the power supply stuff by connecting the 6xAA battery pack to the Arduino DC Jack, as shown in the picture. Remember that the jumper Vin must be removed before connecting two separate power sources to the Adafruit Motor Shield. Double check you removed the jumper before going on.

After you connect the 6xAA battery pack to Arduino the BleMini green led should turn on. If this does not happen check your connections. Check if the +5v and gnd cables are connected from the Adafruit Motor Shield to the breadboard and that all the power supply connections are done as described before.

Very well, all the building is finished now... not really? Just one step more. Put the robot on the road and follow the last build: the one that will give you the control of your robot using your iPhone.

Installing the app on your iPhone/iPad or iPod Touch

As I already mentioned before, you need to be a registered Apple developer to load the code in your device. You can get the source on GitHub.

First of all you have to turn on the robot by connecting the power supplies for the motors and arduino. Be sure the breadboard is connected with BleMini green led light up.

Then build and run the app. The user interface is very simple. You just tap on connect and wait for a connection to be estabilished with the BleMini. If everything is working it should connect in few seconds.

When the connection is estabilished the button title changes from "Connect" to "Disconnect".

You can control the robot by dragging the gas button (white square) up and down for forwards and backwards motions. To turn left or right just tilt your device left or right while dragging the gas button up or down. If you release the gas the robot stops. As simple as that!

If you installed the two IR sensors, you have to uncomment some lines of code that you can find very easily by inspecting the code. When you run the app, you will notice that the robot stops automatically whenever the obstacle is under a certain threshold of distance. You can try to modify the algorithm and elaborate different strategies. You can even mount 2 extra IR sensors to the free Analog In pins 1 and 2 and experiment with 4 IR sensors. If you need to mount more sensors you may consider to buy the Arduino Mega R3., which includes more analog IO pins.

Have fun!