Kenneth Yang
Published © GPL3+

Ultra-Sonic "Ping" Sensor

Need your project to gauge distance? Here's your module!

BeginnerProtip4 minutes54,719
Ultra-Sonic "Ping" Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×6
HC-SR04 (Ultra-sonic "ping" senosr)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Wiring

a Fritzing showing the wire-up for the example code (and other uses)

Code

HC-SR04_UltrasonicSensor

Arduino
This is the main file.
const int out=12;
const int in=13;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(out,OUTPUT);
  pinMode(in,INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  long dur;
  long dis;

  digitalWrite(out,LOW);
  delayMicroseconds(2);

  digitalWrite(out,HIGH);
  delayMicroseconds(10);
  digitalWrite(out,LOW);

  dur=pulseIn(in,HIGH);

  Serial.println(String(dur));

  delay(100);
}

Credits

Kenneth Yang

Kenneth Yang

8 projects • 100 followers
Maker, developer, 3D content creator

Comments