How to program 3 ultrasonic sensors?

First of all, good evening, I want to ask how to program 3 ultrasonic sensors so that when the first and second ultrasonic sensors are active, the third is not active and when the third ultrasonic sensor is active, the first and second ultrasonic sensors are not active. Is my program correct?

const int triggerPin1 = 2;
const int echoPin1 = 3;
const int triggerPin2 = 4;
const int echoPin2 = 5;
const int triggerPin3 = 7;
const int echoPin3 = 8;
const int LED1 = 13;
const int LED2 = 5;
const int LED3 = 6;

long duration;
int distance;

void setup() {
pinMode(triggerPin1, OUTPUT);
pinMode(triggerPin2, OUTPUT);
pinMode(triggerPin3, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(echoPin2, INPUT);
pinMode(echoPin3, INPUT);

Serial.begin(9600);
}

void loop_ultrasonic_1() { // untuk sensor 1
digitalWrite(triggerPin1, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin1, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin1, LOW);
duration = pulseIn(echoPin1, HIGH);
distance = duration*0.034/2;

if(distance > 50){
Serial.println("Jarak lebih dari 50cm");
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}else if(distance <= 50 && distance >= 20){
Serial.println("Jarak berada diantara 20cm hingga 50cm");
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
digitalWrite(LED3, LOW);
}else if(distance < 20){
Serial.println("Jarak kurang dari 20cm");
digitalWrite(LED3, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
}
}

void loop_ultrasonic_2() { // untuk sensor 2
digitalWrite(triggerPin2, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin2, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin2, LOW);
duration = pulseIn(echoPin2, HIGH);
distance = duration*0.034/2;

if(distance > 50){
Serial.println("Jarak lebih dari 50cm");
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}else if(distance <= 50 && duration >= 20){
Serial.println("Jarak berada diantara 20cm hingga 50cm");
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
digitalWrite(LED3, LOW);
}else if(distance < 20){
Serial.println("Jarak kurang dari 20cm");
digitalWrite(LED3, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
}
}

void loop_ultrasonic_3() { // untuk sensor 3
digitalWrite(triggerPin3, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin3, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin3, LOW);
duration = pulseIn(echoPin3, HIGH);
distance = duration*0.034/2;

if(distance > 50){
Serial.println("Jarak lebih dari 50cm");
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
}else if(distance <= 50 && distance >= 20){
Serial.println("Jarak berada diantara 20cm hingga 50cm");
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
digitalWrite(LED3, LOW);
}else if(distance < 20){
Serial.println("Jarak kurang dari 20cm");
digitalWrite(LED3, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
}
}

void loop() {

loop_ultrasonic_1();
loop_ultrasonic_2();
loop_ultrasonic_3();
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

You might be using IDE 2.x but you don't seem to have a problem with it, hence your topic has been moved to a more suitable location on the forum.

Please use code tags when posting code (or error messages); it makes it easier to read, copy and there is no risk that the forum software does not display it properly. Please read How to get the best out of this forum (again?).

Have you looked at the documentation for this call to pulseln()? Each time you call it and there is no immediate echo, there is a ONE SECOND wait to see if an echo ever returns.

In addition, if an echo is returned very late form one sensor, the second sensor will hear it.

You must plan to coordinate your sensor use with the time needed for the sound pulse from one sensor to decay before starting a second pulse.
All a matter of physics!

how far is the sound mirror if sensor not hearing answer in 1 second? 200m? i assume nothing will note such weakened sound impulse.

} else if (distance <= 50 && duration >= 20) {

what?

echoPin2 and LED2 use the same pin ...

In general copying code that provides the same functionality several times is not a good idea. If you want to add something or remove a bug you have to do this at multiple places.

Remember, the sound pulse must travel to the reflector and back to the receiver. So the reflector distance is the speed of sound in microseconds times the pulse length in microseconds, divided by 2.

speed of sound is ~330m/s, halfway is 165m.

Feel free to check this out

It is based on your code but

  • uses a structure for the sensors and an array for the leds
  • uses different pins for the leds
  • switches the leds depending on the closest distance range measured by one of the three sensors
  • prints via Serial only once per distance range change (not with every measurement)
Sketch
/*
  
  Forum: https://forum.arduino.cc/t/how-to-program-3-ultrasonic-sensors/1178011
  Wokwi: https://wokwi.com/projects/378484791065958401

*/

constexpr byte noOfSensors = 3;
constexpr byte noOfLeds    = 3;

struct sensorType {
  byte no;
  byte triggerPin;
  byte echoPin;
  byte mode;
};

sensorType sensor[noOfSensors] = {
  {1, 2, 3, 255},
  {2, 4, 5, 255},
  {3, 7, 8, 255}
};

byte led[noOfLeds] = {9, 10, 11};

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < noOfSensors; i++) {
    pinMode(sensor[i].triggerPin, OUTPUT);
    pinMode(sensor[i].echoPin, INPUT);
  }
  for (int i = 0; i < noOfLeds; i++) {
    pinMode(led[i], OUTPUT);
  }
}

void loop() {
  loop_ultrasonic(sensor[0]);
  loop_ultrasonic(sensor[1]);
  loop_ultrasonic(sensor[2]);
  controlLeds();
}

void loop_ultrasonic(sensorType &thisSensor) {
  unsigned long duration;
  float distance;
  digitalWrite(thisSensor.triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(thisSensor.triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(thisSensor.triggerPin, LOW);
  duration = pulseIn(thisSensor.echoPin, HIGH);
  distance = duration * 0.034 / 2;
  byte newMode = 0;
  if (distance <= 50) newMode = 1;
  if (distance <  20) newMode = 2;
  if (newMode == thisSensor.mode) { // This mode has already been handled!
    return;
  }
  thisSensor.mode = newMode;
  Serial.print("Sensor-No. ");
  Serial.print(thisSensor.no);
  Serial.print("\t");
  switch (newMode) {
    case 0 :  Serial.println("Jarak lebih dari 50cm");
      break;
    case 1 :  Serial.println("Jarak berada diantara 20cm hingga 50cm");
      break;
    case 2 : Serial.println("Jarak kurang dari 20cm");
      break;
  }
}

void controlLeds() {
  byte maxMode = 0;
  for (int i = 0; i < noOfSensors; i++) {
    if (sensor[i].mode != 255 && sensor[i].mode > maxMode) {
      maxMode = sensor[i].mode;
    }
  }
  switch (maxMode) {
    case 0 : 
      switchLeds(HIGH,LOW,LOW);
      break;
    case 1 :
      switchLeds(HIGH,HIGH,LOW);
      break;
    case 2 :
      switchLeds(HIGH,HIGH,HIGH);
      break;
  }
}

void switchLeds(byte state1, byte state2, byte state3) {
  digitalWrite(led[0], state1);
  digitalWrite(led[1], state2);
  digitalWrite(led[2], state3);
}


Good luck and have fun :wink:

The NewPing library has an example showing how to use multiple range sensors.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.