Home ARDUINO Build Distance Measuring System with Arduino UNO

Build Distance Measuring System with Arduino UNO

The ultrasonic sensor (HC-SR04) is usually used to find the distance of an object from a specific point. It’s fairly easy to do this with Arduino and the code is simple. But in this article, we’ll try something different from these popular HC-SR04 sensors.

By doing this, we can track the position of a transmitter using many ultrasonic receivers called triangulation, which can be used to automate the docking of robotic baggage followers and other similar applications.

Finding the distance between two U.S. sensors may sound fairly simple, but I face several challenges discussed in this project.

The techniques discussed in this article are not accurate enough and may be useful in real systems without modification.

At the time of this document, I did not find anyone getting results like me, so I just shared my thoughts on how to get it running so that those who are trying this out do not need to reinvent the wheel.

Collect Hardware

Circuit diagram:

Build Distance Measuring System with Arduino UNO and Ultrasonic sensor HC-Sr04

Even though we are going to have one US (ultrasonic) sensor working as a transmitter and the other as a receiver, all four pins of the sensor must be connected to the Arduino. Why do we want? More will be discussed later, but the circuit diagram is as follows

How the HC-SR04 module actually works:

Before we move on, let us know how the HC-SR04 sensor works. The time chart below will help us understand the work.

Build Distance Measuring System with Arduino UNO and Ultrasonic sensor HC-Sr04

The sensor has two pins, Trigger and Echo, to measure the distance, as shown in the timing diagram. To start the measurement first, we should send the ultrasound from the transmitter, which can be done by setting the trigger pin high for 10uS.

Once completed, the transmitter pin will send eight US waves of sound waves. The U.S. wave will hit the target and will be picked up by the receiver.

 Make your HC-SR04 work as a transmitter only:

HC-SR04 can only be used as a transmitter is very simple. As the timing diagram shows, you must declare the trigger pin as an output pin and hold it high for 10 microseconds. This will start the ultrasound bursts.

So, as long as we want to transmit waveforms, we just have to control the transmitter’s trigger pins, as shown below.

Make your HC-SR04 a Receiver only:

Build Distance Measuring System with Arduino UNO and Ultrasonic sensor HC-Sr04

As shown in the timing diagram, we can not control the rise of the Echo pin because it is related to the trigger pin. So we can not make HC-SR04 work as receiver only.

But we can use hacking techniques by simply covering the sensor part of the sensor with tape (as shown in the image below) or by covering American waves that can not escape from its transmitter housing and the Echo pin is unaffected by the U.S. waveforms.

Measure the distance between two ultrasonic sensors (HC-SR04):

The transmitter module and the receiver module are very far apart and when the receiver module receives a U.S. wave from the transmitter module it does not know when the transmitter will transmit this particular wave.

Without knowing the start time, we were unable to calculate the time and distance spent. To solve this problem, the Echo pulse of the receiver module must be exactly high when the transmitter module transmits a US wave.

Note: if you are beginner in Arduino then please have a look at my previous article “Beginner guide for Arduino

Programming code for Receiver part

const int trigPin = 9;

const int echoPin = 10;

// defines variables

long duration;

int distance, Pdistance;

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication

}

void loop() {

Pdistance=distance;

Calc();

distance= duration*0.034;

if (Pdistance==distance || Pdistance==distance+1 || Pdistance==distance-1  )

{

Serial.print(“Measured Distance: “);

Serial.println(distance/2);

}

//Serial.print(“Distance: “);

//Serial.println(distance/2);

delay(500);

}

void Calc()

{

duration=0;

Trigger_US();

while (digitalRead(echoPin)==HIGH);

delay(2);

Trigger_US();

duration = pulseIn(echoPin, HIGH);

}

void Trigger_US()

{

// Fake trigger the US sensor

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

}

Programming code for Transmitter part

// defines pins numbers

const int trigPin = 9;

const int echoPin = 10;

// defines variables

long duration;

int distance;

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication

}

void loop() {

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

delay(2);

}

Serial Monitor Display

Follow the instructions in the program to connect. Then upload the transmitter code and receiver code given below to the transmitter and receiver Arduino, respectively. Just open the serial Monitor and it will  start showing the data of the given sensor between one ultra-sonic module to the another one

Build Distance Measuring System with Arduino UNO and Ultrasonic sensor HC-Sr04

Note: This method is only an ideology and may not be accurate or satisfactory. However, you can try the improvisation below for better results.

Build Distance Measuring System with Arduino UNO and Ultrasonic sensor HC-Sr04

Build Distance Measuring System with Arduino UNO and Ultrasonic sensor HC-Sr04

You may also like to read these articles

How to Hack Car doors and other Wireless Devices using Arduino

Top Best Arduino Board to Buy in 2018