ULTRASONIC SENSOR

Description

  • Non-contact distance measurement
  • Very easy to set up, with only four pins
  • High accuracy and stability
  • Measures up to 400 cm
  • VCC input voltage 5V

Non-contact distance measurement, with a range of 2 to 400 cm. Among its many uses are robotics barriers, object distance measurements, level detection, security systems and vehicle detection/avoidance. The control circuit makes the measurements reliable and accurate, and with only four pins set up is fast and easy.

Supply voltage +5 VDC 15 mA
Operating frequency 40 kHz
Maximum range 400 cm
Minimum range 2 cm
Measuring angle 15 degrees
Input trigger signal TTL >10 μs
Output echo signal TTL level signal proportional to
distance
Dimensions 45 * 20 * 15 mm

Easy to connect, with only four cables. You can pin the module on the breadboard and use the male to male jumper cables, or use the male to female jumper cables to connect the module directly to the Arduino-compatible board.

To supply voltage to the board, connect the +5v pin to the Vcc power pin and the GND pin to the GND pin. For our example, connect Trig to D12 and Echo to D11.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Rewieved by Dr. Mangus for MangoLabs
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
 
#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 400 // Maximum distance we want to ping for (in centimeters).
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// NewPing setup of pins and maximum distance.
 
void setup() {
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
}
 
void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec).
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result
  Serial.println("cm");          //(0 = outside set distance range)
}
  1. In this example we are connecting:
    Arduino    Ultrasonic Sensor
    D11   —  Echo
    D12   —  Trig
    GND —  GND
    5V     —  Vcc
  2. Download and install the Arduino environment from here or use the web editor.
  3. Before compiling the code you must install the NewPing libraries.
    Download the Arduino libraries here.
  4. Unzip and paste the folder in the Arduino libraries folder. It’s in the same location as your default sketches. To find the sketchbook location, go to File / Preferences.
  5. Download the sketch here and open or open the Arduino environment and copy the code into a blank sketch.
  6. Connect your board to your computer with the included USB cable.
  7. Select your board in Tools / Board / Arduino Uno.
  8. Select your port in Tools / Port / COM# (Arduino Uno). This may be slightly different depending on your operating system.
  9. Upload by pressing the arrow in the circle to the upper left.
  10. To open the serial monitor, press the magnifying glass icon on the upper right.
  11. Set the baud rate to 9600 on the lower right.
  12. You should see a text indicating the distance from your sensor of a solid and big enough object, in centimeters.That’s it!