For a project I'm doing I need the Arduino to be connected to two echo sensors (I have the HC-SR04). Currently I have 4 sensors that I can use, but I will need only 2 or 3.
I have almost no knowledge of coding and the Arduino, but I figured out how to connect it to one echo sensor, and that is working, which is great.
But now I need it to connect to two. I found example code with 3 echo sensors, but whenever I connect them my Arduino shuts down. I guess it is because it takes up too much power, my teacher said something like that. All lights turn off all of the sudden, and it doesn't reset. Only when I remove some the wires for the power, it works again.
So 3 isn't going to work. I figured I'd stick to two. Now the problem: Whenever I try to edit the code to two echo sensors (I basically just removed the trigPin3
and echoPin3
in the first part of the code, and the SonarSensor(trigPin3, echoPin3))
, it doesn't work anymore. The monitor doesn't show any values anymore, while it did with the first code. It just shows zeroes now.
So my question is, how can I edit the example code so that it works with 2 echo sensors?
This is the code I found for the 3 echo sensors:
#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5
#define trigPin3 7
#define echoPin3 8
long duration, distance, RightSensor,BackSensor,FrontSensor,LeftSensor;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
}
void loop() {
SonarSensor(trigPin1, echoPin1);
RightSensor = distance;
SonarSensor(trigPin2, echoPin2);
LeftSensor = distance;
SonarSensor(trigPin3, echoPin3);
FrontSensor = distance;
Serial.print(LeftSensor);
Serial.print(" – ");
Serial.print(FrontSensor);
Serial.print(" – ");
Serial.println(RightSensor);
}
void SonarSensor(int trigPin,int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
}
And this is the one that IS working, but with 1 echo sensor:
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200;
int minimumRange = 0;
long duration, distance;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
if (distance >= maximumRange || distance <= minimumRange) {
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
} else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
//Delay 50ms before next reading.
delay(50);
}
I hope someone can help me!
Many thanks, Leah.
-
Could you update your question to add the code that you've created to work with two sensors, which doesn't work, please? Could you please also elaborate on what you mean by "doesn't respond anymore"? What exactly does happen?Mark Smith– Mark Smith2017年01月01日 15:14:54 +00:00Commented Jan 1, 2017 at 15:14
-
With "Doesn't respond anymore" I mean, lights turn off completely.L Leah– L Leah2017年01月01日 15:17:39 +00:00Commented Jan 1, 2017 at 15:17
-
Elaborated some things in my question. I hope it's a little bit more clear now.L Leah– L Leah2017年01月01日 15:24:33 +00:00Commented Jan 1, 2017 at 15:24
-
We can easily find working code on our own. What we need is your "not working code". BTW, instead of listing the working code, most simply provide a link to the working code. As most get their code from other people's web pages or sources such as github.st2000– st20002017年01月01日 16:26:29 +00:00Commented Jan 1, 2017 at 16:26
-
There is a magical " delay(50);" that you might have omitted *<:)Mikael Patel– Mikael Patel2017年01月01日 16:29:01 +00:00Commented Jan 1, 2017 at 16:29
3 Answers 3
- In the 1 sensor program all the delays added together account for 62ms between samples.
- In the 3 sensor program the sample code contains delays which likely exceed 12ms (delays plus code execution). So each sensor will not be sampled faster than once every 36ms.
- After you removed 1 sensor from the 3 sensor program, you increased the sampling rate to about every 23ms.
- It is likely a minimum amount of time is necessary between samples in order for the HC-SR04 to recover.
Try this one. I hope this will work for you.
int trigPin1 = 2; //Sensor Trip pin connected to Arduino pin 2
int echoPin1 = 3; //Sensor Echo pin connected to Arduino pin 3
int trigPin2 = 4; //Sensor Trip pin connected to Arduino pin 4
int echoPin2 = 5; //Sensor Echo pin connected to Arduino pin 5
float pingTime1; //time for ping to travel from sensor to target and return
float targetDistance1; //Distance to Target in centi meters
float pingTime2; //time for ping to travel from sensor to target and return
float targetDistance2; //Distance to Target in centi meters
float speedOfSound = 760.5583392985; //Speed of sound in miles per hour.
void setup()
{
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}
void loop()
{
ultrasonicFun1();
ultrasonicFun2();
}
void ultrasonicFun1()
{
digitalWrite(trigPin1, LOW); //Set trigger pin low
delayMicroseconds(2000); //Let signal settle
digitalWrite(trigPin1, HIGH); //Set trigPin high
delayMicroseconds(15); //Delay in high state
digitalWrite(trigPin1, LOW); //ping has now been sent
delayMicroseconds(10); //Delay in high state
pingTime1 = pulseIn(echoPin1, HIGH); //pingTime is presented in microceconds
pingTime1 = pingTime1 / 1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime1 = pingTime1 / 3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
targetDistance1 = speedOfSound * pingTime1; //This will be in miles, since speed of sound was miles per hour
targetDistance1 = targetDistance1 / 2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
targetDistance1 = targetDistance1 * 160934.4; //Convert miles to inches by multipling by 160934.4 (centi meters per mile)
}
void ultrasonicFun2()
{
digitalWrite(trigPin2, LOW); //Set trigger pin low
delayMicroseconds(2000); //Let signal settle
digitalWrite(trigPin2, HIGH); //Set trigPin high
delayMicroseconds(15); //Delay in high state
digitalWrite(trigPin2, LOW); //ping has now been sent
delayMicroseconds(10); //Delay in high state
pingTime2 = pulseIn(echoPin2, HIGH); //pingTime is presented in microceconds
pingTime2 = pingTime2 / 1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime2 = pingTime2 / 3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
targetDistance2 = speedOfSound * pingTime2; //This will be in miles, since speed of sound was miles per hour
targetDistance2 = targetDistance2 / 2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
targetDistance2 = targetDistance2 * 160934.4; //Convert miles to inches by multipling by 160934.4 (centi meters per mile)
}
Try this:
void loop() {
RightSensor = SonarSensor(trigPin1, echoPin1);
LeftSensor = SonarSensor(trigPin2, echoPin2);
FrontSensor = SonarSensor(trigPin3, echoPin3);
Serial.print(LeftSensor);
Serial.print(" – ");
Serial.print(FrontSensor);
Serial.print(" – ");
Serial.println(RightSensor);
}
long SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
return distance;
}
-
Some explanation would be greatduck– duck2017年01月06日 01:10:50 +00:00Commented Jan 6, 2017 at 1:10
-
1As far as I understand, your code does not solve the OP problem... Can you just point out what issue this solves and why?frarugi87– frarugi872017年02月09日 08:45:46 +00:00Commented Feb 9, 2017 at 8:45