Project showcase
How to Make an Ultrasonic Ruler with Arduino UNO and HC-SR04

How to Make an Ultrasonic Ruler with Arduino UNO and HC-SR04

This is my first project, so please like the project and comment down below.

  • 8,557 views
  • 4 comments
  • 14 respects

Components and supplies

Ph a000066 iso (1) ztbmubhmho
Arduino UNO
×1
12002 04
Breadboard (generic)
**Optional**
×1
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
Any ultrasonic sensor would work.
×1
11026 02
Jumper wires (generic)
Four would be enough
×4
Adafruit industries ada62 image 75px
USB-A to B Cable
This will help you to connect the laptop with Arduino
×1
Laptop or computer
×1

Apps and online services

Ide web
Arduino IDE
To program and upload to your arduino

About this project

Well I was sitting at home wondering how to measure how far away things are without getting up of my couch. So I took a look at some projects. They were pretty lit but they didn't have inches and millimetres.

Code

Ultrasonic ruler with inches, centimetres, and millimetresArduino
This the code. Just copy it and paste it on your Arduino IDE. Comment down below how I can make this code better.
const int trigPin = 2;
const int echoPin = 4;
void setup() { 
Serial.begin(9600);} 
void loop()
{
long duration, inches, cm, mm;
pinMode(trigPin, OUTPUT); 
digitalWrite(trigPin, LOW); 
delayMicroseconds(2000); 
digitalWrite(trigPin, HIGH); 
delayMicroseconds(1000); 
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT); 
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration); 
cm = microsecondsToCentimeters(duration);
mm = microsecondsToMillimeters(duration);
Serial.print(inches); 
Serial.print("in, "); 
Serial.print(cm);
Serial.print("cm, ");
Serial.print(mm);
Serial.print(" mm");
Serial.println(); 
delay(1000);
}
long microsecondsToInches(long microseconds)
{return microseconds / 74 / 2;}
long microsecondsToCentimeters(long microseconds)
{return microseconds / 29 / 2;}
long microsecondsToMillimeters(long microseconds)
{return microseconds / 2.9 / 2;}

Schematics

Ultrasonic Ruler
The Ultrasonic ruler helps you to measure things with Ultrasonic sounds.
ultrasonic_ruler_757P6Dm0hK.fzz
Ultrasonic Ruler
Just wire the:

VCC of Ultrasonic sensor ---> 5V in Arduino
Trig of Ultrasonic sensor ---> Pin 2 in Arduino
Echo of Ultrasonic sensor ---> Pin 4 in Arduino
GND of Ultrasonic sensor ---> To GND in Arduino


And then just copy and paste the code above in your Arduino IDE. To see the distance just go to Tools -> Serial Monitor or just put Ctrl+Shift+M or for mac just Command+Shift+M.
Screen shot 2018 02 16 at 4 masvqzlcmm

Comments

Similar projects you might like

Getting Started with the HC-SR04 Ultrasonic sensor

by Isaac100

  • 135,836 views
  • 34 comments
  • 89 respects

Simple Ultrasonic Distance Measurer With LCD Display

Project showcase by onatto22

  • 25,384 views
  • 9 comments
  • 42 respects

Arduino Obstacle Avoidance Robot with Ultrasonic HC-SR04

Project showcase by Jorge Rancé

  • 38,902 views
  • 10 comments
  • 62 respects

Door Alarm With Ultrasonic Sensor

Project showcase by MattG

  • 31,431 views
  • 10 comments
  • 31 respects

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 26,521 views
  • 10 comments
  • 36 respects
Add projectSign up / Login