MoonLord-2-0
Published

Ping Sensor

Ping ping ping, it never stops.

BeginnerShowcase (no instructions)1,041
Ping Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino Web Editor
Arduino Web Editor

Story

Read more

Custom parts and enclosures

Ranger

Ping

Schematics

Ranger

Ping

Code

Ultrasonic Ranger

Arduino
It tells you the ping using the Serial Monitor.
#define trigPin 12
#define echoPin 11
#define MAX_DISTANCE 200
// define the timeOut according the maximum range. time out= 2*MAX_DISTANCE /100 /340 *1000000 = MAX_DISTANCE*58.8
float timeOut = MAX_DISTANCE * 60;
int soundVelocity = 340;
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}
void loop() {
  delay(100);
  Serial.print("Ping: ");
  Serial.print(getSonar());
  Serial.println("cm");
}
float getSonar() {
  unsigned long pingTime;
  float distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pingTime = pulseIn(echoPin, HIGH, timeOut);
  distance = (float)pingTime * soundVelocity / 2 / 10000;
  return distance;
}

Credits

MoonLord-2-0

MoonLord-2-0

3 projects • 0 followers

Comments