Devices & Components
Arduino Uno Rev3
Ultrasonic Sensor - HC-SR04 (Generic)
Jumper wires (generic)
Breadboard (generic)
Laptop or computer
USB-A to B Cable
Software & Tools
Arduino IDE
Project description
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.
1const int trigPin = 2; 2const int echoPin = 4; 3void setup() { 4Serial.begin(9600);} 5 6void loop() 7{ 8long duration, inches, cm, mm; 9pinMode(trigPin, OUTPUT); 10 11digitalWrite(trigPin, LOW); 12delayMicroseconds(2000); 13digitalWrite(trigPin, 14 HIGH); 15delayMicroseconds(1000); 16digitalWrite(trigPin, LOW); 17pinMode(echoPin, 18 INPUT); 19duration = pulseIn(echoPin, HIGH); 20inches = microsecondsToInches(duration); 21 22cm = microsecondsToCentimeters(duration); 23mm = microsecondsToMillimeters(duration); 24Serial.print(inches); 25 26Serial.print("in, "); 27Serial.print(cm); 28Serial.print("cm, "); 29Serial.print(mm); 30Serial.print(" 31 mm"); 32Serial.println(); 33delay(1000); 34} 35long microsecondsToInches(long 36 microseconds) 37{return microseconds / 74 / 2;} 38long microsecondsToCentimeters(long 39 microseconds) 40{return microseconds / 29 / 2;} 41long microsecondsToMillimeters(long 42 microseconds) 43{return microseconds / 2.9 / 2;}
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.
1const int trigPin = 2; 2const int echoPin = 4; 3void setup() { 4Serial.begin(9600);} 5void loop() 6{ 7long duration, inches, cm, mm; 8pinMode(trigPin, OUTPUT); 9digitalWrite(trigPin, LOW); 10delayMicroseconds(2000); 11digitalWrite(trigPin, HIGH); 12delayMicroseconds(1000); 13digitalWrite(trigPin, LOW); 14pinMode(echoPin, INPUT); 15duration = pulseIn(echoPin, HIGH); 16inches = microsecondsToInches(duration); 17cm = microsecondsToCentimeters(duration); 18mm = microsecondsToMillimeters(duration); 19Serial.print(inches); 20Serial.print("in, "); 21Serial.print(cm); 22Serial.print("cm, "); 23Serial.print(mm); 24Serial.print(" mm"); 25Serial.println(); 26delay(1000); 27} 28long microsecondsToInches(long microseconds) 29{return microseconds / 74 / 2;} 30long microsecondsToCentimeters(long microseconds) 31{return microseconds / 29 / 2;} 32long microsecondsToMillimeters(long microseconds) 33{return microseconds / 2.9 / 2;}
Downloadable files
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.
Ultrasonic Ruler

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.
Ultrasonic Ruler

Ultrasonic Ruler
The Ultrasonic ruler helps you to measure things with Ultrasonic sounds.
Ultrasonic Ruler
Comments
Only logged in users can leave comments