Late last year, I discovered the Arduino. The Arduino is an open-source physical computing platform based on a simple i/o board and a development environment that implements the Processing/Wiring language. The Processing/Wiring language is a simplified version of the C programming language that has extensions to access the underlying hardware. The Arduino can be used to develop stand-alone interactive objects such as robots, led displays or 3D printers like the MakerBot Replicator.
Since I write software for a living, I am extremely comfortable with the software aspect of the Arduino. However I was a little worried about my lack of electronic skills. Just to give you some background, at the beginning of this project I knew absolutely nothing about electronics. I never used a soldering iron or even held a multimeter. So its goes without saying that I had a lot to learn. I spent a lot of time reading blogs, watching YouTube videos and I even bought the book “Electronics for Dummies”. I typically try to avoid the Dummy books because I find the title to be self deprecating. However in this case, the title fit. Fortunately, after a couple of fun filled weeks, I learned how to solder, felt comfortable using a multimeter and started to understand the basic concepts. I eventually got to the point where I felt I could build something without electrocuting myself! After some brainstorming I decided to build a robot!
The first step in building a robot is to pick a chassis. If you are handy, you can build something from scratch or hack apart an existing toy. In my case, I decided to use a chassis that I found on eBay. The kit I purchased was actually the 2WD Mobile Platform from DFRobot. Buying a pre-built platform has some advantages. Mainly because the kit comes with 2 motors, mounting hardware, wheels, hookup wire and a platform large enough to attach an Arduino, some shields and a small breadboard. The kit also comes with a toggle switch and a six AA battery holder. If you are a beginner like me, then I would recommend buying a kit because it will save you the time of trying to figure out which parts to buy. Finding parts on sites like Mouser or Digi-Key can be intimidating for beginners.

While I was waiting for my chassis to arrive, I purchased a motor shield kit from Adafruit. There are a variety of motor shields on the market but I settled on the Adafruit board for a few reasons. First of all it has excellent documentation. If you are a newbie like me, then getting a step by step instruction guide with pictures is very important. Second, I know that the Adafruit team stands behind their products. If I run into any issues I can rest assured that someone will help me out. Finally, the shield comes with a software library and a sample sketch. On top of all that, the hardware and software are open source! If you are going to build a robot then I would highly recommend buying this shield.

Once you assemble the motor shield you can simply stack it on top of an Arduino board. The male pins from the motor shield plug directly into the female headers of the Arduino board beneath it. The motor shield will occupy quite a few pins. In the end, you will be left with digital pins 2 and 13 and all of the analog pins. The analog pins are conveniently broken out in the bottom right hand corner. The board also gives you easy access to additional +5 volt and ground connections. In order to make prototyping easier and to increase the reusability of the shield, I soldered a 6 pin female header across each of the three rows in the bottom right hand corner of the PCB. I also ended up placing a 2 pin header across digital pins 0 and 1 which are used for TX (transmit) and RX (receive). The TX and RX pins are used for communication to the Arduino, for example when you upload a sketch to the Arduino or when you are sending serial commands from an external device. As a tip, if you have the RX and TX pins wired to a separate device for processing commands, you will need to disconnect these wires before you can upload any sketches.
For phase one of my project, I decided to make my robot autonomous. Autonomous means that the robot can drive around without anyone controlling it. In order for this to work, the robot needs to be able to detect and avoid obstacles. In other words, it needs to be able to see. The easiest way to implement this functionality is to use some sort of distance sensor. After a bit of research I decided to use the Parallax PING))) Ultrasonic sensor. There are cheaper distance sensors available but the PING sensor had sample code and I knew it would be easy to use.

The PING sensor was extremely easy to setup. Within minutes of using it, I had the sample sketch working. After I attached it to my robot chassis, I quickly realized that my robot lacked peripheral vision. If an object was not directly in front of the PING sensor the robot would crash into it. I solved this problem by purchasing a pan and tilt bracketfrom Sparkfun. The pan and tit bracket uses two servos which allows for 180 degrees of movement vertically and horizontally. I mounted the PING sensor on top of the bracket and hooked the servo wires to the motor shield. After a little bit of coding, my robot was able to take distance measurements in multiple directions!

Unfortunately, with the addition of the two servos I exceeded the amount of amps that six AA batteries could provide to the motor shield. Six AA batteries will provide you with approximately 9 volts at 1-1.5 amp hours (series gives you more volts but not more amp hours). As a result, I decided to invest in some Lipo batteries. The Lipo batteries are small and light enough for the robot to carry and provide sufficient amperage to drive 2 motors and 2 servos. After doing some price comparisons, I ended up buying my Lipo batteries from Hobby King. I bought one 1300mAh 2S 20C battery to run the Arduino and a 2200mAh 3S 20C to supply power to the motor shield. I also picked up some XT60 connectors to make it easier to connect and disconnect the batteries. Of course, Lipo batteries are useless without a charger so I threw one of those in my cart too. At the end of the day, I ended up spending more money than I wanted too but since the batteries are rechargeable they can be used on other projects.
Once I got the robot to work autonomously I quickly got bored of watching it drive aimlessly around the room. So I decided to add a remote control mode. As a starting point, I found an instructable which used a $10 Bluetooth adapter paired with an Android phone as a remote control. Once paired, the BlueTooth module can be used as a virtual serial port. Then it becomes a simple matter of sending and receiving serial commands in order to make the robot move. In the instructable, the author uses a Python script to send commands, but I decided to use a native Android app instead. So I downloaded the Eclipse IDE and started working my way through the “hello world” Android tutorials. To my surprise, Android development is easy to learn. There is ample documentation and plenty of community support. I also got lucky because the Android SDK includes a BlueTooth Chat application. I ended up using this application as the basis for my remote control app.
In order to switch the robot between autonomous and remote control mode I added a toggle switch to my robot. The toggle switch controls the power to the BlueTooth adapter. I use one of the analog pins on the Arduino to check if the Bluetooth module is active (HIGH) or inactive (LOW). If the BlueTooth module is active I process serial commands, otherwise I run the autonomous code.
The Arduino Code
Here is a brief summary of the Arduino code. The full source code is available on GitHub
The Adafruit motor library is really simple to use. Simply add a reference and use the methods to control the speed and direction of the motors. If you are using servos with the motor shield you can leverage the existing Servo library that comes with the Arduino IDE. Here are some examples of the methods I used to control the motors.
void brake()
{
leftMotor.run(RELEASE);
rightMotor.run(RELEASE);
}
void moveForward( int duration = 110 )
{
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
delay(duration);
}
void moveBackward( int duration = 1000 )
{
leftMotor.run(BACKWARD);
rightMotor.run(BACKWARD);
delay(duration); //amount of time it takes to back up
}
void turnLeft( int duration = 480)
{
leftMotor.run(FORWARD);
rightMotor.run(BACKWARD); //turns off the motor
delay(duration);
}
void turnRight( int duration = 480 )
{
leftMotor.run(BACKWARD);//turns off the motor
rightMotor.run(FORWARD);
delay(duration);
}
For processing serial commands, I leveraged the SerialCommand library. The library allows you to map incoming commands with methods in your Arduino sketch. For example, if you send the command “F 1000” to the Arduino my code will run the forwardCommand. The library also gives you easy access to the arguments sent with the command. For example the command “F 1000” has two parts. The command is “F” for “Forward” and the argument is 1000. In my case, this means move forward for 1000 milliseconds or 1 second. Here is the code:
void setupSerial()
{
Serial.begin(9600);
sCmd.setTerm(';');
sCmd.addCommand("F", forwardCommand );
sCmd.addCommand("f", forwardCommand );
sCmd.addCommand("B", backwardCommand );
sCmd.addCommand("b", backwardCommand );
sCmd.addCommand("L", turnLeftCommand );
sCmd.addCommand("l", turnLeftCommand );
sCmd.addCommand("R", turnRightCommand );
sCmd.addCommand("r", turnRightCommand );
sCmd.addCommand("?", showCommands );
sCmd.setDefaultHandler( unrecognized );
}
void showCommands()
{
Serial.println( "Hello Master. I am awaiting your commands." );
Serial.println( "------------------------------------------" );
Serial.println( "Commands must be terminated with a semi-colon:" );
Serial.println( "\tMove (F)orward - F <# of steps>");
Serial.println( "\tMove (B)ackward - B <# of steps>");
Serial.println( "\tTurn (R)ight - R <value>");
Serial.println( "\tTurn (L)eft - L <value>");
}
void unrecognized(const char *command)
{
Serial.println("Huh?");
}
Here is the code that uses conditional logic to determine whether the robot is in autonomous or remote control mode:
void loop()
{
//check if we are using the bluetooth module, if so listen for commands only
if( digitalRead(btPin) == HIGH ) {
sCmd.readSerial();
return;
}
//do autononmous functions ....
}
The Android App – Remote Control
Here is a brief summary of the Android code. The full source code is available on GitHub.

The UI is pretty simple. It’s basically just a joystick, a few labels and a connection dialog which allows you to choose which BlueTooth device to connect to. For the joystick I used the Mobile Anarchy Widgets. At this point, my app still needs quite a bit of work. However, the basic concept is proven and its just a matter of adding some trim and polish.
Here is the chunk of code that creates the virtual serial connection:
String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
String deviceName = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
// Get the BLuetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
txtHeader.setText("connected to device: " + deviceName );
outputStream = socket.getOutputStream();
//InputStream inputStream = socket.getInputStream();
} catch (Exception e) {
txtHeader.setText("connection failed: " + e.getMessage() );
}
When the joystick is moved, a event handler is called. I then look at the location of the joystick and send the appropriate command to the Arduino. I also added a little bit of code to control how often a command is sent. If you send too many commands at once you can overflow the serial port and the app will crash.
private JoystickMovedListener _listener = new JoystickMovedListener() {
Date lastSent = new Date();
@Override
public void OnMoved(int pan, int tilt) {
txtX.setText(Integer.toString(pan));
txtY.setText(Integer.toString(tilt));
if( socket == null || outputStream == null ) return;
//only send a message once a second
long diff = new Date().getTime() - lastSent.getTime();
if( diff < 500 ) return;
lastSent = new Date();
String command = getCommand( pan, tilt );
if( command.length() == 0 ) return;
byte[] bytes = command.getBytes();
try {
outputStream.write(bytes);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getCommand( int x, int y )
{
if( ( x >= -3 && x <= 3 ) && y > 3 )
return "B 1000;";
if( ( x >= -3 && x <= 3 ) && y < -3 )
return "F 1000;";
if( ( y >= -3 && y <= 3 ) && x < -3 )
return "L 1000;";
if( ( y >= -3 && y <= 3 ) && x > 3 )
return "R 1000;";
return "";
}
@Override
public void OnReleased() {
txtX.setText("released");
txtY.setText("released");
}
public void OnReturnedToCenter() {
txtX.setText("stopped");
txtY.setText("stopped");
};
Introducing “Robie” the Robot
Here is a video of “Robie” the Robot (my kids named him) running in autonomous mode. This video was taken prior to adding the remote control capabilities.
Conclusion
One of the best things about building this robot was that my kids are now interested in electronics. They love to ask me questions about how the robot works and they are extremely curious about the projects that I have sitting on my workbench. For years I have tried to get them interested in engineering but I could never find the right project to motivate them to learn. However, the Arduino seems to have a special appeal towards makers and even children. As proof, here is a picture that my daughter Noelle drew of her and I working on “Robie”.

Since I started working with the Arduino I have really gained confidence in my ability as a maker. In the past, when something electronic would break around the house I would either throw it away or send it out for repair. Nowadays, I attempt to fix things myself. Once you consider yourself a maker, you start to think differently. After all, “If You Can’t Open It, You Don’t Own It”