Go Down

Topic: UltraSonic Tape Measure Using 2 HC-SR04s (Read 1 time) previous topic - next topic

Feb 06, 2013, 01:09 pm Last Edit: Feb 06, 2013, 07:44 pm by Steelcity74 Reason: 1
Hello,

I have seen a lot of people asking in various forums about using 2 ultrasonic sensors together.  I could not find a solution so I decided to give it a go.  What i made is an ultrasonic tape measure that is in a box.  there are sensors on either side and a parallax 2x16 LCD in the middle.  The two sensors take readings from each side to given objects (ie walls of a room).  The code takes the two readings, adds them together along with the distance between each sensor, and displays the total inches between the two objects on the LCD.  The other cool part of this project is that when the two sensor are equal (IE in the center of the two objects being measured) a red LED is lit to tell you are are half way between the objects.  The good thing about this tape measure is that it does not matter how far you are from each object, you will still get the same reading.  I have tested this up to 25' and it is accurate +/-1"

Here is the code.  If anyone is interested i can upload some video of it working.  I hope this helps....





Code: [Select]

//This is some code for a tape measure that reads distances opposite sides of each other using 2 HC SR04 Ultrasonic Sensors
//reads both of the sensor data, adds them together, along with the length of the
//box they are in and then prints the total distance from one object (walls, beams, etc.)
//to the other on a 2x16 Parallax LCD Screen.  A red LED will light up when both sensors are approx. equal distance
//from an object.

//Define LCD Pin first
const int TxPin = 6;
//Start LCD Library (Parallax)
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
//Define Pins for both sensors and LED
const int trigA = 2;
const int trigB = 4;
const int echoA = 3;
const int echoB = 5;
const int led = 8;

void setup()
{
//Begin LCD Connection and set pins to output and input
 mySerial.begin(9600);
 pinMode(trigA, OUTPUT);
 pinMode(trigB, OUTPUT);
 pinMode(echoA, INPUT);
 pinMode(echoB, INPUT);
 pinMode(led, OUTPUT);
 pinMode(TxPin, OUTPUT);
 digitalWrite(TxPin, HIGH);
}

void loop()
{
 //Items declared later in the loop
 long durationA, durationB;
 float inchesA, inchesB, Total, diff, box;
 // This is the Code for Sensor A
 digitalWrite(trigA, LOW);
 delayMicroseconds(2);  
 digitalWrite(trigA, HIGH);
 delayMicroseconds(500);
 digitalWrite(trigA, LOW);
 durationA = pulseIn(echoA, HIGH);
 inchesA = microsecondsToInches(durationA);
 delay(200);
 
 // This is the Code for Sensor B
 digitalWrite(trigB, LOW);
 delayMicroseconds(2);
 digitalWrite(trigB, HIGH);
 delayMicroseconds(500);
 digitalWrite(trigB, LOW);
 durationB = pulseIn(echoB, HIGH);
 inchesB = microsecondsToInches(durationB);
 delay(200);
 
// Get the difference between inchesA and inchesB
diff = inchesA-inchesB;

// if the difference is negative, multiply by -1 to make it positive.
if (diff < 0) diff = diff * -1;

// if the difference is less than or equal to ΒΌ inch, turn on red LED.
if (diff <= 0.25)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
 //Total the two sensors together (including the length of the box they are attached to
  box = 3.45;
  Total = inchesA + inchesB + box;
 
//Write Results to Parallax LCD:  
 mySerial.write(12);                 // Clear            
 mySerial.write(17);                 // Turn backlight on
 delay(5);                           // Required delay
 mySerial.print(Total);              // First line
 mySerial.write(13);                 // Form feed
 mySerial.print("Inches Total");     // Second line
}

//Convert Microseconds to Inches (default Parallax conversion is no good
float microsecondsToInches(long microseconds)
{
  //return microseconds / 148.0;
   return microseconds * 0.00675676;
}

Go Up