Arduino Uno R3 with two Ultrasonic sensors

Hi,

I have made a small device to read water level for one tank using one ultrasonic sensor and display it on LCD screen. Now, If I want to add another sensor to read from two tanks and display it on the same screen. Any suggestions?!!

I'm using 16x2 LCD display and using the 16 blocks as a bar indicating the water level. Can I add at the end of the bar a percentage?

Help is very appreciated :slight_smile:

Any suggestions?!!

Yes. Go for it.

Can I add at the end of the bar a percentage?

Not if you are using all 16 positions. If not, then, yes you can.

ok any help to add other ultrasonic sensor ?!
i dont know how i will setup the lcd to display the second sensor
i used alot of codes non of them work :roll_eyes:

ok any help to add other ultrasonic sensor ?!

Plug the wires in just like you did the first one. Make note of which is the pulse pin and which is the echo pin.

i dont know how i will setup the lcd to display the second sensor

Going to be hard to write code, then.

i used alot of codes non of them work

Hard to believe. They must have done something, for someone, or it is unlikely that they would have posted the code on the internet.

you not give me any help you just talking and talking , what the hell is wrong with you ?!
if you have something share it, i don't know how to make the code to show 2 level at the same display, also i want a relay turn on the pump in a level i will chose it,
if you know something or can to help me in this code ok, if not go away from this post

i used alot of codes non of them work

But you didn't post any of them.
Difficult to help.

narzan:
Hi,

I have made a small device to read water level for one tank using one ultrasonic sensor and display it on LCD screen. Now, If I want to add another sensor to read from two tanks and display it on the same screen. Any suggestions?!!

I'm using 16x2 LCD display and using the 16 blocks as a bar indicating the water level. Can I add at the end of the bar a percentage?

Help is very appreciated :slight_smile:

My code

#include <LiquidCrystal.h>

#define ECHOPIN 3                            // Pin to receive echo pulse
#define TRIGPIN 4                            // Pin to send trigger pulse

#define STATUSPIN 13                        // Use for troubleshooting

int highWater = 20;        // These values allow to calculate % of full
int lowWater = 100;        // SRF04 hangs above water (lower distance = more water)

byte symbol[8] = {        // Custom character for LCD display
  B00000,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B00000,
};

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);    // Assign pins

// Utility function for flashing STATUSPIN
void flashLed(int pin, int times, int wait) {

for (int i = 0; i < times; i++) {
    digitalWrite(pin, HIGH);
    delay(wait);
    digitalWrite(pin, LOW);

if (i + 1 < times) {
      delay(wait);
    }
  }
}

void setup() {
 
  lcd.begin(16,2);
  lcd.print(" AQUA LEVEL ");
  lcd.createChar(0, symbol);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);

delay(3000);      // Show application name for 3 seconds.

}

void loop() {
  // Measure distance
  digitalWrite(TRIGPIN, LOW);                  // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                  // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.
 
  // Convert measured value to value between 0-16, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 16);
  lcd.clear();
  lcd.print("Water Level:");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
    lcd.print((char)0);
    scaledValue--;
  }
     
  delay(2000);  // Wait 2 seconds before measuring again. We're in no hurry!
}

start with selecting which two pins you will use to attach the sensor to the arduino.

so it will be :

#define ECHOPIN 3 // Pin to receive echo pulse
#define TRIGPIN 4 // Pin to send trigger pulse
#define ECHOPIN 5 // Pin to receive echo pulse2
#define TRIGPIN 6 // Pin to send trigger pulse2

copy all the code again and set the lcd.setCursor(1,0), and delete the line in red ?! to make other valuo shows on the first line on the LCD ?! is that right !!

int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 16);
lcd.clear();
lcd.print("Water Level:");
lcd.setCursor(0,1);
while (scaledValue > 0) {
lcd.print((char)0);
scaledValue--;
}

how about calculating the distance on the 2nd sensor.

you do the first one this way:

 // Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.

houw would you then measure a second distance, call it distance2

then the second one will be under that code ? like this

[glow=yellow,2,300]// Measure distance "1"[/glow]
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.
  
  // Convert measured value to value between 0-11, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 11);
  lcd.clear();
  lcd.print("E    |    F");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(2000);  // Wait 2 seconds before measuring again. We're in no hurry!
}



[glow=yellow,2,300]// Measure distance "2"[/glow]
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.
  
  // Convert measured value to value between 0-11, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 11);
  lcd.clear();
  lcd.print("E    |    F");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(2000);  // Wait 2 seconds before measuring again. We're in no hurry!
}

hmm :cold_sweat: how i can call it distance2 ?

well,you have two sets of trigger pins... but you called them the same thing!!!

#define ECHOPIN 3                            // Pin to receive echo pulse
#define TRIGPIN 4                            // Pin to send trigger pulse
#define ECHOPIN 5                            // Pin to receive echo pulse2
#define TRIGPIN 6                            // Pin to send trigger pulse2

consider:

#define ECHOPIN 3                            // Pin to receive echo pulse
#define TRIGPIN 4                            // Pin to send trigger pulse
#define ECHO_PIN2 5                            // Pin to receive echo pulse2
#define TRIG_PIN2 6                            // Pin to send trigger pulse2

so that you see the difference.

then call those pins in the 2nd read:

// Measure distance
  digitalWrite(TRIGPIN, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);                   // Send pin low again
  int distance = pulseIn(ECHOPIN, HIGH);        // Read in times pulse
  distance= distance/58;                        // divide by 58 gives cm.

// Measure distance
  digitalWrite(TRIG_PIN2, LOW);                   // Set the trigger pin to low for 2uS
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN2, HIGH);                  // Send a 10uS high to trigger ranging
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN2, LOW);                   // Send pin low again
  int distance2 = pulseIn(ECHO_PIN2, HIGH);        // Read in times pulse
  distance2= distance2/58;                        // divide by 58 gives cm.

seeing it?

oh ok so you change these

or this for both sensors ?

so now you need to print the two different values:

  // Convert measured value1 to value between 0-11, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 11);
  lcd.clear();
  lcd.print("E    SENSOR 1    F");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(2000);  // Wait 2 seconds before printing the 2nd value.

  // Convert measured value2 to value between 0-11, to display on LCD
  // Use Arduino built-in map and constrain functions
  int scaledValue = map(constrain(distance2, highWater, lowWater), lowWater, highWater, 0, 11);//<<<<<<<<note distance2
  lcd.clear();
  lcd.print("E   SENSOR 2   F ");
  lcd.setCursor(0,1);
  while (scaledValue > 0) {
     lcd.print((char)0);
     scaledValue--;
  }
      
   delay(2000);  // Wait 2 seconds before measuring again. We're in no hurry!

hmm ok
i use LCD 16*2
so i need the first bar will be for the first sensor
the second bar "under the first one"
so i must delete that line
lcd.print("E SENSOR 1 F");

and i think i must change the :
lcd.setCursor(0,1)
for the first sensor to be at first like this
lcd.setCursor(1,0)

then the second one same steps delete this line
lcd.print("E SENSOR 2 F");
and change this to the cursor for the second !! hmm i am understand it wrong i think !

so I was displaying two tanks on a screen that switches between each tank.

You could do something like this:

  int scaledValue1 = map(constrain(distance, highWater, lowWater), lowWater, highWater, 0, 11);
  int scaledValue2 = map(constrain(distance2, highWater, lowWater), lowWater, highWater, 0, 11);
  lcd.clear();
  while (scaledValue1 > 0) 
  {
    lcd.print((char)0);
    scaledValue--;
  }
  lcd.setCursor(0,1);
  while (scaledValue2 > 0) 
  {
    lcd.print((char)0);
    scaledValue--;
  }
  delay(2000);  // Wait 2 seconds before printing the 2nd value.

it will print your bar graph on one screen.

not a very good approach, in my mind but you can mess about with that.

oh wow i like that :slight_smile:
i think the code is ready now
i want ask other question but i think someone will killing me here :smiley:
i have pump and want make it work at 50% then turn it off at 100%

i have this code is that ok or i need add something else ? i connect a relay to the arduino 5V>220V 10A
and about the port what will be better use analog port or digital from the arduino ?

if (distance >= 18) {
digitalWrite(relayPin, HIGH); // assumes HIGH switches the relay on
}

if distance <= 5) {
digitalWrite(relayPin, LOW);
}

oh ok i miss understood you
i will use the other block to make it in percentage so it will be like that
########### 34%
########### 70%

narzan:
oh wow i like that :slight_smile:
i think the code is ready now
i want ask other question but i think someone will killing me here :smiley:
i have pump and want make it work at 50% then turn it off at 100%

i have this code is that ok or i need add something else ? i connect a relay to the arduino 5V>220V 10A
and about the port what will be better use analog port or digital from the arduino ?

if (distance >= 18) {
digitalWrite(relayPin, HIGH); // assumes HIGH switches the relay on
}

if distance <= 5) {
digitalWrite(relayPin, LOW);
}

your relay will likely prefer a nice digital input :.

glad to see you got there!

thanks :slight_smile: so the code is right 8)
finnaly :slight_smile: / i will test it and i will back

hmm i just have this error
sketch_aug27a.ino: In function 'void loop()':
sketch_aug27a:95: error: expected `}' at end of input
and the program highlight this line
~~ delay(3000); // Wait 2 seconds before measuring again. We're in no hurry!~~
i fix it now for testing 8)
thanks alot you are awesome :slight_smile: