Instructables

Step 10: Wiring and Completion!

Picture of Wiring and Completion!
20120211_212752.jpg
20120211_212843.jpg
20120211_213641.jpg
20120211_214329.jpg
20120211_214343.jpg
20120211_214348.jpg
20120211_215050.jpg
20120211_215111.jpg
20120211_215120.jpg
20120211_215126.jpg
Now, time to wire everything up, download the program, and test her out! 

I'll tell you how everything is connected one component at a time. Refer to the pictures to get a better idea of how it should look (if it's a rats nest of wires... you're on the right track). 

Motor battery: Hook up positive and negative to the positive and negative power rails on the far side of the breadboard (see picture 2).
Grounds: Take a jumper wire and attach the grounds of both power rails together (see picture 3). IMPORTANT, DON'T FORGET
5 volt power supply: Attach a jumper from 5V on the Arduino to the power rail on the breaboard closest to the Arduino.
Servo motors: Attach the left motor with the red and black wires going to the 5 volt power rail and ground respectively. Then, attach the white wire of the left servo to pin 10, the right one to pin 11, and the PING))) sensor panning servo to pin 6.
PING))) sensor: Either use the included cable and attach male-to-male jumpers to that, or use separate male-to-female jumpers to make the following connections: connect the pin on the PING))) that says 5V to the 5 volt rail... connect the pin that is labelled GND to either ground rail... and finally connect the one labelled "SIG" to pin 7 on the Arduino. 

I think that's everything... well, if it doesn't work you can yell at me in the comments because I probably forgot something. The wiring will look like a total rats nest, if you want it to look a little neater you can use male to male header pins like the setup I used in the video.
UPDATE: I have made a mistake, thanks to faroos7 for pointing it out! Also, in the code you'll notice a variable named irPin, which is set to 0. That's part of another project, and you can feel free to delete that variable. 

To get her going, download the attached Arduino program, run it, and it should have behavior similar to that in the video at the beginning of this Instructable. If it works, congratulations, you're done! If not, tell me the problem in the comments and I'll do my best to help you. 

If you want to do more experiments with Arduino on the breadboard, check out sciguy14's Arduino tutorials on Youtube! They're very well done, and they're how I learned originally. http://www.youtube.com/playlist?list=PLA567CE235D39FA84&feature=plcp

I hope my Instructable helped you, this is my first one and so I appreciate any feedback you can give. Please vote for me in the Arduino contest as well! Bye!
 
Remove these adsRemove these ads by Signing Up
Jonny1249 months ago
Hi love the idea and clear instuctions but just to be clear could you send a revised vertion of the code to me?
Thanks so much
John
Hello all
I was thinking about making this awesome robot. But after reading the whole article and the code i got confused with the operation of the two servos that drive the wheels.
As far as i know, a contineous rotating servo has lost its control over the amount of degree to rotate. Then how can the wheel servo rotate 90º and 180º?
can you also put the center servo code
hey can you put the code in the comments because I'm having a hard time opening the code attachment
Sorry it took me so long to reply

/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
*/

#include <Servo.h> //include Servo library

const int RForward = 0;
const int RBackward = 180;
const int LForward = RBackward;
const int LBackward = RForward;
const int RNeutral = 90;
const int LNeutral = 90; //constants for motor speed
const int pingPin = 7;
const int irPin = 0;  //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor; 
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal

void setup()
{
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6); //attach motors to proper pins
  panMotor.write(90); //set PING))) pan to center
}

void loop()
{
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward);
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral);
    panMotor.write(0);
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
 
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed
  {
    leftMotor.write(LBackward);
    rightMotor.write(RForward); //turn left
    delay(500);
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn right
    delay(500);
  }
   else //if they are equally obstructed
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward); //turn 180 degrees
    delay(1000);
  }
}

long ping()
{
  // Send out PING))) signal pulse
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  //Get duration it takes to receive echo
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
 
  //Convert duration into distance
  return duration / 29 / 2;
}
hello good day.. im already starting to build the robot but i have a problem of my sensor because it has a 4 pins output the ( VCC , trig(T), echo(R), GND), whereas your sensor has only 3 pins,, how should i make it to work...?? please help me...hoping for your immediate reply..
thank you very much for your reply.. i will message if i will start doing the project, because im still waiting the components... can i ask you a question on how to send a message using an arduino and receive it using arduino also.. your reply helps a lot..
hello.. your project is awesome.i'm a beginner about arduino.. i want to try how it really works but i'm a little confuse how to connect the wires.. can you help me?? i already have the components.. do you have clear wiring diagram?? I'm looking forward for your immediate reply... please help me...
greentree892 years ago
Hey there i am back, i finally got all the parts and i have put my robot together, all that is left is to wire it up! :)
Just got a quick question, I am not entirely sure about the 5v power rail you mentioned.
So i got the 3 servo motors and the ping sensor that all need 5v.
should i be connecting them all to the battery 5v rail or the arduino 5v rail that you said to connect to the breadboard?
Thanks again !
greentree892 years ago
I found this: http://www.meanpc.com/2012/01/hc-sr04-ultrasonic-sensor-added-to.html
though i much prefer your code where the ping servo rotates to choose the best path.
I shall now try and combine both codes to suit my robot :)
generalgeek314 (author)  greentree892 years ago
Sorry, I made a slight mistake. As well as replacing int distanceFwd = ping();, you have to replace all uses of "ping()" with "ultrasonic.Ranging(CM)".
Hey this is great! thankyou very much.

I spent a while trying to combine the codes but i kept getting so many errors but after i tried what you sent me, it works perfectly.
Just wondering do i change the Ranging(CM) to an actual number like Ranging(15) ? just to be sure.

I have just ordered the right batteries and i must also get a new servo motor as for some reason it gets really hot but it no longer moves.

I will be sure to upload photo's and a vid to show you :)
Thanks again.
generalgeek314 (author)  greentree892 years ago
No problem, I'm happy to help. No, you don't change Ranging(CM) to an actual number, it means you want to get the value of the distance away something is in centimeters. You could do Ranging(IN) instead, which would give you the distance in inches. You could use that in my code if you wanted, as long as you changed what the variable dangerThresh is in the beginning to how far away you want something to be in inches when the robot stops and turns. I hope you get your parts soon!
generalgeek314 (author)  greentree892 years ago
Good luck! I have done so more research, and I think I can help you specifically.
In the beginning, with all the variables, you want to create your ultrasonic sensor. Declare it like this: Ultrasonic ultrasonic( trigPin, echoPin);
Of course, make trigPin and echoPin whatever pins you plugged them into on the Arduino. Now, this library is actually quite handy, so you can delete the whole ping() function I wrote. Find the code that looks like:
void ping()
{
some stuff in here
}
and delete all that. Then, where it says int distanceFwd = ping();, change it to say int distanceFwd = ultrasonic.Ranging(CM);

Good luck, and feel free to ask if you need any more help. I'd like to see your robot when you finish, so if you could post pics/a video of it online that would be nice :)
greentree892 years ago
Hey my ultrasonic sensor has 4 pins, the 5v, gnd, echo and trig.
How should i connect them and what should i add to the code to make it work?
Thanks :)