Ultrasonic Sensors are widely used in Robo projects for Distance measurement as well as Obstacle detector.They are available as modules in the market.
There are 3 pin modules , 4 pin as well as 5pin modules . 3 pin is generally not available in Indian market.
Generally available ones are 4 pin or 5pin modules.
HC – SR 04 HC – SR 05
The Ultrasonic module has two Transducers ,one for Transmit & the other for Receive.Both are fixed on a single PCB with control circuit,so that it can be easily used in your Robo projects.
Ultrasonic distance sensor provides precise, non-contact distance measurements from about 2 cm to 400cm.
Ultrasound is a high frequency sound of frequency 40 KHz (we can’t hear this frequency,but animals can).
The principle of ultrasonic distance measurement is the same as that of RADAR.
Principle of Operation
From Arduino generate a short 10uS pulse to the Trigger input to start the ranging. The Ultrasonic Module will send out an 8 cycle burst of ultrasound at 40khz and raise its echo line high.
It then listens for an echo, and as soon as it detects one it lowers the echo line again. The echo line is therefore a pulse whose width is proportional to the distance to the object. By timing the pulse it is possible to calculate the range in inches/centimeters . If nothing is detected then the module will lower its echo line anyway after about 30mS.
The module provides an echo pulse proportional to distance. If the width of the pulse is measured in uS, then dividing by 58 will give you the distance in cm, or dividing by 148 will give the distance in inches.
uS/58=cm or uS/148=inches.
The module can be triggered as fast as every 50ms, or 20 times each second. You should wait 50ms before the next trigger, even if the SRF05 detects a close object and the echo pulse is shorter. This is to ensure the ultrasonic "beep" has faded away and will not cause a false echo on the next ranging.
The sensor can detect objects within 3cm to 3m range.
5 PIN MODULE
Specifications :
> The module includes ultrasonic transmitter, receiver and control circuit .
> Its stable performance and high ranging accuracy make it a popular module in Robotics.
> There are 5 pins out of the module, VCC, Trig, Echo, OUT & GND.
> The name of 4th pin “OUT “ is misguiding.As such no output is available at this pin.This is a Mode Select pin.Generally it is left unused.
> It performs best in 30 degrees angle .
> Open source Arduino library is readily available to use.
> It operates at 5V DC & consumes 15mA current.Range of sensing is from 3cm up to 400cm with a resolution of 1cm.
> Trigger pulse width is 10us.
5 pin module can be used as a 4 pin or 3 pin module .
If you leave out the 4th pin (OUT) in a 5 Pin Module , it behaves as a 4 pin Module.
If you Ground the 4th pin then it behaves as a 3 pin PING module.
If 4th pin is connected to GND ,pin 2 is left out.
Now the Trigger Input & Echo Output appears on single pin 3 (behaves as a 3 pin module).
So 4th pin of a 5pin module acts as a Mode Select pin.
The Arduino board sends a short pulse to trigger the detection, then listens for a pulse on the same pin using the pulseIn() function. The duration of this second pulse is equal to the time taken by the ultrasound to travel to the object and back to the sensor. Using the speed of sound, this time can be converted to distance.
5PIN Module being used as a 3 pin PING.
Below is the Timing diagram of a 5 pin module with 4th pin GND & 2nd pin left out
(works as a 3pin PING module)
The SRF05 will raise the echo line only after 700uS of end of the trigger signal. You have that long to turn the trigger pin around and make it an input and to have your pulse measuring code ready. The pulseIn() command of Arduino controllers does this automatically.
Connecting Sensor Module to Arduino
Following is the connection diagram between Ultrasonic module and an Arduino UNO board.
The connection involves only 4 wires.
PIN 2 of Arduino to TRIG of Sensor module
PIN 3 of Arduino to ECHO of Sensor module
5V of Arduino to Vcc of Sensor module
GND to GND
If you leave out pin 4 in a 5 pin module then connections are same for 4 pin as well as 5 pin module.
Now connect the USB cable of Arduino to your PC & Upload the following Code.
——————————
//code for testing Ultrasonic Module
#define ECHOPIN 3 // Pin to receive echo pulse
#define TRIGPIN 2 // Pin to send trigger pulse
void setup()
{
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}
void loop()
{
// Start Ranging -Generating a trigger of 10us burst
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
// Distance Calculation
float distance = pulseIn(ECHOPIN, HIGH);
distance= distance/58;
/* The speed of sound is 340 m/s or 29 us per cm.The Ultrasonic burst travels out & back.So to find the distance of object we divide by 58 */
Serial.print(distance);
Serial.println(" cm");
delay(200);
}
———————————
After Uploading is over the TX LED on Arduino starts blinking , indicating that it has started sending Trigger burst to the Sensor.
Open up the Serial Monitor of Arduino IDE & ensure that baud rate is 9600 ,same as that we used in code .
If you move your Hand or any object in front of the Sensor you can see the distance measurement reading on the Serial monitor.
NewPing Library for Ultrasonic Sensors
Libraries are a collection of code that makes it easy for you to connect a sensor, display, module, etc. For example, the built-in LiquidCrystal library makes it easy to talk to character LCD displays. There are hundreds of additional libraries available on the Internet for download.
The NewPing library adds many new features and breathes new life into these Ultrasonic distance sensors,whether it is a 3 pin, 4 or 5 pin module.
https://code.google.com/p/arduino-new-ping/
Libraries are often distributed as a ZIP file or folder. The name of the folder is the name of the library. Inside the folder will be a .cpp file, a .h file and a keywords .txt file, examples folder, and other files required by the library.
To install the library, first quit the Arduino application.
You can download New Ping from
http://code.google.com/p/arduino-new-ping/downloads/list
Then uncompress the ZIP file containing the library.
Drag the NewPing folder into your Libraries folder of Arduino IDE. Generally it is Under "My Documents\Arduino\libraries"
Inside the NewPing folder are some example codes.Just copy them & paste it undee Examples folder of your Arduino IDE.
Restart the Arduino application. Make sure the new library appears in the Sketch->Import Library menu item of the software.
That’s it! You’ve installed a library!
Now you can select & load an example code or
Download Sketch from
http://code.google.com/p/arduino-new-ping/wiki/Simple_NewPing_Example
& you can test 4 pin or 5 pin Ultrasonic using Arduino.
————————————–
// Code using NEWPING Library
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins .
void setup() {
Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings ( 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}
——————————-
Watch this video :
For availability of Ultrasonic Sensors contact :
Thank you, this really helped me with final year project at university! Was very clear and simple to understand
I am trying to use a three pin Ultrasonic Ranger. GND, VCC, NC, SIG are four pins. I am guessing the NC pin is NOT CONNECTED and hence I am ignoring this.
I used your Four PIN code and used the ECHO & TRIGGER on Single PIN by changing INPUT and OUTPUT mode respectively to do digitalWrite and pulseIn.
Serial port reads a value 586.70 cm irrespective of whether I place object in front of the Ultrasonic or NOT.
Appreciate if you get a chance to revert to me…
The demo is given for 4 pin or 5 pin sensors.If you’ve 3 pin PING sensor the working concept is different.The Trigger & Echo both are handle here by a single pin called Ping.In Arduino this Ping pin is connected to a digital pin.While generating trigger pulse this Arduino pin is declared as Output.After the trigeer puse is generated the same pin is declared as Input to receive the Echo pulse.Follow this official guide for PING sensors
http://arduino.cc/en/tutorial/ping
why the distance is divided by 58???
The speed of sound is 340 m/s or 29 us per cm.The Ultrasonic burst travels out & back.So to find the distance of object we divide by 58 (29 x 2)
i want to use ultrasonic sensor mounting in a tank where there is chances of high moisture and water to be collected in the sensor. will it work to measure water level or the sensor cannot measure the distance….. will it work if i use water proof ultrasonic sensors
For a water level sensor it is better to coat the ultrasonic sensor with glue ,leaving out the front sensing part.Glue sticks & GlueGun are easily available in the market.
The distance of the ultrasonic sensor must be stored in five different position in a array and accordding with the servo rotation from (1 to 180 degree) we have to store the distance. can anyone help me with it
What type of sensor required for small distance measurement such as 2 c.m. to 15 c.m.?