Imperial_Proggramer
Published

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.

BeginnerShowcase (no instructions)6,134
How to Make an Ultrasonic Ruler with Arduino UNO and HC-SR04

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Breadboard (generic)
Breadboard (generic)
**Optional**
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Any ultrasonic sensor would work.
×1
Jumper wires (generic)
Jumper wires (generic)
Four would be enough
×4
USB-A to B Cable
USB-A to B Cable
This will help you to connect the laptop with Arduino
×1
Laptop or computer
×1

Software apps and online services

Arduino IDE
Arduino IDE
To program and upload to your arduino

Story

Read more

Schematics

Ultrasonic Ruler

The Ultrasonic ruler helps you to measure things with Ultrasonic sounds.

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.

Code

Ultrasonic ruler with inches, centimetres, and millimetres

Arduino
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;}

Credits

Imperial_Proggramer

Imperial_Proggramer

1 project • 1 follower

Comments