Basic Arduino Robot

The Arduino is a great way to get started in the world of DIY electronics and robotics. This project pulls together a few key items to interface with an Arduino. This robot is controlled through the joystick of a Wii Nunchuck (although you could just as easily use the accelerometer) and has a 2 wheel drivetrain similar to the design used by iRobot’s Roomba. Here are all the supplies needed for this project:

Supplies

  • 1 Arduino
  • 1 breadboard
  • 2 full rotation servos
  • 2 servo wheels
  • Breakaway headers
  • 1 Ball Caster
  • 1 Wii Nunchuck
  • 1 WiiChuck adapter
  • 1 9v battery with connector (or other battery pack)
  • 4 AA batteries and holder
  • Blank CD
  • 2 sided tape

Construction

The blank CD provides a frame for the robot. The servos are attached to the top with 2 sided foam tape and a ball caster is taped to the bottom of the CD in the front. This is called a two wheel drivetrain and is controlled by the speed each wheel rotates at. If both wheels spin at the same speed the robot goes forward, if one spins faster than the other then the robot goes in the direction of the slower wheel.

Wiring the robot is the next step. An easy way to keep this organized and allow for future expansion is to setup the wiring on a small breadboard. Servo’s typically come with a 3pin female connector; to be able to plug these into the breadboard you need breakaway headers. The Wiichuck is plugged directly into the analog pins of the Arduino.

This robot requires different power sources for the Arduino and for the servos. Servos draw a high current so if only one battery pack is used the Arduino would lose power when the wheels started to spin. One thing to remember when using multiple power sources is to tie them all to a common ground.

Here is a diagram of the setup:

Once everything is connected, all you need to do is stack the components onto the platform and connect the power. You can also wrap a rubber band around the breadboard, Arduino and battery pack to prevent it from slipping

Sketch

Here is the code to make the whole thing run:

// Simple Robot controlled with Wii Remote
// ForkRobotics
//

#include <Servo.h>
#include <Wire.h>
#include "nunchuck_funcs.h"

//Create Servo Object for Left Wheel
Servo RightWheel;
Servo LeftWheel;

int loop_cnt=0;
int speed,direction,LeftRotate,RightRotate;

void setup()
{
  RightWheel.attach(5);  // attaches the Right Wheel to pin 5
  LeftWheel.attach(6);  // attaches the Left Wheel to pin 6

  // initilization for the Wiichuck
  nunchuck_setpowerpins();
  nunchuck_init();
}

void loop()
{
  if( loop_cnt > 100 ) { // every 100 msecs get new data
    loop_cnt = 0;

    nunchuck_get_data();
    speed  = nunchuck_joyy(); // reads joystick y axis (range of 38-232)
    direction  = nunchuck_joyx(); // reads joystick x axis (range of 25-223)

    // Converts the joystick input to the servo output range
    speed = map(speed, 38, 232,0,180);
    direction = map(direction, 25, 223,-90,90);

    // The joystick floats a little, this reduces jitters when it's near center
    if (speed >= 87 && speed <= 93) speed = 90;
    if (direction >= 87 && direction <= 93) direction = 90;

    // Translates the joystick reading to the rotation speed for each wheel
    RightRotate=speed-direction;
    LeftRotate=(180-speed)-direction;

    // Writes the rotation speed to the servos
    RightWheel.write(RightRotate);
    LeftWheel.write(LeftRotate);
  }
  loop_cnt++;
  delay(1);
}

If you try to upload or verify this code directly you will get this error:

Before you can compile and upload the code you need to put a copy of the nunchuck_funcs.h file from the WiiChuck Project http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available in the same folder as the sketch. Follow these steps:

  1. Save the sketch
  2. Close Arduino IDE
  3. Copy the nunchuck_funcs.h file into the sketch folder
  4. Reopen the sketch
  5. Upload

The result

The result is a basic robot that has direct control through the Wii remote.

I consider this a starting point for more complex projects. Adding bumpers or distance sensors would allow for the robot to do obstacle avoidance or a camera pointed through the hole of the CD could allow line tracing.

6 thoughts on “Basic Arduino Robot

  1. Pingback: Basic Arduino Robot | Arduino Focus | Scoop.it

  2. Pingback: Arduino Leonard Wii Nunchuck Mouse | Fork Robotics

  3. Running Arduino UNO with Arduino-1.0.3. Named above code ‘BasicArduinoRobot.ino’
    In ‘My Documents’;

    Arduino\
    Libraries\
    BasicArduinoRobot\
    examples\
    Nunchuck_funcs.h
    BasicArduinoRobot\
    BasicArduinoRobor.ino.

    I would Love to get this working; Keep getting compiling error;
    ‘Servo’ does not name a type
    Then below;
    In file included from BasicArduinoRobot.ino:7:
    C:\Program Files (x86)\arduino-1.0.3\libraries\BasicArduinoRobot_ForkRobotics/nunchuck_funcs.h: In function ‘void nunchuck_init()’:
    C:\Program Files (x86)\arduino-1.0.3\libraries\BasicArduinoRobot_ForkRobotics/nunchuck_funcs.h:40: error: ‘Wire’ was not declared in this scope
    C:\Program Files (x86)\arduino-1.0.3\libraries\BasicArduinoRobot_ForkRobotics/nunchuck_funcs.h: In function ‘void nunchuck_send_request()’:
    C:\Program Files (x86)\arduino-1.0.3\libraries\BasicArduinoRobot_ForkRobotics/nunchuck_funcs.h:56: error: ‘Wire’ was not declared in this scope
    C:\Program Files (x86)\arduino-1.0.3\libraries\BasicArduinoRobot_ForkRobotics/nunchuck_funcs.h: In function ‘int nunchuck_get_data()’:
    C:\Program Files (x86)\arduino-1.0.3\libraries\BasicArduinoRobot_ForkRobotics/nunchuck_funcs.h:78: error: ‘Wire’ was not declared in this scope
    BasicArduinoRobot.ino: At global scope:
    BasicArduinoRobot:10: error: ‘Servo’ does not name a type
    BasicArduinoRobot:11: error: ‘Servo’ does not name a type
    BasicArduinoRobot.ino: In function ‘void setup()’:
    BasicArduinoRobot:18: error: ‘RightWheel’ was not declared in this scope
    BasicArduinoRobot:19: error: ‘LeftWheel’ was not declared in this scope
    BasicArduinoRobot.ino: In function ‘void loop()’:
    BasicArduinoRobot:40: error: lvalue required as left operand of assignment
    BasicArduinoRobot:42: error: expected `)’ before ‘RightRotate’
    BasicArduinoRobot:46: error: ‘RightWheel’ was not declared in this scope
    BasicArduinoRobot:47: error: ‘LeftWheel’ was not declared in this scope

    • Thanks for catching that I took at look at the code and when I pasted it into WordPress it stripped 2 of the Include statements at the top. I went back and fixed the post. The lines in question are 5 and 6.

  4. Hi Justin,
    Still cannot get this code to run. When I load the file as is the IDE highlights the line that starts with “if (speed >= 87″, and responds with an error that states “lvalue required as left operand of assignment”. If I comment this line out the sketch compiles and downloads OK but The nunchuck doesn’t function. What I’m looking at are two continuous servos rotating in opposite directions with no reaction from the nunchuck
    I know the sketch is seeing all the #include files because if I comment out either of the Servo.h or Wire.h files I get the correct corresponding error. I know the nunchuck,funcs file is working because I get 5v between pins A2 and A3. I tried an original and a 3rd party Nunchuck and neither work.

    • Hey John, sorry again about that. It looks like another line was mangled. Grabbed a copy of it from my sketchbook, pasted it back to this post and then copied and pasted back to the Arduino IDE and it compiled for me. Thanks for your patience.

Leave a Reply