Me and my friend are working on a project, description of project is attached. We were asked to build a remote height estimation device (ex: height of a human or another object). We decided to do it using arduino, ultrasonic sensor, LCD display, push button and a servo motor.

Plan Of The Project
We aim the ultrasonic sensor to the base of an object using a LASER (LASER is used only for aiming purpose), arduino saves the distance to the base , after that a push button is pressed and system starts to scan an object by rotating the sensor by 10 degrees (can be modified). When sensor detects objects top part it stops scanning. Distance to the top is saved by arduino. Using the amount of steps made by servo motor, angle between bottom, sensor and top is determined. Using cosine theorem arduino calculates the heihgt of scanned object.

We did first attempts but the values it displays on LCD are wrong most of the time but sometimes correct values are displayed.
PLEASE see my code and if there are any mistakes please modify or u can write ur own code. Just help me with this project, its a big pain in my a…

I would be grateful to you
Thank YOU

#include <Servo.h> 
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

Servo myservo;
int pushButton = 2;

int echoPin = 7; 
int trigPin = 8,servo=9; 
unsigned long duration; 
float distance,a,b,theta=0,h;
float c;
void setup() { 
  pinMode(pushButton, INPUT); 
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
void loop() { 
  int buttonState = digitalRead(pushButton); 
  if (buttonState==0){
 for (int i=0; i<15; i++){
if (i==0)  a=ping();   
else if (i>0 && ping()<=165) {distance = ping();
else {b=distance;
      digitalWrite(servo, LOW);
      goto meas;}
  meas: lcd.print("Height: ");
  lcd.println(" cm");
  delay(5000); }

else {myservo.write(0);
long ping()
  // Send out PING))) signal pulse
  digitalWrite(trigPin, LOW);  
  digitalWrite(trigPin, HIGH); 
  digitalWrite(trigPin, LOW); 
  duration = pulseIn(echoPin, HIGH); 
  //Convert duration into distance
  return duration / 29 / 2;

EEE 499 GRADUATION.pptx (332 KB)

Use CTRL-T to format your code to make it more readable.
Add more comments to describe what is going on.
Where does the LASER come into play? Is that just manual thing to ensure the ping sensor is pointing at something?
I'm wondering how small of an area the ping can detect - how much reflection do you get back from an absorbtive object like a person with all kinds of irregular surfaces that can deflect the signal?

LASER is used only for aiming the ultrasonic sensor to the base of an object.
I have HC-SR04 ultrasonic sensor
About beam width see the picture (i attached it)
Or do i have to buy another sensor? from here may be:

This is not my project but there are some details about ultrasonic sensor


You have ping sensor pretty low down, near the base of an object? Got to imagine reflections would come back above the receiver once you start tilting it up vs straight ahead at a vertical surface.

Thanks for your attention, i’m so grateful
I attached a picture.
The sensor and servo are attached to each other because servo rotates it by 10 degrees during scanning. And all together i.e. servo+arduino+LCD+sensor will be portable device, we will design it that way. Whe measuring height we will hold our instrument with hands at a fixed distance, approximately between top and bottom. Not so close to the base.
By the way, we may assume that object height doesn’t exceed 2 meters and an object is in front of the wall.
So when sensor comes to the top of an object it takes data, the it continues scanning, it rotates 10 more degrees and takes data again but this data will be a distance to the wall i.e. it’s a large distance so the arduino must take previous value as a distance to the top.

How do you know you are getting good signals back?

I don't know that i'm getting good signals back :slight_smile: But i believe that some of the signals reflect back to the sensor, may be not exactly from top but may be a little lower from top point. This is the only idea we have to estimate the height of an object remotely. About 10% error in height is ok for us, in worst case 15%

How about a different approach - you take three measurments - angle up, angle down, and base distance.
Then some basic math - SOH CAH TOA
tangent of your angle is opposite side (unknown) over adjacent side (ping measure).
Or Tan(theta) x adjacent = opposite.
Do for top & bottom angle, add the 2 results.
Visually line up the angle, use a gyro or accelerometer to measure the angle and let uC take ping for distance and do the math.

It seems to be a very good idea! :slight_smile:
angle up, angle down, and base distance? I didn’t understand, could you please show it in paint picture, pleeeease?
Is that picture I’ve attached correct?

You said: “Visually line up the angle” i think if we use LASER to aim the gyro+accelerometer board to top or bottom would increase the
precision, what do u think?
So in this case instead of servo i will use gyro and accelerometer right?

Thank you Sir, for your kindness.

Correct. You are looking to see where the laser is pointing, yes? So that would be visual.

Just to make sure, we must measure distance to top, bottom and to some point between using ultrasonic sensor and at the same time we measure the angles theta and alpha using gyro+accelerometer board after that just simple calculations. Unfortunately we still have an error because of ultrasonic sensors beam width. Is there any other type of sensor but ultrasonic?
But I believe that ur proposal will work better than ours, with servo motor.
Thanks Sir!

You don't need distance top to bottom. Only the perpendicular distance from measuring point to the object, and then the angle to top and the angle to bottom. Use the gyro/accelerometer, maybe a Sparkfun IMU, to keep your measuring platform level & perpendicular to the vertical object being measured.
This is high geometry, or maybe trigonometry. SOH CAH TOA.

aah, ok now i got it! :slight_smile:
Thank you very very much sir.
Now i will order IMU we will make this project and if we are stuck with programming part i will return or may be i will e-mail you.
I think taht this IMU will work very well?

"This is a simple breakout for the ADXL345 accelerometer and the ITG-3205 gyro. With this board, you get a full 6 degrees of freedom. The sensors communicate over I2C and one INT output pin from each sensor is broken out. If you need a simple and tiny board that gives you 6 degrees of freedom, this would be a good choice."

I don't have any experience with those parts, I'm there are others who do.

I’m about your idea to measure height (using IMU). In a picture that i attached here, apart from ultrasonic sensor how can we measure ‘L’? Because ultrasonic sensor has limit i.e. an object mustn’t be so far (within 3-4 meters away), can we increase this limit? Any ideas about it? May be another kind of sensor?

Perhaps laser