Electronics Champ
Published © GPL3+

Can Arduino Measure Heights of Tall Buildings?

Arduino learns trigonometry and can now measure the heights of tall objects with just a few clicks!

BeginnerFull instructions provided476
Can Arduino Measure Heights of Tall Buildings?

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
28BYJ-48 Stepper Motor
×1
ULN2003 Stepper Motor Driver Module
×1
Laser Diode, 2 Pins
Laser Diode, 2 Pins
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Switch Actuator, Head for spring return push-button
Switch Actuator, Head for spring return push-button
×2
Breadboard (generic)
Breadboard (generic)
×1
9V 1A Switching Wall Power Supply
9V 1A Switching Wall Power Supply
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Schematic

A part of the schematic was made by Tinkercad

Code

Code

Arduino
Upload this code to the Arduino
/*
 
  This project demonstrates how to calculate the
  heights of objects using Arduino. Upload
  this code to your Arduino board and make the
  necessary connections to start measuring heights.
 
  This program is written by Shreyas for Electronics
  Champ YouTube Channel. Please subscribe to this
  channel. Thank you.
 
*/
 
//including the library
#include <CheapStepper.h>
 
//define the variables
const int trigPin = 2;
const int echoPin = 3;
const int increment = 7;
const int decrement = 8;
bool calibrationMode = false;
float angle = 0.00;
int x = 0;
 
//create CheapStepper object
CheapStepper stepper(9, 10, 11, 12);
 
void setup() {
 
  //define the pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(increment, INPUT_PULLUP);
  pinMode(decrement, INPUT_PULLUP);
 
  //start serial communication
  Serial.begin(9600);
 
  //set stepper motor speed
  stepper.setRpm(20);
 
}
 
void loop() {
 
  if (calibrationMode == false) { //if arduino is in measurement mode...
 
    Serial.println("Arduino is in measurement mode");
 
    while (calibrationMode == false) {
 
      while (digitalRead(increment) == 0 && angle <= 90.00) { //if the 'increment' switch is pressed...
        stepper.moveCCW(1); //move the stepper motor
        delay(10);
        x++;
        angle = x/11.3; //update the angle
      }
 
      while (digitalRead(decrement) == 0 && angle >= 0.00) { //if the 'decrement' switch is pressed...
        stepper.moveCW(1); //move the stepper motor
        delay(10);
        x--;
        angle = x/11.3; //update the angle
      }
 
      angle = (angle > 90.00) ? 90.00 : angle;
      angle = (angle < 0.00) ? 0.00 : angle;
 
      float base = getDistance();
      float tanAngle = tan(((angle > 90.00) ? 90.00 : angle) * 0.0174533);
      float height = base * tanAngle;
 
      //print the values on the Serial Monitor
      Serial.print("Base: ");
      Serial.print(base);
      Serial.print("cm      ");
      Serial.print("Angle: ");
      Serial.print((angle > 90.00) ? 90.00 : angle);
      Serial.print("deg     ");
      Serial.print("Tan: ");
      Serial.print(tanAngle);
      Serial.print("      ");
      Serial.print("Height: ");
      Serial.print(height);
      Serial.println("cm");
      delay(500);
      checkCalibration();
     
    }
   
  }
 
  else if (calibrationMode == true) {
   
    Serial.println("Set the angle of the motor to 0 degrees using the switches\nAfter that, press both the switches together");
   
    while (calibrationMode == true){
 
      while (digitalRead(increment) == 0 && digitalRead(decrement) == 1) {
        stepper.moveDegreesCCW(1);
        delay(50);
      }
 
      while (digitalRead(decrement) == 0 && digitalRead(increment) == 1) {
        stepper.moveDegreesCW(1);
        delay(50);
      }
 
      checkCalibration();
 
    }
 
    angle = 0;
    Serial.println("Calibration successful!");
 
  }
 
}
 
//this function gets the distance between the object and the device
float getDistance() {
 
  long duration;
  float distance;
 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.0343 / 2;
  return distance;
 
}
 
 
//this function is used to switch modes
void checkCalibration () {
 
  if (digitalRead(increment) == 0 && digitalRead(decrement) == 0) {
    delay(1000);
    calibrationMode = !calibrationMode;
  }
 
}

Credits

Electronics Champ

Electronics Champ

3 projects • 10 followers
Projects based on breadboard electronics and Arduino with clear step-by-step instructions, circuit diagrams, schematics, and source code.

Comments