TweetI have an overall goal to use the Arduino to build some kind of really simple robot. Eventually I hope to do cool things like remote controlling it and maybe control it through a C# application (and eventually over the internet would be awesome!)

This weekend I put together my robot chassis. I got the Emgreat Motor Robot Car Chassis Kit. Here is how it looked after I assembled it:

The Build

I also got an Adafruit Motor/Stepper/Servo Shield for Arduino v2.3 Kit. I chose to solder on a stackable header kit. I guess you can just use the standard header pins included with the motor shield, but I thought I should use the stackable headers just in case I want to put another shield on top of it at some point.

Then I simply:

Pushed the motor shield in place on top of the Arduino

Attached the chassis battery back to the motor shield power in

Soldered the wires to the chassis motors (included in the chassis kit)

Connected the motor wires to the motor shield (I used M2 and M3 inputs because they happened to be closest to the motor wires)

Removed the VIN jumper from the motor shield. This is to make it use my external batteries for the motor, rather than drawing power from the Arduino

Here is how it looked all connected:

The Code

I downloaded and installed the Adafruit Motor Shield V2 Library and then used the included DCMotorTest test sketch as a basis to write the following code:

#include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_PWMServoDriver.h" Adafruit_MotorShield AFMS = Adafruit_MotorShield(); Adafruit_DCMotor *leftWheelMotor = AFMS.getMotor(2); Adafruit_DCMotor *rightWheelMotor = AFMS.getMotor(3); void setup() { AFMS.begin(); // create with the default frequency 1.6KHz } void loop() { leftWheelMotor->setSpeed(50); rightWheelMotor->setSpeed(50); leftWheelMotor->run(FORWARD); rightWheelMotor->run(FORWARD); delay(5000); leftWheelMotor->run(RELEASE); rightWheelMotor->run(RELEASE); delay(5000); }

In this example, I just get references to motors 2 and 3 (as I connected the left motor to M2 and the right to M3 of the motor shield) and set the speeds of both to 50 (an arbitrary number that wasn’t too fast so I could catch the robot if/when things went bad!!).

Then I make both motors run forward, leave it for 5 seconds (to give it a chance to move) then release the motors (stop) and again wait for five seconds.

The only problem I encountered was the first time I ran the motors, the left wheel rotated forwards but the right wheel went reverse. I “fixed” this by switching the wires to the right wheel (switched the places of the black and red wires into the M3 input).

Here’s a video of it running:

Arduino Robot

Pretty simple stuff, but a huge breakthrough in my Arduino learning and big step towards my robot!

Hopefully as I learn new things I can build on this … next up I hope to incorporate the stuff I learnt playing with the ranging sensor to stop this little guy running into walls!

I couldn’t have achieved any of this without the tutorial over at Adafruit Motor Shield V2.