How to connect two ultrasonic sensors with one lcd display?

I want to simultaneously connect two of the sensors with the lcd display in Arduino?

Just do it.

Thanks bro

I'm working from memory here, but:

Make as many sensors as you need, calling them say sonar1 and sonar2 on whatever pins you choose.

Then use sonar1.ping and sonar2.ping to read the distances into 2 variables say distance1 and distance2.

Then lcd.print() the distances.

Read the data sheets, work through an example, get the display, then one sensor working… try to get a second one in the mix.

Post your attempt (in code tags), and we can probably help from there.

At the moment we don’t know enough about your requirements, and you won’t learn anything if one of us just drops it on the table for you,

Go on, give it your best shot.

would you want their data on the same or separate lines of the LCD?
how often would you want them queried?

You'll get some help from here: How to use two Ultrasonic sensors with Arduino - YouTube

create a sub-function with the pin #(s) as the argument(s). generate a pulse, wait for the response and wait a sufficient period of time for the pulse energy to dissipate before calling the sub-function again.

i guess it's up to you how to format the 2 data for display on the LCD

I have already connected a sensor to a display. The display already shows the distance. Now I want to attach a second sensor and that the display of both shows the distance. We have already tried to connect both, but the display shows only the distance from one sensor and not from two. How can I make it so that the display shows both distances or do I need two displays?

Help us help you.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Please post a schematic.

Please post an image of your project.

Please describe the problem better then you just did.

See my #4...

You presumably have the 2 sensors reading into variables called say distance1 and distance2, but seems you may only be doing one lcd.print(distance1) not for distance2?

But you can't really expect any meaningful help if you don't show the program you have so far.

post the code showing what you've done

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2); // Set the LCD address to 0x27 for a 16 chars and 2 line display

int trigPin1 =2 ; // TRIG
int echoPin1 =3 ; // ECHO
int trigPin2 =6 ; // TRIG
int echoPin2 =7 ; // ECHO
int pieps=5;
float duration_us1, distance_cm1, duration_us2, distance_cm2;

void setup() {
lcd.init(); // Initialize the lcd
lcd.backlight(); // Open the backlight
pinMode(trigPin1, OUTPUT); // Config trigger pin to output mode
pinMode(echoPin1, INPUT); // Config echo pin to input mode
pinMode(trigPin2, OUTPUT); // Config trigger pin to output mode
pinMode(echoPin2, INPUT); // Config echo pin to input mode
pinMode(pieps, OUTPUT);
Serial.begin(9600);
}

void loop() {
// Generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin1, HIGH);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
digitalWrite(trigPin2, LOW);

// Measure duration of pulse from ECHO pin
duration_us1 = pulseIn(echoPin1, HIGH);
duration_us2 = pulseIn(echoPin2, HIGH);
// Calculate the distance
distance_cm1 = 0.017 * duration_us1;
distance_cm2 = 0.017 * duration_us2;
Serial.println(distance_cm1);
Serial.println(distance_cm2);

//if(distance_cm < 30 & distance_cm > 20){
//tone(5, 100);
// delay(200);
// noTone(5);
//}
//if(distance_cm < 20 & distance_cm > 10){
// tone(5, 100);
// delay(100);
// noTone(5);
// }
// if(distance_cm < 10 & distance_cm > 5){
// tone(5, 100);
// delay(50);
// noTone(5);
// }
// if(distance_cm < 5 & distance_cm > 0){
// tone(5, 100);
// }
// else{
// noTone(5);
// }
if(distance_cm1 > 30){
lcd.clear();
lcd.setCursor(0, 0); // Start to print at the first row & first line
lcd.print(" ");
delay(100);
}
else{

lcd.clear();
lcd.setCursor(0, 0); // Start to print at the first row & first line
lcd.print("Vorne: ");
lcd.setCursor(0, 1); // Print at the first row & second line
lcd.print(distance_cm1);
lcd.print(" cm");

delay(100);
}

if(distance_cm2 > 30){
lcd.clear();
lcd.setCursor(6, 0); // Start to print at the first row & first line
lcd.print(" ");
delay(100);
}
else{

lcd.clear();
lcd.setCursor(6, 0); // Start to print at the first row & first line
lcd.print("Hinten: ");
lcd.setCursor(6, 1); // Print at the first row & second line
lcd.print(distance_cm2);
lcd.print(" cm");

delay(100);
}
}
this is my code. my problem is that the second ultrasonic sensor doesn't get a signal one of them prints the distance but the secound doesn't print the distance

you can't generate the pulse from both sensors at the same time, they interfere with one another.

you also need to add a delay between pulse to allow the pulses to dissipate before generating the next pulse (this means having a delay either after capturing the return from the 2nd sensor or before generating the pulse for the 1st sensor

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