Friday, 12 December 2014

Multiple ultrasonic sensors at once in Arduino


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.

int ledPin = 52; // This pin is for first led and you can change it for any pin you want.
int ledPin2 = 53; // This pin is for second and you can change it for any pin you want.
int ledPin3 = 40; // This pin is for third and you can change it for any pin you want.

int trigPin = 50; // TrigPin of first sensor and you can change it for any pin you want.
int echoPin = 51; // EchoPin of first sensor and you can change it for any pin you want.

int trigPin2 = 48; // TrigPin of second sensor and you can change it for any pin you want.
int echoPin2 = 49; // EchoPin of second sensor and you can change it for any pin you want.

int trigPin3 = 30; // TrigPin of third sensor and you can change it for any pin you want.
int echoPin3 = 31; // EchoPin of third sensor and you can change it for any pin you want.

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 >

Diagram of Ultrasonic Sensors and LEDs

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.

Keywords:
Ultrasonic
Ad

22 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. Thanks for your comment. I'm sorry I'm on the vacation at the moment so I can't really try the code with lcd display but I've alredy written a post similar to what you have asked I think that will help. How to print different variables in a straight line

      Delete
    2. 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
    3. Ok, I'll try make that and let you here as soon as possible.

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

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

    ReplyDelete
    Replies
    1. Thanks for your comment. I'm really sorry I don't have any buzzer to test and check but I did one similar to that with serial mp3 player. Please click here to see the post.

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

    ReplyDelete
    Replies
    1. You are most welcome my friend. Please click on the picture above sketch2 and see it in big form. It's all there, you just need to change the pins as you like to arrange in your one.

      Delete
  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. Need to see your whole sketch then we might be able to say what's wrong with your sketch.

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

    ReplyDelete
    Replies
    1. Hello there, thanks for your comment, Sorry I don't really email anyone anything that's why I thought let me tell you here. You can download the software from the arduino main site https://www.arduino.cc/en/Main/Software and then install it. After that you can upload the sketch (above) to any arduino board by using that software. Sorry I didn't approved your email comment because I didn't want anyone else to spam you. Thanks

      Delete
  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
    Replies
    1. Sorry my friend you came a bit late. If you want to know why please see my All Arduino Posts page.

      Although I could have helped you but I'm kind of disappointed. But still I'm publishing your comment so that if anyone got extra time in his/her hand can help you out. I'm really sorry for not being helpful to you. :(

      Delete
  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
    Replies
    1. My friend your project seems interesting to me and it's not much difficult to make. I could have helped you out but I'm very much disappointed from people. If you want to know why then you'll find our more on All Arduino Posts. I'm still not a rock hearted person so here is small help for you, i hope that helps. When you visit All Arduino post you will find a post called How to make fence/border security system play with that sketch you'll get the idea.

      Delete
  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
    Replies
    1. Did you check the code properly and the pins in the code? Or maybe your ultrasonic sensor is not working. You can first try the ultrasonic sensor (which is not working) separately and then add both together.

      Delete
  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
    Replies
    1. Yes it should work as far as I know. Thanks for your comment. :)

      Delete