MicroBob
Published © GPL3+

Ultra-Sonic "Ping" Sensor

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

BeginnerProtip4 minutes34,613
Ultra-Sonic "Ping" Sensor

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino 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

MicroBob

MicroBob

8 projects • 75 followers
DIY technology hobbyist

Comments