Mini BT Ultrasonic Rangefinder

> 1 m

During the weekend, I wanted to create a simple Arduino project that would not take more than 30 minutes, so I have decided to come up with this portable Bluetooth-based ultrasonic range finder. Using Bluetooth to transmit the value to the smartphone, this is useful as a mean to measure things without the need of using rulers or measuring tape.

Gallery

Demo

 

Code

#include <SoftwareSerial.h>  // import the serial library

SoftwareSerial BTSerial(7, 6); // RX, TX

const int usPin = 9;  //Ultrasonic sensor pin

void setup() {
  // put your setup code here, to run once:
  BTSerial.begin(9600);
  BTSerial.println("Initialising...Ready");
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(usPin, OUTPUT);
  digitalWrite(usPin, LOW);
  delayMicroseconds(2);
  digitalWrite(usPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(usPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(usPin, INPUT);
  duration = pulseIn(usPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
  BTSerial.println(cm);
  delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

 

You may also like...