Monday, 12 December 2011

Ultrasonics



As the final goal is a robot, collision detection is a must. There are various types of detection from IR (Infra-red) range finding, ultrasonic, laser and actual collision detection through use of a push button of something similar. For the purposes of this post I'll be documenting my findings with ultrasonics, however I do intend to look into IR and maybe even laser at some point.
There are many ultrasonic modules on the market, ranging from £10 - £100+. Interfaces range from TTL, I2C and Serial. However by far the easiest to interface with Arduino is the PING sensor. The PING sensor itself is relatively cheap and accurate enough for a basic collision avoidance robot, however SeeedStudio do an even cheaper model (about 2/3 the price of the PING). This is a simple 3 wire sensor, 5V, GND and SNG (which can be connected to any digital pin on the Arduino).
Using the PING tutorial and the inbuilt LCD library that comes with the Arduino IDE, I created a quick demo. All wiring drawings can be found in the respective tutorials.

The sensor basically works by sending a High pulse on the digital pin for 2ms, then recording the time taken (in microseconds) for the same pin the receive the pulse back. The time taken, divided by 2 can then be used to calculate the distance. The PING documentation states that sound travels at 340 m/s in air or 29 microseconds / cm.
The sensor was pretty accurate, easily to within 1cm using my crude measuring techniques in the video.
Below is the code used in the demo:
/* Ping))) Sensor
  
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
    * +V connection of the PING))) attached to +5V
    * GND connection of the PING))) attached to ground
    * SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe
 
   This example code is in the public domain.

 */


#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int pingPin = 7;

void setup() {
  
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);
  
  lcd.setCursor(0,0);
  lcd.print("Range in cm:");
  lcd.setCursor(0, 1);
  lcd.print(cm);
  
  
  delay(1000);
  lcd.clear();
}


long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

No comments:

Post a Comment