Step 10: Wiring and Completion!
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 ads by
Signing Up











































Visit Our Store »
Go Pro Today »




/*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;
}
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 !
Wire up the 3 servo motors to power from the separate battery. Motors use up a lot of current, more than the regulator on the Arduino provides, and they can't be powered directly from the 9V battery because 9 volts is too much for them. That's why you have to hook them up to the other battery.
As for the sensor, hook that up to 5V power from the Arduino. It needs the same steady voltage the Arduino is getting in order to give accurate results.
Hope I helped. If things are still unclear, feel free to ask and I'll do my best to fix whatever comes up.
What happens however is that it rotates in a circle with the right motor moving forward and the left moving backward.
when i put my hand in front of the sensor it moves backwards untill all is clear and then continues to move in rotation, also the sensor servo does not operate at all.
I made sure i got all the wires in the right place and also tried swapping the motor wires around but just got more odd movements
here is the code i am using:
/*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 //include Servo library
#include
Ultrasonic ultrasonic ( 4, 5);
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 irPin = 0; //Sharp infrared sensor pin
const int dangerThresh = 4; //threshold for obstacles (in inches)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;
Servo leftMotor;
Servo rightMotor; //declare motors
void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(6); //attach motors to proper pins
panMotor.write(90); //set PING))) pan to center
leftMotor.write(90);
rightMotor.write(90);
}
void loop()
{
int distanceFwd = ultrasonic.Ranging(INC);
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 = ultrasonic.Ranging(INC); //scan to the right
delay(500);
panMotor.write(180);
delay(700);
leftDistance = ultrasonic.Ranging(INC); //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);
}
}
const int LForward = RBackward;
const int LBackward = RForward;
To this:
const int LForward = 0;
const int LBackward = 180;
As for your sensor servo, I'm not sure what the problem is. Are you certain this servo works? Try using code from this tutorial to test it - http://arduino.cc/it/Tutorial/Sweep
I was reading up the reason for the odd behavior from all 3 servos and it turns out it was because the servos need to share the ground from both arduino and the battery, i must of missed that part on your tutorial, i just took a lead from from the arduino gnd and connected it to the breadbord gnd rail that you said to link from both sides.
now it responds perfectly! :)
I have now tested out the whole code as i was breaking it down to see how it responded.
everything seems fine except the panmotor still moves all the way to the right at the very start before it even detects an object.
i will do some more tests tomorrow to find out the problem.
i will order a new servo but in the mean time i have simplified the code for a fixed sensor so it simply turns left on detection of obstacles, i have also added a piezo and led to the code, i will make a video to show you my robot :)
it is very simple compared to your robot but for my first robot i am very proud :D
i also had to increase the speed of the right servo as it seems to turn left, i must of designed the balance of the chassis badly.
i also find that when it is heading towards obstacles at an angle it does not pick up on the obstacle and completely gets stuck, but i do have a spare sensor, perhaps i can use 2 on the robot to help this.
well theres plenty of ideas to upgrade my robot :)
I will make the video of my robot very soon, thankyou so much for all your help, you've been amazing and so very helpful :)
I hope you continue with this hobby, it's very fun and I'm incredibly happy to have helped you enter the world of robotics. I recommend you check out letsmakerobots.com, they have a great community of robot builders that give great advice and feedback, and also there are lots of robots there to get inspiration from.
Thankyou so much for all your encouragement and it is because of you i have built my first robot.
I built up a random code from the link you sent me, it works great!
And to add to that i combined the code with something else i found which solves a problem my robot had,
from my last post, simply when the robot came in at an obstacle from an angle it would not detect it and then try and turn too late and it must move forward to make a turn, but when it gets too close to an obstacle it moves backwards then turns.
I have tested the random turning which i really like but i noticed a problem i had not thought of before,
lets say it comes to a corner, and chooses to turn left, it will then immediately meet the other wall and by chance the random code makes it turn right it hit the previous wall again, i have watched it do this quite a few times, but its still quite fun to watch it like this as you said because you never know which turn it will take making things interesting, it always gets it's self out of areas sooner or later anyway hahah
I have bought a new servo for the sensor which should arrive shortly.
Video coming soon! :)
Thanks again!
here is my code if you are interested in seeing it at the moment:
#include //include Servo library
#include
Ultrasonic ultrasonic ( 4, 5);
const int numOfReadings = 1; // number of readings to take/ items in the array
int readings[numOfReadings]; // stores the distance readings in an array
int arrayIndex = 0; // arrayIndex of the current item in the array
int total = 0; // stores the cumlative total
int averageDistance = 0; // stores the average value
int echoPin = 5; // SRF05 echo pin (digital 2)
int initPin = 4; // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm
int pinSpeaker = 13;
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
int leftDistance, rightDistance; //distances on either side
Servo leftMotor;
Servo rightMotor; //declare motors
int randNumberSpeed; // variable to store the random speed value
int randNumberDir; // variable to store the random direction value
int x = randNumberDir;
void setup()
{
Serial.begin(9600);
rightMotor.attach(11);
leftMotor.attach(8);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(pinSpeaker, OUTPUT);
pinMode(4, OUTPUT); // trig init
pinMode(5, INPUT); //echo
// create array loop to iterate over every item in the array
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;
randomSeed(millis());
}
}
void loop() {
digitalWrite(initPin, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(initPin, LOW); // stop sending the pulse
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // Distance = pulse time / 58 to convert to cm.
total= total - readings[arrayIndex]; // subtract the last distance
readings[arrayIndex] = distance; // add distance reading to array
total= total + readings[arrayIndex]; // add the reading to the total
arrayIndex = arrayIndex + 1; // go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
}
averageDistance = total / numOfReadings; // calculate the average distance
delay(10);
// Check the average distance and move accordingly
if (averageDistance <= 10) {
// go backwards
leftMotor.writeMicroseconds(1000);
rightMotor.writeMicroseconds(2000);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
{
playTone(750, 500);
delay(350);
}
}
if (averageDistance <= 25 && averageDistance > 10 ) {
//turn left
randomSeed(millis());
randNumberDir = random(300); {
if (randNumberDir > 150){
rightMotor.writeMicroseconds(1000);
leftMotor.writeMicroseconds(1500);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
{
playTone(750, 300);
delay(750);
}
}
else if (randNumberDir < 150){
rightMotor.writeMicroseconds(1500);
leftMotor.writeMicroseconds(2000);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
{
playTone(750, 400);
delay(750);
}
}
}
}
if (averageDistance > 25) {
// go forward
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(1000);
leftMotor.writeMicroseconds(2000);
rightMotor.writeMicroseconds(1100);
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
It simply continues to move in rotation, it seems to be the left motor that is going the wrong way no matter where ever i set it to 0, 180 or 90
And of course it does not stop but rather instead moves backwards when there is an obstacle.
I also tried that code for the sensor servo, it moves it all the way to the right but then nothing else happens, same for the robot code, nothing moves it to the left again.
Its ok if you cant figure this one out, you've been an amazing help with all my questions, i will continue to try and work on it.
Thanks again.
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 :)
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.
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 :)
How should i connect them and what should i add to the code to make it work?
Thanks :)
I haven't used a sensor like that myself, so I can't give any personal help. However, this link has some useful information on how to use it with Arduino: http://letsmakerobots.com/node/30209. I'm not sure what your level of experience is, because unfortunately the link I provided doesn't go into much detail about what to do. If you need more help, just ask and I'll do what I can.
I am not sure what i should take out of your code and replace.