UG 106: Praxis I Undergraduate Program Handout:“Ultrasonic distance-sensor”
September 2012 Semester Asian Insitute of Technology Instructors: Waqar Shahid, Matthew N. Dailey
“Working with ultrasonic sensor using Arduino board”
Introduction: This lab will help you learn the basics of ultrasonic distance sensors and its usage. The task is to be able to measure the distance using Seedstudio ultrasonic range measurement module with the help of Arduino board. Ultrasonic sensors are proximity sensors that are able to measure distance of objects, within the specified range and without any physical contact. “ Ultrasonic sensors work on a principle similar to sonar which evaluate attributes of a target by interpreting the echoes from sound waves respectively. Ultrasonic sensors generate high frequency sound waves and evaluate the echo which is received back by the sensor. The time interval between the sent signal and received signal is determined to measure the distance from an object.” The Ultrasonic range measurement module from Seedstudio is a plug and play, industrial performance, non-contact proximity sensor with digital output. The module have 3 pins i.e, VIN, GND, and, signal. VIN and GND are connected to power the module. Signal pin works as in I/O pin to trigger the signal for transmitting the ultrasonic pulse and outputs the electric counter part of the received signal. An ultrasonic pulse is sent at time 0. The pulse is reflected by the object. The sensor receives the signal back. It converts it into an electric signal and output to signal pin. When the echoed signal is faded away, the next pulse can be sent again. The time period between the two pulses should be no less than 50ms. If a 10s width trigger pulse is sent to the signal pin, the Ultrasonic module will output eight 40kHz ultrasonic signal and detect the echoed signal back. The distance of the object is proportional to the echo pulse width and can be calculated as mentioned in the code. If no obstacle is detected, the output pin will give a 38ms high level signal.
Arduino library functions The new library functions used are defined below: delayMicroseconds() : This function will pauses the program for the amount of time specified as parameter in µ secounds. In the current version of the arduino library the largest value that will produce an accurate delay is 16383. For producing a delay in milliseconds you can use the function delay(). pulseIn() or pulseIn(pin, value, timeout) : This function reads a pulse on a pin. If the incoming pulse is HIGH, the function start the timer, waits for the pin to go LOW and then stop timer. It then returns the length of the pulse in µ seconds. It returns 0 if no pulse starts within a specified time out. It works on pulses from 10 microseconds to 3 minutes in length.
Instructions Implement the circuit as shown in the diagram of Figure - 1, 2. Be careful while using the proximity sensor, read the data-sheet attached in Annex-A before plugging in the sensor for pin configuration. Connect the signal pin of sensor with digital pin 7, VCC to 5V , and GND pin of sensor to GND of the arduino board.
1
Figure 1: fig:Diagram generated via Fritzing software
Figure 2: fig:Schematics generated via Fritzing software Implement the Code-1, compile, download and see the output on the serial port monitor. Place a white paper in front of the distance sensor and see how the reading changes. Find the maximum range that the sensor can accurately measure.
2
Task to be completed before the next lab Problem statement: Program your robot to move in a perfect square. Note the corners of the square are at right angles. The length of each side of the square is assumed to be 50cm. Make use of your proximity sensor and place some white paper on the edge of each side of the square to control the movement. Does your robot comes back to the same location where it started?
Code Code-1: Arduino Example program for Ultra Sonic range measurement module /* Ultrasonic Sensor * This sketch reads a 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 sensor module attached to +5V * GND connection of the sensor module attached to ground * SIG connection of the sensor module attached to digital pin 7 Copied from "http://www.arduino.cc/en/Tutorial/Ping" created 3 Nov 2008 by David A. Mellis modified 30 Aug 2011 by Tom Igoe edited 30 Nov 2012 by Waqar Shahid Qureshi This example code is in the public domain. */ // this constant won't change. // of the sensor's output: const int sensorPin = 7;
It's the pin number
void setup() { // initialize serial communication: Serial.begin(9600); } void loop() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration, inches, cm; // The module is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(sensorPin, OUTPUT); digitalWrite(sensorPin, LOW); delayMicroseconds(2); digitalWrite(sensorPin, HIGH); delayMicroseconds(10); digitalWrite(sensorPin, LOW); // The same pin is used to read the signal from the module: 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(sensorPin, INPUT); duration = pulseIn(sensorPin, HIGH); // convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); delay(100); } long { // // // //
microsecondsToInches(long microseconds) There are 73.746 microseconds per inch (i.e. sound travels at 1130 feet per second). This gives the distance travelled by the ping, outbound and return, so we divide by 2 to get the distance of the obstacle. See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
// You can also use the Formula given in Seedstudio datasheet // i.e. return microseconds/148; return microseconds / 74 / 2; } 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.
3
// You can also use the Formula given in Seedstudio datasheet // i.e. return microseconds/58; return microseconds / 29 / 2; }
References 1. http://en.wikipedia.org/wiki/Proximity_sensor. 2. http://profesores.fi-b.unam.mx/m3615m/datasheet-sen136b5b.pdf. 3. http://www.seeedstudio.com/wiki/Ultra_Sonic_range_measurement_module. 4. http://arduino.cc. 5. “Arduino Programming Notebook” by - by Brian W. Evans. 6. Beginning Android ADK with Arduino by Mario Bohmer 7. http://arduino.cc/en/uploads/Main/arduino_Uno_Rev3-02-TH.zip. 8. http://arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf. 9. http://arduino.cc/en/Main/ArduinoBoardUno. 10. Computational Principles of Mobile Robotics by from Dudek and Jenkin.
Anex-A Datasheet of Seedstudio ultrasonic distance sensor module
4
Figure 3: Data-sheet
5
Figure 4: Data-sheet
6