Kokakola
Published

Measuring distance with the HC-SR04 ultrasonic sound sensor

Go to measure distance with the HC-SR04

BeginnerFull instructions provided12 minutes30
Measuring distance with the HC-SR04 ultrasonic sound sensor

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Ultrasonic Sensor - HC-SR04
SparkFun Ultrasonic Sensor - HC-SR04
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Untitled file

Arduino
const int trigPin = 12;  
const int echoPin = 8;   

float duration;         
float distance;         

void setup() {
  Serial.begin(9600);              
  pinMode(trigPin, OUTPUT);        
  pinMode(echoPin, INPUT);         
}

void loop() {
  digitalWrite(trigPin, LOW);      
  delayMicroseconds(2);           

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

  duration = pulseIn(echoPin, HIGH);    
 
 

  distance = (duration * 0.0343) / 2;  

  Serial.print("Afstand: ");             
  Serial.println(distance);

  delay(100);                           
  
}

Credits

Kokakola

Kokakola

4 projects • 0 followers

Comments

Add projectSign up / Login