Project tutorial
Ultrasound Sensor: 2D Tracking With Arduino

Ultrasound Sensor: 2D Tracking With Arduino © GPL3+

Typically ultrasound sensor used to measure the range of the target, this project can locate the target in two dimensions.

  • 4,092 views
  • 2 comments
  • 4 respects

Components and supplies

Apps and online services

About this project

Ultrasound ranging is a complicated task that made easy by the readily available and inexpensive module for Arduino. To detect or measure the distance, it transmits the signal to the target and the target reflects that back. Arduino measure time is taken for complete travel. As the speed of sound is known the distance between module and target can be calculated easily.

This tutorial is divided into two-part. The first part explains the calibration of an ultrasound sensor and 2d tracking with 2 sensors. while the second part discusses on accurate 2d tracking with multiple ultrasound sensors.

A. Ultrasound sensor calibration and 2d tracking

Ultrasound sensor or sonar sensors used to measure just the distance from target here I am trying you get the exact location/coordinate of the target. I have used HC-SR04 for the purpose.

I tried to explain a similar thing to the below video.

Explanation on 2d tracking with 2 Modules

Calibrating the Sensor:

To check the accuracy of this sensor I placed a target in front of it and measured the distance by using the ultrasound sensor and also by ruler scale. Measurement by ultrasound sensor is strength forward.

from the above plot, it is very clear that the output from the sensor is fairly linear. Slop is also almost one (as expected) and the standard deviation of the error is around 9mm. which is a mostly static error. As I was measuring all distances from the extreme end of the sensor, while Arduino measures it from the transmitting and sensing element.

Measurement for 2D Coordinate: Concept

In the previous case, we were measuring only one distance from the target that gives a circle of that measured radius. the object can be anywhere on the circumference of the circle. But the sensor operates only on the cone shape area so the possibility is reduced to an arc (from a circle).

If we add one more sensor aside from the first sensor, the intersection of both of these sensors will give the exact location of the target.As shown in the 4th figure, both sensor has an operating cone, the area where both of the cones intersects, the exact location can be calculated.

If the object is in the area covered by only one cone than only range can be calculated. and obviously, we will get no idea about an object if it is outside of these cones.

Measurement for 2D Coordinate: Calculation

By measuring distance by two sensors a triangle will be created. in which one angle is the target and rests two angles are sensors. The length of all sides of this triangle is known.

Triangle is completely constrained and any required data can be calculated by applying basic geometry. to measure coordinate od the target cosine rule is applied from which angle from one of the sensors can be calculated which further can be converted to Cartesian coordinate (or polar coordinate with a required reference point).

The attached image shows the formula to calculate the location.

Measurement for 2D Coordinate: Setup

Setup is very simple, as discussed before it required at least two of these ultrasound sensors. I have attached it to the breadboard. The distance between these modules needs to be measured. this distance needs to measure from left extreme of one sensor to left of another sensor or vice versa. below point need to be considered while selecting separation distance between the sensor.

  • If the target is closer then both of these sensors need to be slightly closer, such that the cone of these sensors intersects at close distance.
  • The lower separation will lead to lower accuracy (especially at the high range) and higher separation distance will lead to better accuracy.
  • An angle of sensor facing can be changed as per the requirement to intersect cones of the sensor.

Woking of Code:

working of code will follow the below steps:

  • Measure range of sensor 1,
  • Measure range of sensor 2,
  • Solve the coordinate.

Code is available at below link:

Once the system is set up it can be tested for the various target for accuracy. refer to the previously attached video for a demonstration.

B. Ultrasound sensor calibration and 2d tracking

The above method also explained in the below video:

Working:

This method makes use of multiple sensors. Higher the number of the sensor can be useful for more accurate measurement. Let's take an example of a setup with 4 sensors (as I am using for the tutorial). So after finishing measurement, we will have the distance to target from all of this sensor.

To calculate the position of the target in the lateral direction we need two measurements to complete a triangle and locate the target position. As we have 4 sensors we will have 6 combinations (4C2) of pairs. These pairs can be seen from the above images.

Once the pair is decided we have to calculate the target location for every combination of the sensor pair. In the end, there will be six measurements for the target location. By using all these six values relatively accurate measurements can be made. now all these data are fused with weightage as per accuracy. if we consider a triangle than the higher distance between the sensor will lead more accurate. So higher weightage is given to the pair having a sensor far from each other.

This method is still not very accurate as we are not making the measurement from the single point target. as the target is a planner object sensor that will measure the distance from the nearest point.

Preparing Setup:

This setup is similar to the previous tutorial on 2d tracking with two sensors. Ground supply and Vcc is connected to a common line and all Tx and Rx pin are connected to one of the Arduino pins. It can be connected to any of the Arduino pins that need to be defined in code. The above image can be referred for the overall connections.

After that, all these sensors need to be mounted with a fixed distance. similar to the previous case, higher separation will lead to better accuracy and vice versa. the sensor may face in any direction for best coverage. In this case higher the number of sensors will face the target better result we can obtain.

Working of Code

There are basically 4 functions that work to get accurate output and avoid a false reading.

1. Raw_cap(): This function will simply take data from the available sensor, sensor number can be any number, as discussed higher number will give better accuracy. another point need to consider is that in the command


pulseIn(Rx[i], HIGH,5000);

The Value 5000 need to be changed as per object placement, once we had the approx operating range we can calculate the value of time taken (in Microseconds) by the sound wave to travel through and back to the range and replace with 5000. At the completion of the function, we will end up with an array having values measured by all sensors.

2. Position_calc(): This function will take various combinations (nC2 for n number of the sensor) and will calculate the position of the target in 2D,

3. check(); This function will check every combination taken by position_calc() function is possible to form a triangle or not. If this function concludes this test negative Position_calc() will simply make entry as 0 values, this function is very avoided considering the wrong values of the sensor. This function makes use of the property that summation of the length of any two sides of the triangle is always higher than the length of the rest 3rd side of the triangle.

4. Fuse_data() The above function will take the weighted average of all measurements, weightage proportional to distance in between the pair of the sensor.

Setting Up the Code and Testing the Output:

Values like the number of sensors and sizes of arrays as commented in code need to be updated. Another important parameter is the distance between these sensor need to be defined. Here the distance from the leftmost sensor to all sensors is added in an array named Position [ ].

Once everything is set up target can be placed in front of the sensors and check the output from the sensor.

Code

Multi sensor 2D trackingArduino
// below code is for target tracking in 2D using 2 ot more ultrasound sensor
//more details: https://youtu.be/MFVwW3jtXvw
//by abhilash

int n=4;  //nos of sensor
int Tx[4]={12,8,6,4};       //pin number: sensor 1, sensor 2,...
int Rx[4]={11,7,5,3};       //pin number: sensor 1, sensor 2,...
float Raw[4];
float Position[4]={0,41,85,130}; //all sensor midpoint are located in one line only, 
                                 // position mentoined from mid point for left most sensor
float Calc_X[6];        //for 4 sensor, total calculated outpurt wil be 4C2=6 combinations
float Calc_Y[6];
int test[6];

float Final_X, Final_Y; // final calculated values wil be stored in these variables

void setup() 
{
      Serial.begin(250000); // Starting Serial Terminal

        for(int i=0;i<n;i++)
        {
         pinMode(Tx[i], OUTPUT);   
         pinMode(Rx[i], INPUT);
        }
}

void loop() 
{

Raw_cap();
Position_calc();
Fuse_data();
Serial.print(Final_X);  // X coordinate
Serial.print("\t");
Serial.println(Final_Y);

}






//==============================Functions==============================//



//Raw_cap is function to capture data from various sensor
//by setting value of samples, average of defined samples can be taken as output
void Raw_cap()          
{ 
  float sample=1;
  for(int i=0; i<n; i++)
        { 
          Raw[i]=0;
            for(int j=0; j<sample; j++)
             {              
                digitalWrite(Tx[i], LOW);   
                pinMode(Rx[i], INPUT);
                digitalWrite(Tx[i], LOW);
                delayMicroseconds(2);
                digitalWrite(Tx[i], HIGH);
                delayMicroseconds(10);
                digitalWrite(Tx[i], LOW);
                Raw[i] =Raw[i] +  pulseIn(Rx[i], HIGH,5000);
                delay(15);
             }
          Raw[i]=Raw[i]/sample;
          Raw[i]=Raw[i]*343/2000;
    //      Serial.println(Raw[i]);
          
        }
}


//-------------------------------------------------------------------------------------//
// this function applies tringle properties to find co-ordinates of 
// various combination of ultrasound sensor
void Position_calc()
{float d1,d2,a;
int  count,test_temp;
count=0;
  for(int i=0;i<n;i++)
  {
    for(int j=i+1;j<n;j++)
    {
    d1=Raw[i]; d2=Raw[j]; a=Position[j]- Position[i];
    test_temp=check(d1,d2,a);
   
      if(test_temp==0)
        {      
            Calc_X[count]=0;
            Calc_Y[count]=0;
            test[count]=0;        
        }
    
      if(test_temp==1)
        {
   //  Serial.print(d1);Serial.print("\t");
   //  Serial.print(d2);Serial.print("\t");
   //  Serial.print(a);Serial.print("\t");
          
      //applying cosine rule to calculate angle    
              float theta=acos((((d1*d1)+(a*a)-(d2*d2)))/(2*d1*a));
              Calc_X[count]=d1*cos(theta)+ Position[i]; // y coordinate 
              Calc_Y[count]=d1*sin(theta);            // X coordinate  
              test[count]=1;  
           //   Serial.print(Position[i]);Serial.print("\t");
        }  
 // Serial.print(Calc_X[count]);Serial.print("\t");
 // Serial.print(Calc_Y[count]);Serial.print("\t");
 // Serial.print(test[count]);Serial.print("\n");

      count=count+1;   
    } 
  }
}



//-------------------------------------------------------------------------------------//
// this function check if triangle is possible our not for selected combination of sensor data
int check(float d1,float d2,float a) 
{
  int temp_test;
if(d1>d2+a || d2>d1+a  ||  a>d2+d1){return 0;}
else{return 1;}
}


//-------------------------------------------------------------------------------------//
//this function combines data of various sensor data combination with
// weitage as per accuracy of perticular combination
// and proportional to distance in between sensors
void Fuse_data()
{
float temp_sum=0;
int count=0;
Final_X=0;
Final_Y=0;
for(int i=0;i<n;i++)
    {
    for(int j=i+1;j<n;j++)
      {
        if(test[count]==1){temp_sum=temp_sum+Position[j]-Position[i];}
   
        Final_X=Final_X+Calc_X[count]*test[count]*(Position[j]-Position[i]);
        Final_Y=Final_Y+Calc_Y[count]*test[count]*(Position[j]-Position[i]);
        
        count=count+1;
      }
    }
    
  Final_X=Final_X/temp_sum;
  Final_Y=Final_Y/temp_sum;

}
Tracking with 2 sensorArduino
float  X,Y;
void setup() {
    Serial.begin(9600); // Starting Serial Terminal

    
    pinMode(8, OUTPUT);   
    pinMode(7, INPUT);
    
    pinMode(4, OUTPUT);
    pinMode(6, INPUT);
}

void loop() 
{
Track();
Serial.print(X);
Serial.print("\t");
Serial.println(Y);
}



//------------Function For Object Track with Ultrasound------------//
//this method use triangulates Position of object by Using measurement 
// of two ultrasound sensor saperated by known distance.
//output is based on mid point of two sensor
//for more details refer following link
//
//             by-ABHILASH

void Track()
{
   float d1,d2,theta;
   float dist=81; //distance between two sensors

   //pins for sensor 1 need to be uploaded
   digitalWrite(8, LOW);
   delayMicroseconds(2);
   digitalWrite(8, HIGH);
   delayMicroseconds(10);
   digitalWrite(8, LOW);
   d1 = pulseIn(7, HIGH);
   d1=d1*343/2000;

  
   delay(10);
   //pins for sensor 2 need to be uploaded
   digitalWrite(4, LOW);
   delayMicroseconds(2);
   digitalWrite(4, HIGH);
   delayMicroseconds(10);
   digitalWrite(4, LOW);
   d2 = pulseIn(6, HIGH);
   d2=d2*343/2000;
  
theta=acos((((d1*d1)+(dist*dist)-(d2*d2)))/(2*d1*dist));

if(theta<3 && theta>0){               // to avoid impossible values
  X=d1*cos(theta)+ dist/2; // y coordinate 
  Y=d1*sin(theta); // X coordinate
}
else{Serial.println("bad track");}
/*Serial.print(d1);
Serial.print("\t");
Serial.print(d2);
Serial.print("\t");
Serial.print(theta*180/3.14);           //for debugging
Serial.print("\t");
Serial.print(X);
Serial.print("\t");
Serial.print(Y);
Serial.print("\n");*/
delay(10);

}

Comments

Similar projects you might like

Distance Sensor and OLED

Project tutorial by javier muñoz sáez

  • 49,590 views
  • 25 comments
  • 111 respects

Eye Motion Tracking Using Infrared Sensor

Project tutorial by HomeMadeGarbage

  • 29,297 views
  • 16 comments
  • 104 respects

Automatic Fan with PIR Sensor and LM35 Sensor

by Jamaluddin

  • 15,385 views
  • 6 comments
  • 20 respects

Using IR Sensor (TCRT 5000) With Arduino

Project tutorial by abhilash_patel

  • 9,477 views
  • 0 comments
  • 1 respect

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 26,521 views
  • 10 comments
  • 36 respects

IOT - Smart Jar Using ESP8266, Arduino and Ultrasonic Sensor

Project tutorial by Team Smazee

  • 16,910 views
  • 5 comments
  • 18 respects
Add projectSign up / Login