Introduction: Analog Distance Measuring Meter Using Arduino & Ultrasonic Sensor

Hi Guys,

I am Lenin k raj, and this is my first post in Instructables. so guys if you have any doubt or something is unclear please make a comment.

Today I 'll show you how to make a distance measuring device using an Arduino & Ultrasonic Sensor. The Meter reading is an Analog style using a micro servo.

Supplies

a) Arduino microcontroller board

b) Micro servo
c) Few jumper wires
d) 6V supply
e) An Ultrasonic sensor, I have used the HC SR04 module.

So let's start !!!!

Step 1: Marking Outline

Take any medium size plastic box and make an outline of it in a hard sheet (similar to a cartoon or a foam sheet) and make a precise cut through the outline markings.

Search for an analog meter reading image in google (Dail of the meter) and print it on an A4 sheet and mark its outline in order to make a precise cut. Apply some glue on the paper and paste it over the cartoon sheet.

Step 2: Connecting Arduino & Sensors

Now take an HC-SR04 ultrasonic sensor & Servo motor for markings its outline in the meter dial and cut through the markings in order to make a hole to attach servo and ultrasonic sensor.

Connect jumper wires as per the diagram.

  • Arduino 5V & GND pin is common for both HC-SR04 & Servo.
  • Connect Servo pulse pin to Arduino Pin 9
  • Connect HC-SR04 Trigger pin to 3 & Echo pin to 2
  • 6V power supply to 5V pin and GND pin which is common for Sensor and Servo.

Install any buzzer module to the Arduino pin 2 & 4 for making a beep sound once any object reaches very close to the meter.

Once again double-check the connections and after that, we can flash the code to Arduino microcontroller.

Step 3: Arduino Code

#include <Servo.h>

#define servoPin 9
#define triggerPin 3
#define echoPin 2
#define distanceFrom 0
#define angleFrom 30
#define angleTo 180
#define distanceLimit 100
#define STEP_ZERO 0
#define STEP_ONE 1
#define STEP_TWO 2
#define STEP_THREE 3
#define STEP_FOUR 4
#define STEP_FIVE 5
#define buzPin_P 11
#define buzPin_N 13

Servo servo;
int distanceTo = distanceLimit;
int stepDiv = distanceLimit / 5;
void setup() {
  // put your setup code here, to run once:

  setupPins();
  Serial.begin(9600);
  servo.attach(servoPin);

}

void loop() {
  switch (getAngleDiv()) {
    case STEP_FIVE:
      servo.write(1 * 140 / 5);
      break;
    case STEP_FOUR:
      servo.write(2 * 140 / 5);
      break;
    case STEP_THREE:
      servo.write(3 * 140 / 5);
      break;
    case STEP_TWO:
      servo.write(4 * 140 / 5);
      break;
    case STEP_ONE:
      servo.write(5 * 140 / 5);
      break;
    case STEP_ZERO:
      servo.write(6 * 140 / 5);
      break;
  }

}

void setupPins() {
  pinMode(servoPin, OUTPUT);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzPin_P, OUTPUT);
  pinMode(buzPin_N, OUTPUT);
}

void logger(String logs) {
  Serial.println(logs);
}
void logger(int logs) {
  Serial.println(logs);
}
void logger(long logs) {
  Serial.println(logs);
}
int getDistance() {
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(5);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(triggerPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  int distance = (duration / 2) / 29.1;
  if (distance <= 10) {
    buzOn();
  }
  else {
    buzOff();
  }
  distance = 2 * distance;
  if (distance > distanceLimit) {
    distance = distanceLimit;
  }
  logger(distance);
  return distance;
}
int getAngleDiv() {
  return getDistance() / stepDiv;
}
int getAngleDivFixed() {
  int returnVal = 5;
  if (getDistance() < 10) {
    returnVal = 0;
  }
  else if (getDistance() < 20) {
    returnVal = 1;
  }
  else if (getDistance() < 30) {
    returnVal = 2;
  }
  else if (getDistance() < 40) {
    returnVal = 3;
  }
  else if (getDistance() < 50) {
    returnVal = 4;
  }
  else {
    returnVal = 5;
  }
  return returnVal;
}
void buzOn() {
  digitalWrite(buzPin_P, HIGH);
  digitalWrite(buzPin_N, LOW);
}
void buzOff() {
  digitalWrite(buzPin_P, LOW);
  digitalWrite(buzPin_N, LOW);
}<br>

Step 4: Flashing Code & Testing

Connect Arduino and your laptop using A 2 B cable and flash your code to the controller using any Arduino IDE and fix the dial into the plastic box.

The servo indication should move according to the distance the object getting closer also the beep sound will come once an object reaches close to the meter.

If you are getting these results, then the program should be running correctly and everything should be connected the right way.