pcbway

ultrasonic sensors with arduino, ultrasonic sensor code for arduino,hcsr04 ultrasonic sensor arduino, sonar sensor with arduinoHello friends, hope you are having fun and enjoying life. Today, I am gonna post about interfacing of multiple Ultrasonic sensor with Arduino. In the previous post, we have seen Interfacing of Ultrasonic Sensor With Arduino and in this post I have interfaced single ultrasonic sensor but in projects especially related to robotics, we have to interface multiple ultrasonic sensors. For example you have an obstacle detection robot, now in order to detect obstacle in front of robot you have to place once sensor on the front side but now you can’t detect any object present on left or right side of your robot, so you have to place two sensors one on the left side of robot and one on the right side so in this project you need to use total three ultrasonic sensors, one on the front, one on left and one on right side of robot. Similarly, in another project I have to move the robot in a maze having walls on the side of robots, and my task was to move the robot straight within these walls without hitting the walls. In that case, I also used two ultrasonic sensors on both sides of robot and then applied PID algorithm in order to avoid hitting the walls. So, in short its a common practice to use multiple ultrasonic sensor with arduino and today we are gonna have a look at how to do it.

I have posted about the basics of Ultrasonic sensor and how it works in my previous post so I am not gonna go into that detail. If you haven’t read it then I recommend that you should first read Interfacing of Ultrasonic sensor with Arduino. Now, let’s get started with Interfacing of multiple ultrasonic sensor with arduino, which isn’t that difficult. 🙂

Note:

Interfacing of Multiple Ultrasonic Sensor With Arduino

  • Let me first summarize the working of ultrasonic sensor again. With ultrasonic sensor, what we need to do is to generate a trigger signal on its trigger pin for around 10 microsecond.
  • As soon as the ultrasonic sensor gets this trigger signal, it sends out an ultrasonic signal.
  • This ultrasonic signal then hits something and bounced back.
  • Now, in order to check this bouncing signal, we have to read the Echo pin and check for how long it remains HIGH, and on the basis of this duration we calculate our distance with the object.
  • This is the process for single ultrasonic sensor and when we are using multiple ultrasonic sensors, what we need to do is simply repeat the whole procedure for all the sensors one by one.
  • First of all, we will generate the trigger pulse for first sensor and the read its echo pin and get the distance, then we generate the trigger pulse for second sensor and read its echo pin and so on for the third.
  • So, here I am gonna use three ultrasonic sensor and the circuit diagram is shown below:

ultrasonic sensors with arduino, ultrasonic sensor code for arduino,hcsr04 ultrasonic sensor arduino, sonar sensor with arduino

  • I have tried my best while designing this image to make it simple but as there are too much wires so it has become a little complex.
  • I am pointing out the pin configuration here so it will be easy for you to interface your sensors with arduino. The pin configuration is as follows:
    • Vcc of all sensors will go into +5V of Arduino.
    • GND of all sensors will go into GND of Arduino.
    • Trig Pin of first sensor into Pin # 3 of Arduino.
    • Echho Pin of first sensor into Pin # 2 of Arduino.
    • Trig Pin of second sensor into Pin # 4 of Arduino.
    • Echo pin of second sensor into Pin # 5 of Arduino.
    • Trig Pin of third sensor into Pin # 7 of Arduino.
    • Echo pin of third sensor into Pin # 8 of Arduino.
  • After connecting the pins as discussed above, now copy the below code and upload it in your arduino board.
  • After uploading the code in your arduino, open the Serial Terminal of Arduino software and you will start receiving the distances for all the three sensors.

#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5
#define trigPin3 7
#define echoPin3 8

long duration, distance, RightSensor,BackSensor,FrontSensor,LeftSensor;

void setup()
{
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
}

void loop() {
SonarSensor(trigPin1, echoPin1);
RightSensor = distance;
SonarSensor(trigPin2, echoPin2);
LeftSensor = distance;
SonarSensor(trigPin3, echoPin3);
FrontSensor = distance;

Serial.print(LeftSensor);
Serial.print(” – “);
Serial.print(FrontSensor);
Serial.print(” – “);
Serial.println(RightSensor);
}

void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

}

  • The code is quite similar to the one we used while interfacing single ultrasonic sensor with arduino, the only thing we changed here is the repetition.
  • Before, we were using the same function SonarSensor() but calling it only once for our single sensor interfaced with arduino but now we are calling it three times for all the three sensors.
  • Its kind of a generic code, you can interface more sensors with it if you want and what you need to do is only calling this function for the next interfaced sensor.

That’s all for today, I think we have posted a lot on the ultrasonic sensor so I am not gonna post any more tutorial on this sensor and now I will start writing on some other sensor. Thanks for reading and share it with your friends and help us grow. 🙂

29 Comments

  1. hello. Can I ask? I would like to apply to your coding to combine the left and front sensor using OR(||), so if a person pass by this 2 sensor, it will light up the LED to give a high. How do I do that? Any example?

  2. Hello. Your coding is awesome. I finally found what I need. But, how do I implement the codes were by the front and left sensor can detect the distance and then light up the LED. Can you help me with this? I have tried many ways to modify the codings but it is not reading the codes

  3. i have interfaced a single ultrasound sensor(hcsr04) and got correct measurement of the distance no problem.
    But when there is no object then too the led’s display some huge value (may be the max limit of the sensor i.e 400cm)
    I have written code in 8051 c language.
    did u get the same problem?

  4. here is the code for two sensor interface
    i have not called the function twice.
    But i would like to discuss on logic what i have written in order to detect two sensors interface
    and here is the code only a part of main code and not any initialization or delay subroutines shown
    1)while(1)
    2){
    3)trig1=1;
    4)trig2=1;
    5)delay(1);
    6)trig1=0;
    7)trig2=0;
    8)while(echo1==0&&echo2==0);
    9)TR0=1;
    10)while(echo1!=0||echo2!=0);
    11)TR0=0;
    12)DPH=TH0;
    13)DPL=TL0;
    14)if(DPTR<30000)
    15) range=DPTR*0.01847091;
    16)else
    17) range=0;
    18)if(range<100)
    19)P2=range;
    20)else
    21)P2=0X00;
    22)leddelay(999);
    23)}

    line no 10 is the one i would like to discuss.
    in this i have checked for which of the two sensors go low at first whether the left or right whichever goes low first the distance is calculated and checked if within the max limit if not within the max limit then no object detected.
    if say left sensor goes low first then robot should turn left first otherwise right.
    But if both sensor go low together that means object right in front of the robot hence can move accordingly. Is this logic ok?

  5. Salam brother Syed Zain Nasir

    thank you for your informative tutorial. I need to use similar case to control my robot to avoid obstacles. I also have three ultrasonic sensors. Just in your code how I can know if the distance is related to left sensor so a robot can turn right for example or for right sensor or even for front sensor.

    Many thanks in advance

  6. Hello .I met some problem with HC-SR 04 . Could you give me a hand?
    does it possible use Multiple Ultrasonic Sensor to communicate each others ?
    Ex : A ,B two sensors
    A trig and B echo
    not A trig A echo B trig B echo …etc

    the result that i get always B trig & B echo.
    A never do any work. ..

  7. Dear Syed,

    We are a group of students from the Netherlands, end working on an autonomic wheelchair. we are using your guide for multiple ultrasonic censors as an part for the project. the problem we encountered is that, if we use more than three ultrasonic censors. the system is way too slow to use, and become even slower when the distant increases.
    we want the program to print the values in the monitor. do you have any idea’s what the reason is and how we eventually can solve it.

    we thank you for your guide and hope you can help us out with this one.

    • I just wanted to ask the same question. I think it would be possible to connect all triggers to one common pin and then process signal just from appropriate echo pin. Has anybody tested it? ?

      • No, its not possible. When you trigger from same pin then you can’t get all the echo pins at the same time. So, use separate pins.

        • Why cannot you use one trigger, but separate echo pins? You trigger all the senders at the same time, but you read only the echo that you want, it should save some wiring, and the code is a little simpler as well.

  8. Hey. I’m doing a project which also involves 2 Ultrasonic Sensors. My problem is that every time I try on running both sensors simultaneously I always get a “0” distance in one of the sensors. The first thing that I hypothesized is that the power in my arduino isn’t enough to drive both Ultrasonic sensors, but according to all my research regarding the problem, the arduino shouldn’t have a problem with driving 2 US. What do you think could be the problem in my project? Should I not position the sensors side by side in order for the pulse of one sensor interferes with the echo of the other sensor? I’m stuck with this problem. Any feedback from you would be greatly appreciated. Thank you!

  9. Hello..we have a project using srf-04..The set up is almost the same to yours removing the rightmost sensor. We are able to measure distance . However, for initial set up, they must have same distance to be measured..but the reading has difference( for e.g. 2cm for the first sensor while 5.06 cm to the other one) and they keep on changing..Also, the sensor are measuring negative values for distance. Both sensor are touching the tube with 1 cm diameter..Why is the sensor gives inaccurate measurement? Thank you 🙂

  10. can i add 1 additional sensor, 4 LEDs on each sensor as indicator, 1 LCD display and buzzer? do the connections on arduino would be enough? PLEASE HELP. Thank you so much. 🙂

  11. Hey, if not measuring distance but just for motion detection, can I switch ON all the Sensors at the same time to increase the directivity of detection? will there be any issue related to interference?

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment