Ultrasonic Sensor Help

Hi All!

I’m having trouble with some code and I have no idea why its not working! Its the bog standard code for the HC-SR04 Ultrasonic sensors with the SMS library attached to communicate to the computer via the Serial Bus:

ORIGINAL CODE

#include <SimpleMessageSystem.h>

const int trigPin = 2;
const int echoPin = 4;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// read the value from the sensor
messageSendChar(‘a’); // send the value in a message
// messageSendInt(millis()); // along with a timestamp
messageSendInt(duration);
messageEnd();

delay(1000);

}

However, when I come to add another sensor, the timing of the data returning lags and the second output returns 0 over and over; so the serial bus look like this : [a, 11345, 0]
When I unplug the second sensor though, I then get a number coming through instead of 0.

I’ve tested all the components and my wiring and they are all fine; so the problem must ne in my code. All I did though was copy and paste the original code with new variables so I have no idea how it has caused an error.

MY CODE:

#include <SimpleMessageSystem.h>

const int trigPin = 2;
const int echoPin = 4;

const int trigPin2 = 6;
const int echoPin2 = 8;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, duration2, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
pinMode(trigPin2, OUTPUT);

digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
digitalWrite(trigPin2, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print(“cm”);
Serial.println();

// read the value from the sensor
messageSendChar(‘a’); // send the value in a message
// messageSendInt(millis()); // along with a timestamp
messageSendInt(duration);
messageSendInt(duration2);
messageEnd();

delay(1000);

}

If someone can spot the problem I would be really grateful as this project has been giving me headaches for days!

Thanks in advance!

Jack

You can't run two ultrasonic sensors at the same time, which is what you're doing with that interleaved code, unless they are physically separated. The second ping will get nothing because it's echo apparently came back while the first pulsin was occuring.You need to run one, then the other and even then you might want to wait a little for echoes to die down before you invoke the second.

Edit: Correction on diagnosis

wildbill:
You can't run two ultrasonic sensors at the same time, which is what you're doing with that interleaved code, unless they are physically separated. The second ping will get nothing because it's echo apparently came back while the first pulsin was occuring.You need to run one, then the other and even then you might want to wait a little for echoes to die down before you invoke the second.

Edit: Correction on diagnosis

Ah! That makes sense :smiley: So would something like this be appropriate?

void loop()
{

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

delayMicroseconds(10);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

delayMicroseconds(10);

pinMode(trigPin2, OUTPUT);

digitalWrite(trigPin2, LOW);

delayMicroseconds(2);

digitalWrite(trigPin2, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin2, LOW);

pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);

Thanks again!

Jack

That looks better. You may need to experiment with the delay between the two pings a bit. I'd be inclined to make it substantially larger initially until you have it working.

Hi wildbill!

I’ve adjusted the code for the 4 sensors, however the first isn’t coming in even though the 3 after it are. I can’t see what’s going wrong in the code as I’ve tried different wait times and still got nothing through. Could you take a look?

#include <SimpleMessageSystem.h>

const int trigPin = 0;
const int echoPin = 1;

const int trigPin2 = 3;
const int echoPin2 = 4;

const int trigPin3 = 5;
const int echoPin3 = 6;

const int trigPin4 = 7;
const int echoPin4 = 8;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, duration2, duration3, duration4, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

delayMicroseconds(10);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

delayMicroseconds(15);

pinMode(trigPin2, OUTPUT);

digitalWrite(trigPin2, LOW);

delayMicroseconds(2);

digitalWrite(trigPin2, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin2, LOW);

pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);

delayMicroseconds(15);

pinMode(trigPin3, OUTPUT);

digitalWrite(trigPin3, LOW);

delayMicroseconds(2);

digitalWrite(trigPin3, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin3, LOW);

pinMode(echoPin3, INPUT);
duration3 = pulseIn(echoPin3, HIGH);

delayMicroseconds(15);

pinMode(trigPin4, OUTPUT);

digitalWrite(trigPin4, LOW);

delayMicroseconds(2);

digitalWrite(trigPin4, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin4, LOW);

pinMode(echoPin4, INPUT);
duration4 = pulseIn(echoPin4, HIGH);

delayMicroseconds(15);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print(“cm”);
Serial.println();

// read the value from the sensor
messageSendChar(‘a’); // send the value in a message
messageSendInt(duration);
messageSendInt(duration2);
messageSendInt(duration3);
messageSendInt(duration4);
messageEnd();

delay(500);

}

Thanks again,

Jack

Pins 0 and 1 are used by the serial port. Since you are using it to send messages back to the terminal program in the IDE, you can't also use them to run a ping sensor.

Hi wildbill,

… I am a knob … forgot about that and that’s sorted the lag problem as well! Thanks so much for your help!!

All the best,

Jack