Wednesday, 9 March 2016

How to make Master and Slave ultrasonic sensors

Some one wanted to know how to make two ultrasonic sensors work together but one should be given more priority on another.

Well, that's pretty easy. You probably have seen my other post called multiple ultrasonic sensors at once on that post you can see few lines of sketch like below.
Ad

firstsensor();
secondsensor();
thirdsensor();
delay(100);

The above code shows that there are three sensors and they all work at the same time, but obviously the Arduino board collects the data from all the sensors first then prints all of them together at once.

But if you arrange them like this (code below), it will first get the data from first sensor then print it then will go for the second sensor after 500 miliseconds, and you change the delay as you wish.
firstsensor();
delay(500);
secondsensor();
delay(500);
thirdsensor();
delay(500);

So that's one way of giving priority to a sensor, but in this post I want to share a sketch by which you can control one sensor with another sensor. The true master and slave relationship.

Let's see the sketch first.
int ledPin1 = 3;
int ledPin2 = 4;

int trigPin1 = 6;
int echoPin1 = 7;

int trigPin2 = 8;
int echoPin2 = 9;


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



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 loop() {
Serial.println("\n");
  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);
    secondsensor();
    delay(100);
  } else {
    digitalWrite (ledPin1, LOW);
  }   
}

Ad

How does it work?
Well, the first sensor controls the second sensor. For example, when the first sensor picks up any object within 30cm then it will turn on the led1 and the second sensor will start working. But if the first sensor can't find anything withing 30cm then it will turn off the led1 and the second sensor will not work.

But don't think that second sensor is connected to the first led. It's this code "secondsensor();" by which you can control second sensor. The first sensor is controlling both the led1 and second sensor.

That's all for this post if you want the diagram of the sketch than please have a look at top picture. Please add resistors to the leds if you don't want to burn you them out.


Ad


No comments:

Post a Comment