Please try not to use adblocker for this small site. Thanks

Friday, 12 December 2014

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

17 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
  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



NOTE: PLEASE DO NOT SPAM. BY SPAMMING I MEAN LEAVING COMMENT WITH A BACK LINK. I SEE MANY SHAMELESS SPAMMERS STILL TRY TO LEAVE COMMENTS WITH BACK LINKS. FOR THEM I JUST WANT TO SAY COULD YOU PLEASE F**K OFF FROM MY SITE. YOUR (SPAMMER) COMMENT WILL BE REMOVED AS SOON AS I NOTICE IT. Thanks