Multiple Ultrasonic Sensor Arduino with code


Hi, In this post I'm going to show you how you can control 3 leds with 3 ultrasonic sensors at once in Arduino. I was actually looking for this sketch all over the internet and I couldn't find how I wanted. 

Finally I found someone who was looking for similar sketch but he was looking for a sketch that control servo motor and the sketch was not working for him. So I thought let me modify this sketch and try for my own project, then I modified it and it started working. 


The sketch1 is below:

int ledPin = 52;
int ledPin2 = 53;
int ledPin3 = 40;
int trigPin = 50;
int echoPin = 51;
int trigPin2 = 48;
int echoPin2 = 49;
int trigPin3 = 30;
int echoPin3 = 31;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}

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

      Serial.print(distance);  
      Serial.print("cm");
      Serial.println();

  if (distance < 30) {  // Change the number for long or short distances.
    digitalWrite (ledPin, HIGH);
  } else {
    digitalWrite (ledPin, LOW);
  }

    int duration2, distance2;
    digitalWrite (trigPin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin2, LOW);
    duration = pulseIn (echoPin2, HIGH);
    distance2 = (duration/2) / 29.1;
   
      Serial.print(distance2);  
      Serial.print("cm");
      Serial.println();
   
    if (distance2 < 20) {  // Change the number for long or short distances.
      digitalWrite (ledPin2, HIGH);
    }
 else {
      digitalWrite (ledPin2, LOW);
    }

    int duration3, distance3;
    digitalWrite (trigPin3, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin3, LOW);
    duration = pulseIn (echoPin3, HIGH);
    distance3 = (duration/2) / 29.1;
   
      Serial.print(distance3);  
      Serial.print("cm");
      Serial.println();
   
    if (distance3 < 10) {  // Change the number for long or short distances.
      digitalWrite (ledPin3, HIGH);
    }
 else {
      digitalWrite (ledPin3, LOW);
    }

  }
// Code ends here.

 Note: The pin numbers you see here are way to high you might think. Well, that's because I've used Arduino Mega in this project and it has way too many pins.

 Allocating pins:

int ledPin = 52; // This pin is for first led.
int ledPin2 = 53; // This pin is for second led.
int ledPin3 = 40; // This pin is for third led.

int trigPin = 50; // Trigger Pin of first sensor.
int echoPin = 51; // Echo Pin of first sensor.

int trigPin2 = 48; // Trigger Pin of second sensor.
int echoPin2 = 49; // EchoPin of second sensor.

int trigPin3 = 30; // Trigger Pin of third sensort.
int echoPin3 = 31; // Echo Pin of third sensor.

You can change the pins to which ever you may like,
but please keep in mind which pin you are allocating to what.


The green highlighted arrow < means that if any object is less than what number you put after the arrow, then the led will turn on. You can make it opposite as well just by putting >. And that will mean, any number you put after the arrow the led will turn on at.


Special thanks for visiting this page! :)


Diagram of Ultrasonic Sensors and LEDs. Remember to add resistors to leds to limit the current flow.

Ad

Sketch2

int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;

int trigPin1 = 6;
int echoPin1 = 7;

int trigPin2 = 8;
int echoPin2 = 9;

int trigPin3 = 10;
int echoPin3 = 11;

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

void firstsensor(){ // This function is for first sensor.
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

      Serial.print("1st Sensor: ");
      Serial.print(distance1); 
      Serial.print("cm    ");

  if (distance1 < 30) {  // Change the number for long or short distances.
    digitalWrite (ledPin1, HIGH);
  } else {
    digitalWrite (ledPin1, LOW);
  }   
}
void secondsensor(){ // This function is for second sensor.
    int duration2, distance2;
    digitalWrite (trigPin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin2, LOW);
    duration2 = pulseIn (echoPin2, HIGH);
    distance2 = (duration2/2) / 29.1;
 
      Serial.print("2nd Sensor: ");
      Serial.print(distance2); 
      Serial.print("cm    ");
  
    if (distance2 < 20) {  // Change the number for long or short distances.
      digitalWrite (ledPin2, HIGH);
    }
 else {
      digitalWrite (ledPin2, LOW);
    }   
}
void thirdsensor(){ // This function is for third sensor.
    int duration3, distance3;
    digitalWrite (trigPin3, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin3, LOW);
    duration3 = pulseIn (echoPin3, HIGH);
    distance3 = (duration3/2) / 29.1;

      Serial.print("3rd Sensor: ");  
      Serial.print(distance3); 
      Serial.print("cm");
  
    if (distance3 < 10) {  // Change the number for long or short distances.
      digitalWrite (ledPin3, HIGH);
    }
 else {
      digitalWrite (ledPin3, LOW);
    }  
}

void loop() {
Serial.println("\n");
firstsensor();
secondsensor();
thirdsensor();
delay(100);
}


Note:
The sketch2 has been updated later. Even though the sketch1 works but to make it more easier I've written the sketch2 and tested it properly and I'm very happy with its result. It prints the distances and controls the leds very fast without any problem. I hope it helps you :) Best wishes for your project!

You might be also interested in:
How to make Master and Slave ultrasonic sensors

Ad

........................................................
3 pin ultrasonic sensor, 3 pin ultrasonic sensor arduino, 3 pin ultrasonic sensor arduino code, 3 ultrasonic sensor arduino, 3 ultrasonic sensor arduino code, 4 pin ultrasonic sensor arduino code, 4 ultrasonic sensor arduino code, arduino - ultrasonic sensor with led, arduino - ultrasonic sensor with led and buzzer, arduino code for multiple sensors, arduino code for multiple ultrasonic sensor, arduino code for two ultrasonic sensor, arduino code for ultrasonic sensor, arduino code for ultrasonic sensor and buzzer, arduino code for ultrasonic sensor with led
arduino code ultrasonic sensor, arduino distance detector with a buzzer and leds, arduino distance sensor code, arduino multiple sensors, arduino multiple ultrasonic sensor, arduino multiple ultrasonic sensors, arduino sonar code, arduino sonar sensor code, arduino two ultrasonic sensors, arduino ultrasonic program. arduino ultrasonic sensor 3 pin, arduino ultrasonic sensor buzzer, arduino 

ultrasonic sensor code, arduino ultrasonic sensor led, arduino ultrasonic sensor led code, arduino ultrasonic sensor program, arduino ultrasonic sensor tutorial, arduino ultrasonic sensor with buzzer, arduino ultrasonic sensor with buzzer and leds, arduino ultrasonic sensor with led, arduino ultrasonico multiple, arduino uno ultrasonic sensor, arduino uno ultrasonic sensor code, connect ultrasonic sensor to arduino, connecting multiple sensors to arduino uno, connecting multiple sensors to arduino uno code, how to combine multiple sensors with arduino, how to connect 3 ultrasonic sensor arduino, how to connect multiple ultrasonic sensors to arduino, how to connect two ultrasonic sensors to arduino, how to connect two ultrasonic sensors to arduino uno, how to connect ultrasonic sensor to arduino uno, how to use 2 ultrasonic sensors with arduino, how to use two ultrasonic sensors with arduino.

interfacing multiple ultrasonic sensors with arduino, interfacing of ultrasonic sensor with arduino, interfacing ultrasonic sensor with arduino code, long range ultrasonic sensor arduino, multi ultrasonic sensor arduino, multiple hc-sr04 arduino, multiple sensor arduino code, multiple ultrasonic sensor, multiple ultrasonic sensor arduino, multiple ultrasonic sensor arduino code, multiple ultrasonic sensor at once in arduino, multiple ultrasonic sensors, multiple ultrasonic sensors arduino, multiple ultrasonic sensors arduino code, ping sensor arduino code, program arduino 2 sensor ultrasonik, sonar sensor arduino code, three ultrasonic sensor arduino, two ultrasonic sensor arduino, two ultrasonic sensor arduino code, ultrasonic arduino code, ultrasonic arduino program, ultrasonic code for arduino, ultrasonic sensor and led arduino, ultrasonic sensor arduino, ultrasonic sensor arduino circuit, ultrasonic sensor arduino code, ultrasonic sensor arduino code for obstacle detection, ultrasonic sensor arduino code led.

ultrasonic sensor arduino connection, ultrasonic sensor arduino led, ultrasonic sensor arduino nano, ultrasonic sensor arduino project, ultrasonic sensor arduino project code, ultrasonic sensor arduino tutorial, ultrasonic sensor arduino with buzzer, ultrasonic sensor arduino โค้ด, ultrasonic sensor code, ultrasonic sensor code arduino, ultrasonic sensor code for arduino, ultrasonic sensor coding, ultrasonic sensor dc motor arduino code, ultrasonic sensor interfacing with arduino, ultrasonic sensor interfacing with arduino uno, ultrasonic sensor to arduino, ultrasonic sensor with arduino code, ultrasonic sensor with buzzer arduino, ultrasonic sensor with buzzer arduino code, ultrasonic sensor, with servo motor code, ultrasonic sensors arduino, ultrasonic sensors for arduino, using 2 ultrasonic sensor arduino, using multiple ultrasonic sensors arduino, using two ultrasonic sensors arduino

30 comments:

  1. hi, thanks for the code, as im a noob at coding i need your help:)
    The code works fine, but is it possible to get the value from the senors to an lcd 20x4, one sensor on on row, i need total 4 senors showing up on the lcd.

    Thanks

    ReplyDelete
    Replies
    1. can you please give a code for two ultrasonic sensors that alerts you of distance by 1 led and then if the object is further close it lights a different led and as well as a piezo buzzer

      Delete
  2. thank you very much, it is perfekt :)

    ReplyDelete
  3. can u show coding with added a buzzer?

    ReplyDelete
  4. hey thanks for the code can i have the circuit connections for the arrangement

    ReplyDelete
  5. hola, lo he probado, pero me da error las declaraciones de los LEd:


    int ledPin1 = 3;
    int ledPin2 = 4;
    int ledPin3 = 5;
    ¿como se puede corregir?

    ReplyDelete
    Replies
    1. jajaja solo cambia el nombre de cada pin

      Delete
  6. hey can you send me a link or email me how you wired everything up

    ReplyDelete
  7. Hey.... I am a arduino beginner.... I don't know head or tail of this programming stuff of arduino.... I just know to copy it to the ide and upload it... I'm searching for a exactly same code... But for a arduino uno and using only 2 sensors and using buzzers instead of leds..... So I need ur help to know how to go about all this... Please reply as fast as possible

    ReplyDelete
  8. hi there, I am making a WARMAN robot which is my universities project, it needs to go straight and then sense the obstacle and turn right, then goes straight and then needs to turn left after sensing the second obstacle which is on the left side where i have the second sensor, can you please help me with the code? i am using the same sensors, Dc motors H bridge and UNO.
    I am unable to form a loop

    ReplyDelete
  9. Thanks for the code friend...Actually I have connected my second ultrasonic sensor on the same breadboard but it didn't calculate any distances..it continuously giving me zero result...I have already checked my connections twice but it didn't work....so i want to know the cases in which it happens the same..Means causes for that problem

    ReplyDelete
  10. Hi, first of all thank u Soo much for the code, I loved it. I have a question can we use am Arduino UNO with the sketch2 to make the circuit?
    Hope u'll respond

    ReplyDelete
  11. Which arduino are you used here????
    Could u give me the diagram of connections with arduino uno (I mean connection with pin nos)

    ReplyDelete
  12. can i require any resistor for 1.5v
    led

    ReplyDelete
  13. Will i be able to connect both IR and UV sensors together in Arduino??
    If then please leave a comment on that
    :)

    ReplyDelete
  14. Thanks for the code, but, how I can set the lighting time of the leds to stay ON for 20 seconds? Y want to make my home stairs automated.

    ReplyDelete
  15. Sir can we use buzzer instead of LED

    ReplyDelete
  16. Thanks for the code friend.. I am using two sensors in that first sensor give all values but my second ultrasonic sensor didn't calculate any distances..it continuously giving me zero result...I have already checked my connections twice but it didn't work my code
    const int trigPin1=A0;// 5;
    const int echoPin1 = A1;//6;
    const int trigPin2= A4;//7;
    const int echoPin2 = A5;//8;
    //.....................................................................Utrasonic sensor Initilisation....................................................
    long duration1, inches1, cm1;
    long duration2, inches2, cm2;
    pinMode(trigPin1, OUTPUT); // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
    digitalWrite(trigPin1, LOW); // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
    delayMicroseconds(2);
    digitalWrite(trigPin1, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin1, LOW);

    pinMode(trigPin2, OUTPUT); // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
    digitalWrite(trigPin2, LOW); // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
    delayMicroseconds(2);
    digitalWrite(trigPin2, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin2, LOW);
    //.....................................................................Utrasonic sensor1 Program....................................................
    pinMode(echoPin1, INPUT); // Read the signal from the sensor: 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.
    duration1 = pulseIn(echoPin1, HIGH);

    inches1 = microsecondsToInches(duration1); // convert the time into a distance
    cm1 = microsecondsToCentimeters(duration1);
    Serial.println("_________________________________________________________________________________________________________");//print on Serial monitor
    Serial.print("Sensor First Output:-");
    Serial.print("Duration1 :- ");
    Serial.print(inches1);
    Serial.print("In, ");
    Serial.print(" Distance1:- ");
    Serial.print(cm1);
    Serial.print("cm");
    Serial.println();
    Serial.println();

    delay(2000);

    //.....................................................................Utrasonic sensor2 Program....................................................
    pinMode(echoPin2, INPUT); // Read the signal from the sensor: 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.
    duration2 = pulseIn(echoPin2, HIGH);

    inches2 = microsecondsToInches(duration2); // convert the time into a distance
    cm2 = microsecondsToCentimeters(duration2);
    // Serial.println("_________________________________________________________________________________________________________");//print on Serial monitor
    Serial.print("Sensor Second Output:-");
    Serial.print("Duration2 :- ");
    Serial.print(inches2);
    Serial.print("In, ");
    Serial.print(" Distance2:- ");
    Serial.print(cm2);
    Serial.print("cm");
    Serial.println();
    Serial.println();

    delay(2000);
    }
    please give suggestion

    ReplyDelete
  17. can you post a pic of the bread board pleaseeee

    ReplyDelete
  18. i really need to know how the breadboard looks please

    ReplyDelete
    Replies
    1. Sorry for disappointing you. You can read All Arduino Posts for the reason why I'm not interested in replying.

      Delete
  19. While it work if I replace one led with a moter , or should I chang the coad

    ReplyDelete
  20. Which type of jumper wire I should use in this project ? (Male to male or female to male..)

    ReplyDelete
  21. hello this code works perfect but i want to know that is it working in nano after changing the code to the right port does it gets a problem wish you will reply soon

    ReplyDelete



Newly posted:
Reason why rich getting richer and poor getting poorer