Notifications
Clear all

Water level indicator  

Page 2 / 5
  RSS

Tooter
(@tooter)
Eminent Member
Joined: 6 months ago
Posts: 34
2020-05-19 10:32 am  
Posted by: @robotbuilder
Posted by: @tooter

Hi all I feel I have made a lot of progress today I've taken some code written for a wireless water meter and adjusted it to compile for the uno. I felt i would be beneficial to re write the code as opposed to copy and paste it I found it to be a beneficial learning curve there was a few hiccups along the way but managed to get it working quite well through the serial output. 

Great work.  Being able to figure it out for yourself comes with great personal satisfaction.

I noticed you use a header file called Blynk.h that I am unfamiliar with.

Checking the internet I read that Blynk allows arduino to communicate over the internet?

 

Thanks yer for some reason i cant explain the sonar wouldn't out put the measurements to serial if i didn't add the blynk library. I'm now trying to figure out how to make the screens back light to come on for 5 sec when a button is pressed. I've tried using the delay function but it resulted in random flashing from the back light. Looking for a different approach now.


ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 506
2020-05-19 1:25 pm  

@tooter

Ok. I don't think you need the blynk library. Where are you getting this code and what is your actual hardware wiring? Perhaps tomorrow I can hook up a couple of sonar modules and the LCD module shown below plus code for you to try out. I notice that a I2C Port Expander Module can be used with the LCD but I would have to order one of those.

LCD1602A

 

This post was modified 5 months ago by robotBuilder

ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 6 months ago
Posts: 34
2020-05-19 4:04 pm  
Posted by: @robotbuilder

@tooter

Ok. I don't think you need the blynk library. Where are you getting this code and what is your actual hardware wiring? Perhaps tomorrow I can hook up a couple of sonar modules and the LCD module shown below plus code for you to try out. I notice that a I2C Port Expander Module can be used with the LCD but I would have to order one of those.

LCD1602A

 

Hi thanks that would be good. I think the blynk library is used for the timer. If I comment out the blynk library

I get no output from the serial un-comment it and bingo works again.

The sketch i used was from instrucables and was made for the esp 8266 (nodemcu) hence the blynk library.

I will try and find time to draw a diagram for you to see my wiring.

I have also been working on a sketch to incorporate the lcd but struggling a bit but making slow progress if you would like to see it? 


ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 506
2020-05-19 7:13 pm  
Posted by: @tooter

The sketch i used was from instructables and was made for the esp 8266 (nodemcu) hence the blynk library.

So why are you using code for an ESP8266:ESP8266 ? 

I see that the blynk library offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor. Is that what you want, to control the hardware from an Android mobile device or something? I don't have a smart phone so that is not an option for me to work out.

I understood that you wanted to read two sonar sensors and output results on an LCD?  Also that you wanted a button to toggle between the readings of each sonar sensor. If the LCD has two lines of text you might just display both readings at the same time.

 


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 6 months ago
Posts: 34
2020-05-19 7:14 pm  

diagram of water meter.

water meter diagram

ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 506
2020-05-19 7:17 pm  

Ok just saw your diagram of the water meter circuit.

I will wire it up tomorrow. It is 4:15AM here and I am going back to bed 🙂

 


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 6 months ago
Posts: 34
2020-05-19 7:30 pm  
Posted by: @robotbuilder

Ok just saw your diagram of the water meter circuit.

I will wire it up tomorrow. It is 4:15AM here and I am going back to bed 🙂

 

no probs thanks for the help.


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 6 months ago
Posts: 34
2020-05-19 7:31 pm  
Posted by: @robotbuilder
Posted by: @tooter

The sketch i used was from instructables and was made for the esp 8266 (nodemcu) hence the blynk library.

So why are you using code for an ESP8266:ESP8266 ? 

I see that the blynk library offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor. Is that what you want, to control the hardware from an Android mobile device or something? I don't have a smart phone so that is not an option for me to work out.

I understood that you wanted to read two sonar sensors and output results on an LCD?  Also that you wanted a button to toggle between the readings of each sonar sensor. If the LCD has two lines of text you might just display both readings at the same time.

 

Hi yes that is what i want. I used the esp 8266 code as it was the easiest for me to understand. I have rewritten the code so i can understand it a little better and got it out punting to serial shoeing water depth and how many litres are left I just need it out putting to the lcd. I think it would be easier to understand if it had a mode button as I want it to show the amount of fresh water in one tank and the amount of grey water i the other I wouldn't want to get them mixed up for obvious reasons. thank you.

 


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 6 months ago
Posts: 34
2020-05-19 10:35 pm  

I really like the LCD output that you get form the firsr  skech posted on page 1. It gives a persentage and a bar increasing from left to fight my plan was to have that screen plus fresh and grey in the top left of each screen when the mode button is pushed. Hope that makes sense.


ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 506
2020-05-20 1:24 am  

Sure you can make it however you want once you know how it all works.
I already know how to read two sonar sensors so that is done.
This is my first foray into using the LCD display so I have begun with the "Hello World" 🙂
By the end of the day I should have a good idea how to  use the LCD display.

Here are some examples of the sources I am using as a tutorials.

https://www.circuitbasics.com/how-to-set-up-an-lcd-display-on-an-arduino/

https://www.arduino.cc/en/Reference/LiquidCrystal

LCD HelloWorld

 

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  lcd.print("hello, world!");
}

void loop() {
  
}

 

This post was modified 5 months ago 2 times by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Honorable Member
Joined: 1 year ago
Posts: 506
2020-05-20 5:54 am  

@tooter

So I have now added two ultrasonic sensors and the two results are displayed on the LCD.

I will draw the connections later in case you can't work them out from the code.

Will think about how to output a percentage bar and a mode button later.

 

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#include "NewPing.h"
 
// Define Constants

#define TRIGGER_PIN_1  7
#define ECHO_PIN_1     7
#define TRIGGER_PIN_2  8
#define ECHO_PIN_2     8

#define MAX_DISTANCE 400

NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);

//
//
// Define Variables
 
float duration1; // Stores First HC-SR04 pulse duration value
float duration2; // Stores Second HC-SR04 pulse duration value
float distance1; // Stores calculated distance in cm for First Sensor
float distance2; // Stores calculated distance in cm for Second Sensor



void setup() {
  lcd.begin(16, 2);  // 16 digits and two rows

}

void loop() {
  
  duration1 = sonar1.ping();
  duration2 = sonar2.ping();
  
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
  
  distance1 = (duration1 / 2) * 0.0343;
  distance2 = (duration2 / 2) * 0.0343;

  lcd.setCursor(0,0);
  lcd.print(distance1);
  lcd.setCursor(0,1);
  lcd.print(distance2);

  delay(500);
  
}
sonarLCD
sonarLCD2

Close up of the LCD displaying two distances.  Unfortunately the webcam has a fixed focus and close ups tend to be blurry.  Maybe I should get another digital camera!

sonarLCD3

 

This post was modified 5 months ago 3 times by robotBuilder

ReplyQuote
frogandtoad
(@frogandtoad)
Honorable Member
Joined: 1 year ago
Posts: 504
2020-05-20 6:16 am  

@tooter

Some comments on your code:

#define pi 3.1415926535897932384626433832795

You left this "pi" macro in your code, however you are using the Arduino built in version of "PI" instead.  Just be aware that the C and C++ languages are case sensitive in nature, so you should remove one of these to avoid any confusion.

Having said that, Arduino also offers a built in sq(x) function to help simplify your code, i.e:

const int Area1 = PI * sq(Diameter1 / 2);

Most importantly, I would review the logic you're using in the following conditional statement:

if (Distance1 >= Depth1 || Distance1 == 0 ) Distance1 = Depth1; //Check it dont go negative

I think you really only want to ensure that the distance is within range, and then perform your calculations, and take appropriate action otherwise - For example:

  if(Distance1 >= 0 and Distance1 <= Depth1) {
    Serial.println("In range");
    /*Perform your calculations here*/
   } else {
    Serial.println("Out of range");
    /*Flash a warning led or buzzer, etc... here*/ 
   }

Lastly, the following is not a big deal, but just letting you know that you can embed new lines and tab spaces directly within strings, to reduce your code size if you wish:

  Serial.println("\nTank 1");
  Serial.println("\tWater Distance: " + String(Distance1));
  Serial.println("\tWater Depth: "    + String(WaterDepth1));
  Serial.println("\tLitres: "         + String(Litres1));

Cheers!


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 6 months ago
Posts: 34
2020-05-20 2:39 pm  

Hi wow you have been bust today. It looks like you made more progress in one day that I have all week 😄. I will have a good look through the examples you put up and adress the things you flagged in my code it's just a matter of time and I'll get there. I appreciate the help. Cheers


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 6 months ago
Posts: 34
2020-05-20 5:15 pm  

Ok so made a little progress today. learnt a little to. Using your sketch as a guide I managed to get the lcd to out-put how many litres in the tank(see pic) surprisingly easy when it printing to serial already.

I also made the changes to the sketch you suggested. thanks.

Is it possible to change the out of range amount  as I would like the range to be low for the frash water tank and high for the grey water and a 3-5 sec buzzer to sound when fresh is down to 25% and grey is at 75%.

IMG 20200520 170638
IMG 20200520 170630

 


ReplyQuote
Tooter
(@tooter)
Eminent Member
Joined: 6 months ago
Posts: 34
2020-05-20 5:23 pm  

This is what i have so far.

 

//#Water level sensor. This scetch is to add a digital water lwvel sensor for a motor home 
//to monitor the fresh and grey water levels. 
//#The buzzer will sound for 3-5 sec when the level is at 50 and 25% and when grey water is
//at 75%

#include <Blynk.h>
#include <NewPing.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 13);
#include <EEPROM.h>
int addr = 0;
int addr2 = 1;
int flag;
byte readval;


#define SONAR_NUM 2                                //numer of sensors used.


//##Tank dimentions change to suit
const int MAX_DISTANCE = 400;                      //Max Distance to measure.
const int Diameter1 = 19;                          //Diameter of tank 1.
const int Diameter2 = 19;                          //Diameter of tank2 2.
const int Depth1 = 25;                             //Depth of tank 1.
const int Depth2 = 25;                             //Depth of tank2.

const unsigned int Period = 2000;                  //period between pings, in milliseconds

//##Sensor Pins

const int TrigPin1 = 6;                            //Triger pin for sensor 1
const int EchoPin1 = 7;                            //Echo pin for sensor 1
const int TrigPin2 = 10;                           //Triger pin sensor 2 
const int EchoPin2 = 8;                            //Echo pin sensor 2

const int Area1 = PI * sq(Diameter1 / 2);          //Area of tank 1
const int Area2 = PI * sq(Diameter1 / 2);          //Area of takn 2

//Global variables
int Litres1, Litres2, Distance1, Distance2, WaterDepth1, WaterDepth2;

BlynkTimer timer;                                  //Config Timer

NewPing sonar [SONAR_NUM] = {                      //Sensor object aerray
  NewPing (TrigPin1, EchoPin1, MAX_DISTANCE),      // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing (TrigPin2, EchoPin2, MAX_DISTANCE)
};

void sendSensorReadings()
{
//#Tank 1 Readings
Distance1 = sonar[0].ping_cm();                  //Get distance to top of tank 
if(Distance1 >= 0 and Distance1 <= Depth1) {
Serial.println("In range");
WaterDepth1 = Depth1 - Distance1;                 //calulate the depth of water in tank 1
Litres1 = (Area1 * WaterDepth1) / 1000;           //calulate the volume of water
delay(50);
 } else {
 Serial.println("Out of range");
    /*Flash a warning led or buzzer, etc... here*/ 
   }

//#Tank2 Readings
Distance2 = sonar[1].ping_cm();                   //Get distance to top of tank 2
if(Distance2 >= 0 and Distance2 <= Depth2) {
Serial.println("In range");
WaterDepth2 = Depth2 - Distance2;                 //calulate the depth of water in tank 1
Litres2 = (Area2 * WaterDepth2) / 1000;           //calulate the volume of water
delay(50);
 } else {
 Serial.println("Out of range");
    /*Flash a warning led or buzzer, etc... here*/ 
   }

digitalWrite(13, HIGH);                          //Flash the led so we know ist running
delay(50);
digitalWrite(13, LOW);

//#Serial Output
Serial.println();
Serial.println();
Serial.println("Tank 1 Water Distance: " + String(Distance1)); //print Depth tank 1
Serial.println("Tank 1 Water Depth: " + String(WaterDepth1));  //print WaterDepth tank 1
Serial.println("Tank 1 Litres: " + String(Litres1));           //print Litres tank 1

Serial.println();
Serial.println("Tank 2 Water Distance: " + String(Distance2));  //print Depth Tank 2
Serial.println("Tank 2 Water Depth: " + String(WaterDepth2));   //print Waterdepth tank 2
Serial.println("Tank 2 Litres: " + String(Litres2));            //print Litres tank 2

}

   

void setup() {
  lcd.begin(16, 2);
  pinMode(13, OUTPUT);                                          //led pin 13
 
  timer.setInterval(Period, sendSensorReadings);      // Setup a function to be called every 'n' seconds
  delay(10);
  
  Serial.begin(19200);                                           //Open serial console
  Serial.println();
  delay(20);
}
  
void loop() {
  
 lcd.setCursor(0,0);
 lcd.print("TNK 1 Litres " + String(Litres1));        //print on lcd amount in liters in Tank1
 lcd.setCursor(0,1);
 lcd.print("TNK 2 Litres " + String(Litres2));        //print on lcd amount in liters in tank2  
 
 timer.run();
}


ReplyQuote
Page 2 / 5