Controlling 2 HC-SR04 sensors

Hy, Let me first tell that I am a complete beginner.
I wanted to controll two HC-SR04 ultrasonic sensors with one controllerboard for an project,
I tryed to do that with this code:

const int trig = 6;
const int echo = 5;

int lamp = 3;
int lamp1 = 9;

const int trig1 = 10;
const int echo1 = 11;

long totaltime;
int distance;

long totaltime1;
int distance1;
void setup ()
{
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
 
  pinMode(lamp, OUTPUT);
  pinMode(lamp1, OUTPUT);

  pinMode(trig1, OUTPUT);
  pinMode(echo1, INPUT);
  Serial.begin(9600);

}

void loop()
{
  digitalWrite(trig, LOW);
  delayMicroseconds(5);
  digitalWrite(trig, HIGH);
  delayMicroseconds (10);
  digitalWrite(trig, LOW);
  
  digitalWrite(trig1, LOW);
  delayMicroseconds (5);
  digitalWrite(trig1, HIGH);
  delayMicroseconds (10);
  digitalWrite(trig1, LOW);

  totaltime = pulseIn(echo, HIGH);
  totaltime1 = pulseIn(echo1,HIGH);

  delay(500);

  distance= (totaltime / 2) / 29.1;
  distance1= (totaltime1 / 2) / 29.1;

  Serial.print("Distance:");
  Serial.println(distance);


  if (distance > 10)
  {
    digitalWrite (lamp, HIGH);
  }
  else
  {
    digitalWrite(lamp, LOW);
  }

  if (distance1 > 10);
  {
    digitalWrite (lamp1, HIGH);
  }
}

And yes, I put a '1' after every piece of code for the second sensor because I tought that that would be the most logic way to to this, but it does not work.
so, What did I wrong? or is there a better way?

That is a poor description of the problem

What is stopping the second sensor picking up the signal from the first one ?

no it's reading nothing, It's just giving zero's

Start by using only one sensor in the code and circuit

Does that work ?

yes

Post your code that works with a single sensor

const int trig = 6;
const int echo = 5;

int lamp = 3;

long totaltime;
int distance;

void setup ()
{
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
 
  pinMode(lamp, OUTPUT);
  Serial.begin(9600);

}

void loop()
{
  digitalWrite(trig, LOW);
  delayMicroseconds(5);
  digitalWrite(trig, HIGH);
  delayMicroseconds (10);
  digitalWrite(trig, LOW);
  
  totaltime = pulseIn(echo, HIGH);

  delay(500);

  distance= (totaltime / 2) / 29.1;
  distance1= (totaltime1 / 2) / 29.1;

  Serial.print("Distance:");
  Serial.println(distance);


  if (distance > 10)
  {
    digitalWrite (lamp, HIGH);
  }
  else
  {
    digitalWrite(lamp, LOW);
  }
}

You need to trigger them one at a time, with a short delay between them, so the echos from the first one don't get read by the second one.

Does this code (Post 7) show a valid distance with each sensor?

Should we suggest him using ultra sonic sensor library or let him learn by this one what you guys suggest

Why? Make a pulse train, read a pulse train. What else is needed?

It would make him easy to handle multiple sensors .If he using only 2 then i think it would be fine

How many do you count? What does the topic say?

Yeah i know , It was just my suggestion that's all

I'll Made few changes to the code . I'll hope it helps

const int trig = 6;
const int echo = 5;

int lamp = 3;
int lamp1 = 9;

const int trig1 = 10;
const int echo1 = 11;

long totaltime;
int distance;

long totaltime1;
int distance1;
void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

  pinMode(lamp, OUTPUT);
  pinMode(lamp1, OUTPUT);

  pinMode(trig1, OUTPUT);
  pinMode(echo1, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  digitalWrite(trig, HIGH);
  delayMicroseconds(20);
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
  totaltime = pulseIn(echo, HIGH);

  digitalWrite(trig1, LOW);
  delayMicroseconds(2);
  digitalWrite(trig1, HIGH);
  delayMicroseconds(20);
  digitalWrite(trig1, LOW);
  delayMicroseconds(2);
  totaltime1 = pulseIn(echo1, HIGH);

  delay(100);

  distance = (totaltime / 2) / 29.1;
  Serial.print("Distance:");
  Serial.println(distance);

  distance1 = (totaltime1 / 2) / 29.1;
  Serial.print("Distance1:");
  Serial.println(distance1);



  if (distance > 10) {
    digitalWrite(lamp, HIGH);
  } else {
    digitalWrite(lamp, LOW);
  }

  if (distance1 > 10)
  {
    digitalWrite(lamp1, HIGH);
  } else {
    digitalWrite(lamp1, LOW);
 }
}

Try NewPing library, multiple sensor examples included :
https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home#!ping-3-sensors-sketch

You deleted half the code and no library to help?

Thank you for spotting the missing lines. As per your suggestion i didn't used any library just changed the order of calling the pulseIN

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