Arduino Project Hub
Project tutorial

Measuring Distance using Ultrasonic sensor © MPL-2.0

Using ultrasonic sensor to find the distance between the sensor and the object placed.

  • 3,212 views
  • 1 comment
  • 3 respects

Components and supplies

Apps and online services

About this project

This a very simple project to find the distance between .While going through the internet i got an idea to make something using ultrasonic sensor& arduino uno.

In this project i will use SR04 ultrasonic sensor.

Wiring...

Connect the SR04 VCC pin to the Arduino 5v

Connect the SR04 GND pin to the Arduino GND

Connect the SR04 TRG pin to the Arduino Digital pin 12

Connect the SR04 ECHO pin to the Arduino Digital pin 11

Code for making this...

#include <NewPing.h>

#define TRIGGER_PIN  12  
#define ECHO_PIN     11  
#define MAX_DISTANCE 200 

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 

void setup() {
  Serial.begin(115200); 
}

void loop() {
  delay(50);                     
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
}
Distance measuring using a SR-04 Ultrasonic Sensor and Arduino UN0


Schematics

Distance using Ultrasonic Sensor
Ultrasonic Distance.fzz

Code

Ultrasonic.inoC/C++
#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 and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

Comments

Add projectSign up / Login