How can I control 3 relay module in 3 ultrasonic sensor

////how can I control 3 relay module in 3 ultrasonic sensor

For the relay, write a program to change a DIO pin from HIGH to LOW (or LOW to HIGH, depending on the relay) to activate the relay. Search "arduino relay"

For the ultrasonic sensor, write a program to make a HIGH pulse with a 10 microseconds width, then measure the return time to discover the distance. Search "arduino ultrasonic sensor"

You can practice with simulations on WOKWI.COM

how sir? what is the code

You must write it.

Search "arduino ultrasonic sensor"
Search "arduino relay"


int relayPin1 = 2;
int relayPin2 = 3;
int relayPin3 = 4;

int trigPin1 = 11;
int echoPin1 = 10;

int trigPin2 = 9;
int echoPin2 = 8;

int trigPin3 = 7;
int echoPin3 = 6;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
 
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
 
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
 
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, 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 < 10) {  // Change the number for long or short distances.
    digitalWrite (relayPin1, HIGH);
  } else {
    digitalWrite (relayPin1, 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 < 10) {  // Change the number for long or short distances.
      digitalWrite (relayPin2, HIGH);
    }
 else {
      digitalWrite (relayPin2, 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 (relayPin3, HIGH);
    }
 else {
      digitalWrite (relayPin3, LOW);
    }  
}

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

I try this but it's not working

Check the correct data types for the variables used.

i don't know how

2 relay shot down, only one relay is working

The program works (relays turn on at 10cm). You connected the devices wrong.

no, the device is connected correctly.

I am certain your connections are not correct. The sketch works.

Show a wiring diagram.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you please post some pictures of your project?
So we can see your component layout.

Are you using chatGPT?

Tom... :grinning: :coffee: :+1: :australia:

image

Hi,
Can I suggest you use the NewPing library for your ultrasonics.
It even has an example for more than one ultrasonic unit.

It will make your code simpler.

Have you managed to write code for ONE ultrasonic unit ON ITS OWN, no relay and get it working?
If not then I would suggest you do so and build your project in stages, rather than write the whole code at once and try and debug.

Tom.. :grinning: :+1: :coffee: :australia:


in simulation it's working, but in my output only 1 sensor is working the other 2 is not

Sorry, does that mean in your simulation, only one is working and the other 2 are not?

You will need to put some time delay between pinging each sensor to allow for any other echoes from each previous sensor ping to subside.

So you haven't built this in the real world yet?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

depending on

  • in which directions your three ultra-sonic snsors "look" and send the ultrasonic pulse
  • what room you are in and how good the walls reflect ultrasonic pulses

it might be that the pulses disturb each other.

You have to wait at least 50 milliseconds between each sensor.

  • sending a pulse from sensor 1
    wait 50 milliseconds

  • sending a pulse from sensor 2
    wait 50 milliseconds

  • sending a pulse from sensor 3
    wait 50 milliseconds

What you have posted is a picture of your real setup.
Look close at this picture. If this picture would be the only information to wire everything
would you be able to wire it correctly? Surely not.

How should any user here check if your wiring is correct?

A hand drawn circuit = schematic that shows how you have things wired together

1 Like

in my simulation all sensor is working, but in actual only one sensor working and the 2 sensor are not