The Woody Bot gets collision avoidance with the Parallax Ping

Parallax Ping Ultrasonic Distance Sensor
The next logical step to any robot build is to figure out a control system of some sort. You could remote control it with a cell phone, I-pad or a radio remote. You could build it as a Line Following robot, or make it completely autonomous. I am eventually going for the latter. You could also use a combination of all these. For now we are going to add the first step in the autonomous process and add a basic collision avoidance system. We are starting by adding one Parallax Ping Ultrasonic Distance Sensor. We will probably be adding to this in the future and use multiple Pings or Infrared sensors to help us avoid objects.

Parallax Ping Ultrasonic Range Sensor 28015

What is it.
The Parallax Distance Sensor is an Ultrasonic device that sends out an Ultrasonic wave that bounces back to itself. The sensor measures the time it takes in milliseconds for the signal to return to the ping sensor. You can then take this number and convert it for use in your sketch. The ping has built in functions that convert the sensed data to Inches and Centimeters. You just have to tell it this in code. We will go over the code after we get everything assembled.

The version we are testing here is the 3 wire model sold by Parallax. I have a couple of the 4 wire Knockoff brand models on the shelf and will be comparing them to this model soon.

Parallax Ready Made Bracket
Mounting Bracket
The first obstacle to overcome is mounting your Ping in a proper place on your device. Being right up front most of the time you are putting your expensive little sensor at risk of being damage in a collision. So building a mount that protects it as much as possible is an essential part of adding this to your project.

I built my mount from scratch from a piece of black Lexan I had laying around. But if that is above your skill set, then fortunately for you there are several ready made options available for sale on the web. I will include a couple of links to them at the end of the article.



I started with a piece of lexan that I cut 2" wide x 3 1/2" Long. I heated it with a heat gun and bent it into a 90 degree piece. this allows for easy mounting.  I drilled 2 3/16" holes in the short edge to allow mounting to the bot. Using a 3/4" spade bit I drilled two holes 1" apart and 3/4" down from the top to accommodate the round sensor hoods.
Foam Spacers for Ping.

Next I put the sensor in place and marked the two small holes that are drilled for mounting it in the
corners. I drilled these with a 3/32" drill bit. My intention was to mount using screws or small bolts, but I couldn't find any on short notice that would fit. So I used small black Zip Ties instead. I placed small pieces of adhesive backed foam around the edges of the sensor holes to hold the sensor back from the bracket and allow air flow, then zip tied the sensor on the bracket. Just cinching it down enough to make it even across the mount.

NOTE:
Ping Mounted. 
It is a good Idea to attach your wires prior to mounting your Ping, since the labels are on the front side of the board. For your info though, on the 3 wire Ping from the back the wires are from left to right Signal, 5V+ then Ground. On the four wire models I have the wiring is also from left to right, Ground, Echo,Trig, and VCC.

Mounting
I screwed the bracket to the underside of the front edge of my bot.
Mounted on underside of Bot
this leaves the top area for easy mounting of some other device.
I used 3 male to female jumper leads that I tucked up under the Arduino and connected to the motorshield. I created a small breadboard on the analog inputs of the motorshield for easier connecting. Check out my other article for how to do that.

Connections
On the analog breadboard I connected the Ping Signal wire to pin A3 and the 5v+ and Grd to the pins right behind pin A1. This keeps it nice and neat. I originally had the Ping Connected to Pin A5 but I had to move it later when I added an LCD that uses I2C, as the pins for I2c are A4 and A5 and can't be changed easily that I could find. With it all connected it is time to move on to the code.

Code
I am still using the old Ping library at this moment because the NewPing library I found has all the docs written for the 4 wire sensor. I didn't take the time to see how to use with the 3 wire module. But when I do I will add notations for it. So go get the OLD Ping library for this tutorial. You can find it Here -->> Ping Library. Download it and add it to your libraries.

    Before we go any further let me give some advice. If you are coding for Arduino and your not     using or don't even know what Functions are, then stop and go learn how to use them. They make things so much easier as you add more and more things to your project. This way you can trouble shoot individual pieces without rewriting the whole sketch. If your like me and came from the old DOS, Cobol and Fortran days then a Gosub routine is familiar to you. That is basically what a function is. Callable routines that are reusable over and over in a sketch. This is written assuming your using functions.

Declarations - before void setup().
The first thing you need to do is include the Ping Library and tell the Arduino what ping version your using. I am using version 3 so that is what is in the code. Change it to match your model. Add these three lines at the beginning of your sketch before void setup().

#include <Ping.h> //include the ping library

#define THRESHOLD 6  //set a threshold for the ping to trigger from

Ping ping = Ping(A3);  //set model of ping
Function - add at bottom of sketch
Next we need to make a function for the ping to scan for objects.


double scan()
{
  ping.fire(); //fire the ping
  return ping.inches(); //return the ping result in inches
}
Actions- add to void loop()
Next we add the code to the void loop() to make the Arduino do something in response to the Ping data. In this case when or if the Arduino detects that an object is closer than the 6" that we set the THRESHOLD at it in our Declarations, it is going to make the Arduino run the turnLeft() function I have written in my sketch. Otherwise aka else it is going to call the goForward() function and drive straight ahead.



if (scan() <= THRESHOLD)    // if the Woody Bot gets too close...
    turnLeft(); // call the turn left function
  else
    goForward(); //call the go forward function

Here is a little video of this sketch in action.


 I have included the basic code to get a two wheeled bot navigating with a ping sensor. It is not fancy just a basic avoidance sketch.

//Code for the Woody Bot, with Ping Sensor,
//
//Kevin "Woody" Woodyard
//
//Check out http://woodystime.blogspot.com for more info and build guide.

#include <Ping.h>
#include <AFMotor.h>

#define THRESHOLD 6

Ping ping = Ping(A3);

//Set motors to right frequency to match your servo.
AF_DCMotor motor(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);

void setup()
{
  Serial.begin(9600);

  pinMode(A0, INPUT_PULLUP);

  motor.setSpeed(255);
  motor2.setSpeed(255);

}

void loop()
{
  if (scan() <= THRESHOLD)    // if the Woody Bot gets too close...
    turnLeft();
  else
    goForward();
 
}

double scan()
{
  ping.fire();
  return ping.inches();
}

void goForward()
{
  motor.run(FORWARD);
  motor2.run(FORWARD);

  delay(100);
}

void turnLeft()
{
  motor.run(BACKWARD);
  motor2.run(FORWARD);

  delay(300);
}


Comments