Ultrasonic Module

Ultrasonic Module

Home GPS Module Ultrasonic Module RF Module

Ultrasonic Module

Criteria:

  1. This sketch will display the distance to an object on user request
  2. This is useful for obstacle detection and evasion so that the rover can sense nearby objects and remain a set distance from it

Required Hardware


Useful Links

Link to buy sensor Here

Link to programming tutorial Here

Wiring Diagram


Sketch to determine the user's latitude and longitude

// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3
// Define variables:
long duration;
int distance;
void setup() {
// Define inputs and outputs:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
Serial.println("Press any key to determine how far an object is from the sensor "); //print user prompt
while(Serial.available()==0) {}
}
void loop() {
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration * 0.034 / 2;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(2000);
}