Introduction: 3D Printed Arduino Robot

Picture of 3D Printed Arduino Robot

This instructable will show you how to build a basic 3D printed autonomous robot for around $40.

This instructable is primarily written for my students at MakerLab NZ as a guide to finishing or repairing their robots that they built during the term. However there is enough detail here to build a robot from scratch.

Step 1: Parts List

Picture of Parts List

You will need

  • Access to a 3D printer. Or if you don't have access you can easily build the printed components out of wood, lego or other material.
  • Soldering iron
  • Hot glue gun

Parts

Step 2: Solder 2 Wires to the Wheel's Electric Motors

Picture of Solder 2 Wires to the Wheel's Electric Motors

Find 4 wires approximately 100mm long. I used an old bit of Cat6 cable wire.

Solder each wire to the copper connector on the electric motor.

I usually add a dab of hot glue afterward for a bit of extra strength and insulation.

Step 3: Print the Body and Assemble the Wheel.

Picture of Print the Body and Assemble the Wheel.

Download and print the 4 STL files.

Some assembly is then required

Front Wheel:

  • Insert the wheel into the wheel bracket and fasten with a 4mm nut and bolt
  • Insert the servo into the hole in the servo bracket and screw it in with the screws provided with your servo kit.
  • Attach a wing to the servo cog and attach this with the small screw, make sure it has freedom to spin a full 180 degrees from side to side, with 90 degrees on each side as shown in the images.
  • Either screw or hot glue the servo wing to the wheel bracket, again ensuring that the wheel faces forward in the middle of its range of motion.

Step 4: Assemble the Remaining Parts of the Body

Picture of Assemble the Remaining Parts of the Body
  • Glue the 2 wheels and motors to the back end of the underside of the robot body.
  • During MakerLab there was the option to not use a servo to steer the robot, if you built one of those bots and you do not have a front wheel with a servo, you will have to rely on the power wheels to turn the robot. This will work better if you glue the wheels more into the center of balance of the robot body, you will have to put some balancers or caster wheels at both ends to prevent it from tipping. But then it will turn well, if you put the wheels at the back it will have trouble turning, unless you can put a caster wheel or similar on the front.
  • Glue or screw the sonic sensor holder to the front of the robot
  • Insert the proto shield onto the Arduino
  • Glue or screw the motor controller and Arduino onto the robot as shown.
  • Attach the 9V battery holder to the back of the robot, in a place where the wire from the 9V can reach the power plug at the back of the Arduino. I just glued mine to the top of the breadboard near the back

Step 5: Wire It Up - Motor Controller

Picture of Wire It Up - Motor Controller

Wire up the Motor Controller

  • Poke the wires from the electric motors through the holes in the base and up toward the motor controller.
  • These 4 wires need to go into the 'OUT' connectors on the side of the motor controller - 2 on each side. At this stage don't worry about which one they connect to, but connect the left wheel to the left connectors and the right wheel to the right ones. You have a 50/50 chance of getting the forward/backward wiring right and we'll test that later on.
  • Hot glue the battery holder to the under side of the robot wherever you can fit it, space is a bit tight under there!
  • Poke the red and black wires through the small hole in the robot base.
  • On the front of the motor controller there is a bank of 3 connectors. Plug the red wire into the left most connector on the motor controller. If you have a different type then this should say either VCC, or +12V or similar.
  • Plug the black wire into the GND bank on the Arduino protoshield. If you are not using a proto shield you may want to run a wire from your Arduino GND to a row on the breadboard (color the row black with a marker pen), and then plug the black battery wire into that.
  • With a red marker pen draw a red line on a row of your breadboard as shown in the 4th image, this will remind you that this is the power rail for running your other devices.
  • From the 3rd Connector take a wire to the row on your breadboard that you just colored red. This connector should say 5V. It is a 5V regulated output power supply.

Wire the controller inputs to the Arduino

We now have to take 4 wires from the motor controller input line and plug them into the Arduino. We need to plug them into PWM (Pulse Width Modulation) pins on the Arduino, Read this for more information about how they work.

Anyway look closely at these pins on the motor controller this is the row of 4 male pins in a row by the 3 connectors you have already wired up. To connect to these you will need 4 female to male wires.

Look at the number on the IN pins, this writing is pretty small, maybe your eyes are better than mine but I needed a magnifying glass to read these :). These are numbered from 1 to 4. Plug the IN1 and IN2 into Arduino pins 6, 11.

Plug the pins IN3 and IN4 into pins 3 and 5.

The correct wiring I believe is:

IN1 -> 6

IN2 -> 11

IN3 -> 5

IN4 -> 3

However we will run a test program to confirm this and if anything is not working in the correct order you can easily switch it around by swapping the wires that connect to the IN rail. Depending on which way around you plugged your motor wires in these might have to be switched around.

Step 6: Wire It Up - Servo Front Wheel

Picture of Wire It Up - Servo Front Wheel

Servo

  • There are 3 wires from the servo. Usually red/brown/yellow. These are generally terminated with a female end. You will need 3 male-male wires to connect to this.
  • Red wire - Power. Plug this wire into the red line you drew on the breadboard, this will provide the power to your servo.
  • Black Wire - Ground. Plug this wire into a free slot on the GND bank on the proto shield
  • Yellow Wire - Control. Plug this wire into pin 10 on the Arduino.

Step 7: Wire It Up - Ultrasonic Senor

Picture of Wire It Up - Ultrasonic Senor

These sensors generally have 4 pins. If you have one that only has 3 pins then the code I have provided will not work for you, you will need to take the Arduino example code for sensors/ping and use that instead. Essentially you need to use the same pin to read/write rather than 2 separate pins. It's a fairly minor modification. But if you have the one pictured you will have 4 pins to connect.

  • Take a wire from the VCC and plug it into your red line on the breadboard.
  • Take a wire from the GND and plug it into one of your GND's on the Arduino proto shield
  • Take a wire from the TRIG and plug it into pin 7 on the Arduino
  • Take a wire from the ECHO and plug it into pin 8 on the Arduino

Step 8: Test It Out...

Picture of Test It Out...

We are now ready to start testing your robot.

  • Insert 4xAA batteries into the battery holder and see if the LED on the motor controller lights up. If so great, if not check your wiring, maybe the red wire from the battery is not secured properly, or the 2nd connector is not going to the Arduino GND, or the black wire from the battery pack is not going to GND. (unplug your batteries while you figure it out!!!)
  • Make sure no lights come on in the Arduino when you plug in the AA batteries. If lights on the Arduino come on you have some incorrect wiring and you need to check it before proceeding. Most likely a power output going into the Arduino's 5V pin.

Start coding....

If you do not yet have the Arduino development environment running then go to www.arduino.cc and download the latest version.

Install it

Download the sketch attached to this page and open it, then save it in your Arduino folder with some other name for your bot (e.g. myrobot.ino), as you will customize this file.

  • Plug your computer into your robot, select the correct port (Tools/Port) and upload the code to your robot.
  • Once uploaded go to tools/Serial Monitor and open the serial monitor
  • This will go through a series of tests on your robot.
    • wheels: First it will drive each wheel in turn in both directions. Notice on the serial monitor it will say which direction it is supposed to be driving in. If this does not match what you see you will need to switch the wires plugged into IN1/IN2 (left wheel) or IN3/IN4 (right wheel) around the other way. If the wrong wheels drive you may need to swap 1/2 for 3/4 etc, keep swapping these over until it matches. If you are completely messed up, it is sometimes easier to unplug all 4 wires and just put in one at a time until you get it right, then the next, until you have all 4 working.
    • Trouble shooting wheels: If not all wheels move in both directions you need to check your wiring. If a wheel is not working check your solder points on the electric motor and also where they connect to the motor controller. If you have a volt meter put this on the output terminals (OUTx and GND on Arduino) on the motor controller and see if voltage is arriving at that point (should be about 5-6V). If not then the issue is on the 'IN' wires and where these are plugged in to Arduino, check you got the correct pins 3,5,6,11 plugged in.
    • Steering wheel: The next test will move the front wheel all the way to the left, then the right, then center. It will then perform a small move to the left/right and then center again. If you see that the center is not perfectly centered you need to adjust a variable in the code. This just depends on how accurately you attached it.

const int WHEEL_CENTER = 85;

Find the above line in the sketch, it is on line 25. Each change here is 1 degree of movement, so if you think your robot wheel center is out by about 5 degrees change this by 5. Upload the modified sketch and see if that is better. It may take a few iterations to get this right the wheel should be straight.

  • Eyes: Each loop through the test, it will print out a distance, this is the distance to the nearest object as seen by the ultrasonic sensor, make sure this is about right by putting an object in front of the robot. If your distance=0 then the sensor is not connected properly, check your wiring again.

Once you are happy that the robot is working and you want to try it out, look for the following method in the code: (line 43)

void loop() {
   // comment this line in, to run in test mode
   runTestMode();   // comment this line in to run a drive mode with front servo wheel
  // runDriveMode();  // comment this line in to run with no front wheel
  // runDriveModeWithNoFrontWheel();
}

Comment out the runTestMode(); method and remove the comment on the runDriveMode(); method, so it looks as follows:

void loop() {   
   // comment this line into run in test mode
  // runTestMode();   // comment this line in to run a drive mode with front servo wheel
   runDriveMode();  // comment this line in to run with no front wheel
   //runDriveModeWithNoFrontWheel();
}


Upload the new code, plug in the 9V battery to the Arduino and set your robot free.

Yay! go robot....

Step 9: Code

Picture of Code

The driving code is currently pretty simple and I encourage you to work on this to make your robot more intelligent.

void runDriveMode(){
   // basic drive loop
   int distance = getDistanceToWall();
  Serial.println(distance);
   if (distance < 20)
   {
      turnRandom(2);
      moveBackward();  
      delay(random(1500));  
   }
   else if (distance < 50)
   {
      turnRandom(2);
   }
   else if (distance <80)
   {
      turnRandom(1);
   }
   else
   {
      myWheel.write(WHEEL_CENTER);
      moveForward();
   }
 
  delay(200);
}

So the robot checks the distance to the next object / wall. If this is less than 20cm it will go in reverse turning in a random direction for a random amount of time. If the distance is less than 80cm it will start to turn away and if the distance is greater than that it will just run in a straight line.

No front turning wheel.

If you built a robot without a turning front wheel then you need to enable the method runDriveModeWithNoFrontWheel() rather than the runDriveMode(); method. This is similar but will turn by moving the wheels at different speeds and directions. To be honest you may need to tweak this code as each robot will have a different center of gravity and will work and turn at different speeds.

Step 10: Notes, Trouble Shooting and Enhancements.

Picture of Notes, Trouble Shooting and Enhancements.

Your robot should now be up and running. Its now up to you to change the runDriveMode() method to make it doing something more intelligent.

Some enhancements to add:

  • Add a switch that connects to both the AA battery pack and 9V wires, so you can easily switch the robot on and off
  • You may want to add a delay in the code after it's powered up so that you have time to put it down once you switch it on.
  • Try and get it to drive through an obstacle course.
  • Add a robot claw or weapon -

Trouble shooting

  • If you find the front wheel is 'jittery' or the servo doesn't seem to work very well it may be that the batteries are getting a bit low. I have found that the regulated 5V supply is not great when you are using only a 6V power supply (4xAA), you can usually expect a drop of 1.2 volts across a regulator I believe. Probably even worse if you are using rechargeables which will only be 4.8 volts to start with. However you can take a wire from the VCC connector (or battery red wire directly) and put it directly into your servo power line. But only do this if you are using 4AA batteries, if you are using higher voltage lipo's or similar use the regulated supply and in that case it should work fine.
  • I also have said to use a separate 9V battery, however once mine went flat, I just plugged another wire into the battery output and into the VIN on the Arduino and that worked, this should be fine until your batteries start to lose power.

Let me know how you get on!

Here is a vid of mine in action.

Step 11: Wiring Diagram

Picture of Wiring Diagram

Here is the fritzing wiring diagram and image. First time using this program so not sure on what standards you are supposed to do for layout etc.. yet. But you should get the picture...

Comments

Slk_Stephane (author)2015-11-21

Can you help me make it? Please send me an email at slk91@hotmail.fr

happy crappy grandpappy (author)2015-10-21

do yeh have the wiring sequence pic??

Had a go at it. Its now on the last step.

DIY Hacks and How Tos (author)2015-10-17

Great robot design!