ccPegasus
Published

Ultrasonic Measuring Tape

Measure distances with an ultrasonic sensor and Arduino UNO, and output the distance to the serial monitor!

BeginnerFull instructions provided1,775
Ultrasonic Measuring Tape

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Arduino Web Editor
Arduino Web Editor

Story

Read more

Schematics

The Diagram

Here is the diagram for the arduino circuit

Code

Arduino Code

Arduino
/*
This code should output the distance from the
ultrasonic sensor to the nearsest object

press crtl + shift + m to open the serial moniter

https://bit.ly/2KxSD1h
https://bit.ly/29aWeQi
*/

// Define the pins for the ultrasonic sensor
int const trigPin = 5;
int const echoPin = 4;
int pos = 0;    // variable to store the servo position

void setup() {
  pinMode(trigPin, OUTPUT); // trig pin will have pulses output
  pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
  Serial.begin(9600);
}

void loop() {
  // Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
  int duration, distance;
  
  // Output pulse with 1ms width on trigPin
  digitalWrite(trigPin, HIGH);
  delay(1);
  digitalWrite(trigPin, LOW);
  
  // Measure the pulse input in echo pin
  duration = pulseIn(echoPin, HIGH);
  
  // Distance is half the duration devided by 29.1 (from datasheet)
  distance = (duration/2) / 29.1;

  // Print the distance (CM) into the serial moniter
  Serial.print(distance);
  Serial.println(" CM");
  
  // Waiting 60 ms won't hurt any one
  delay(60);
}

Credits

ccPegasus
0 projects • 0 followers

Comments