haris511
Published © GPL3+

DIY Ultrasonic Sensor Measurement

Uses an ultrasonic sensor with an LCD to measure objects.

AdvancedFull instructions provided321
DIY Ultrasonic Sensor Measurement

Things used in this project

Hardware components

Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
Mine is a 4 pin but any will work
×1
Jumper wires (generic)
Jumper wires (generic)
Lots of wires both male and female.
×18
Arduino UNO
Arduino UNO
Any Arduino board will work.
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Any Ultrasonic sensor will work but if you are using a different one than mine you may need to tweak your code.
×1

Software apps and online services

Arduino IDE
Arduino IDE
Any Arduino IDE will work.

Hand tools and fabrication machines

Scissor, Electrician
Scissor, Electrician
Any scissors will be fine.
Tape, Clear
Tape, Clear
I am using clear tape but you may use other types of tape.

Story

Read more

Custom parts and enclosures

Ultrasonic Sensor Measurement

It's just a picture but you will need it for the connections.

Schematics

Ultrasonic Sensor Measurement

It's just a picture of the connections.

Code

Ultrasonic Sensor Measurement

C/C++
You will need to download a library or two so pay attention to all of the comments, I don't put them there for no reason.
#include <Wire.h> //The "Wire" library should be pre installed with your IDE if not download it.
#include <LiquidCrystal_I2C.h> //This library requires you to manually download it so here is the download link: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library

LiquidCrystal_I2C lcd(0x27, 16, 2);//I2C address and dimensions you may need to change it if your LCD is different.
int length = 0;//The length of the object
int range = 20;//Set the range of your obejct's length

//Declares the pins on the ultrasonic sensor
long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);// Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}

void setup()
{
  // initialize the LCD
  lcd.begin();

  // Turn on the blacklight to print a message.
  lcd.backlight();
}

void loop()
{
  //Ok heres were all the math is. It may look a little scary but all it's doing is subtracting the distance the ultrasonic sensor senses by the range and getting the absoulute value of it which is the length of the object.
  length = abs((0.01723 * readUltrasonicDistance(9, 10) - range)); 
  //This just prints out "Object Length" and then the object length.
  lcd.print("Object Length:");
  lcd.setCursor(0,1);
 lcd.print(length);
 delay(500);
 lcd.clear();
}

Credits

haris511

haris511

3 projects • 1 follower

Comments