Water Level and Weight Monitoring using Waterproof Ultrasonic Sensor and HX711 Load Cell, with Blynk App

Overview

This project aims to develop wireless monitoring using IoT Technology to interface the data acquired from the sensors to your smartphone. The project uses Blynk IoT platform to handle the data acquired. Two sets of sensor data are monitored: (1) water level, and (2) weight. The water level monitoring is part of an automated pumping system which
automatically fills a tank with water if the water level is too low.

Hardware Used


  • WATERPROOF ULTRASONIC MODULE JSN-SR04T
  • 
    
  • 20kg Load Cell with HX711 Amplifier
  • 
    
  • 12V AIR DIAPHRAGM DC PUMP 6W
  • 
    
  • RELAY MODULE 1-CHANNEL RELAY 5V
  • 12V DC External Supply
  • 
    

    Parts Overview

    Image result for doit esp32
    ESP32 Dev Board is a combo Wifi and Bluetooth development board compatible with the Arduino IDE. It’s already integrated with antenna and RF balun, power amplifier, low-noise amplifiers, filters, and power management module. The entire solution takes up the least amount of printed circuit board area. This board is used with 2.4 GHz dual-mode Wi-Fi and Bluetooth chips by TSMC 40nm low power technology, power and RF properties best, which is safe, reliable, and scalable to a variety of applications.
    JSN-SR04T waterproof ultrasonic distance sensor is suitable for outdoor applications such as car reversing sensors, security alarms, industrial inspection, outdoor water level sensing, and many more. This sensor has a distance measurement range of 20cm – 600cm.
    The working principle of an ultrasonic sensor it that a pulse is transmitted at time 0, and is reflected back by an obstacle or barrier. The sensor receives this reflected signal and converts it to an electric signal. The next pulse can be transmitted when the echo fades. This time period is called cycle period. The recommend cycle period should be no less than 50ms. If a 10μs width trigger pulse is sent to the signal pin, the ultrasonic module will output eight 40kHz ultrasonic signals and detect the echo back. The measured distance is proportional to the echo pulse width and can be calculated by the formula above. If no obstacle is detected, the output pin will give a 38ms high level signal.
    Related image
    The Load Cell sensor and Load Cell Amplifier module is shown above. The load cell amplifier is a small breakout board for the HX711 IC that amplifies very small strain/force measurements, and allows you to easily read analog data to measure weight. By connecting the amplifier to your microcontroller you will be able to read the changes in the resistance of the load cell and with some calibration you’ll be able to get very accurate weight measurements. This can be handy for creating your own industrial scale, process control, or simple presence detection.
    Load cells use a four wire Wheatstone bridge to connect to the HX711 chip.
    A Wheatstone bridge has four resistors forming the sides of a diamond shape. A battery is connected across one pair of opposite corners, and a galvanometer across the other pair.
    Wheatstone bridge is illustrated above. It is an electrical circuit used to measure an unknown electrical resistance by balancing two legs of a bridge circuit, one leg of which includes the unknown component. The primary benefit of the circuit is its ability to provide extremely accurate measurements (in contrast with something like a simple voltage divider). Its operation is similar to the original potentiometer.

    Library Used

    Software Used

    Hardware Set-up

    Schematic Diagram
    This is the Schematic diagram for the entire system
    This is the actual circuit of the system.
    This must be the set up for the load cell so that it will properly flex when a certain load is being measured. You can put any spacer as long as it will elevate your load cell .

    Software Set-up

    First we need to set up a Blynk account. In order to use Blynk we must download first the application from Google play if you’re using Android or App Store if using Apple otherwise.

    Here are the steps in setting up Blynk :

    1. Create Account

    After you download the Blynk App, you’ll need to create a New Blynk account. This account is separate from the accounts used for the Blynk Forums, in case you already have one.

    We recommend using a real email address because it will simplify things later.


    An account is needed to save your projects and have access to them from multiple devices from anywhere. It’s also a security measure.
    2.Create a New Project
    After you’ve successfully logged into your account, start by creating a new project.
    3. Choose Your Hardware
    4. Get your Authentication Token
    Click the icon and it will direct you to your project settings.
    Authentication Token is a unique identifier which is needed to connect your hardware to your smartphone. Every new project you create will have its own Authentication Token. You’ll get Authentication Token automatically on your email after project creation. You can also copy it manually. Click on devices section and selected required device .
    5. Adding Widgets
    Now after generating your authentication token you can now add Widgets by clicking the icon and add widgets such as button, displays etc.
    In choosing your Widgets you can drag and drop the widgets that you want but take note of your Energy balance if it is your first time without any projects created in your account it is usually 2000 energy balance.

    Here are more tutorials and projects on Blynk.

    Code:
    /* Calibration sketch for HX711 */
     
    #include "HX711.h"  // Library needed to communicate with HX711 https://github.com/bogde/HX711
     
    #define DOUT  16//  pin G16 connect to HX711 DOUT
    #define CLK  17  //   pin G17 connect to HX711 CLK
     
    HX711 scale;  // Init of library
    
    void setup() {
      Serial.begin(9600);
      scale.begin(DOUT, CLK);
      scale.set_scale();  // Start scale
      scale.tare();       // Reset scale to zero
    }
    
    void loop() {
      float current_weight=scale.get_units(5);  // get average of 20 scale readings
      float scale_factor=(current_weight/0.173);  // divide the result by a known weight
      Serial.println(scale_factor);  // Print the scale factor to use
    }

    This is for the calibration of your load cell. The number 0.173  represents the known weight, in my case I used the weight of my cellphone as my reference for the known weight. For more information about calibrating your load cell follow this link https://www.youtube.com/watch?v=pZpzdu97JYw  

     

    #include <MedianFilter.h>
    #define BLYNK_PRINT Serial
    #include "HX711.h"
    #include <WiFi.h>
    #include <WiFiClient.h>
    #include <BlynkSimpleEsp32.h>
    #include <NewPing.h>
    
    // You should get Auth Token in the Blynk App.
    // Go to the Project Settings (nut icon).
    char auth[] = " Your Auth Token ";
    
    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "Your Wifi SSID";
    char pass[] = "Your Wifi Password";
    BlynkTimer timer; 
    
    #define TRIGGER_PIN  26// Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     25  // Arduino pin tied to echo pin on the ultrasonic sensor.
    
    #define PI  3.1415926535897932384626433832795
    
    #define calibration_factor  106005.00 //This value is obtained using the SparkFun_HX711_Calibration sketch
    
    #define DOUT  16
    #define CLK  17
    
     // NewPing setup of pins and maximum distance.
    HX711 scale;
    
    const int MAX_DISTANCE = 100; // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    const int DIAM = 20;
    const int DEPTH = 60;
    const unsigned int Period = 1000;
    const int Area1 = PI * ((DIAM / 2) * (DIAM / 2)); 
    
     int litrs,dist,dept;
     
    NewPing sonar = NewPing(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
    
    void readings()
    {
      dist = sonar.ping_cm();                                         //get distance to the top of the water tank 1
       if (dist >= DEPTH || dist == 0 )  dist = DEPTH;                   //check it does not go negative
        dept = DEPTH - dist;                                   //calculate the depth of the water
        litrs = (Area1 * dept)/1000 ;                             //calculate the volume of the water as litres
        delay(50);
       
        Blynk.virtualWrite(V0, dept);                                //send depth to Blynk server
        Blynk.virtualWrite(V3, litrs);                                            //send litres to Blynk server
        Blynk.virtualWrite(V1, litrs / 10);                                   //send litres to Blynk server. for vertical level widget & chart, 
        Blynk.virtualWrite(V2,scale.get_units());
        pumpTrig();
            
         //scaled to 1/10 as Blynk only goes up to 9999 and we need up to 16000
        delay(50);
        
        Serial.println();
        Serial.println();
        Serial.println("Tank  water distance: " + String(dist));      //print depth
        Serial.println("Tank  water depth: " + String(dept));     //print depth
        Serial.println("Tank  Litres: " + String(litrs));
             
       Serial.print("Reading: ");
      Serial.print(scale.get_units(20), 3); //scale.get_units() returns a float
      Serial.print(" kg"); //You can change this to kg but you'll need to refactor the calibration_factor
      Serial.println();
    
    
    }
    void pumpTrig(){
    if (litrs <=3 ){
    digitalWrite(12,LOW);
    Blynk.virtualWrite(V12, "PUMP ON");
    }
    if (litrs >= 5)
    {
      digitalWrite(12,HIGH);
      Blynk.virtualWrite(V12, "PUMP OFF");
    }
    
      
    }
    
    void setup()
    {  scale.begin(DOUT, CLK);
     scale.set_scale(calibration_factor); 
      scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
     
      pinMode(12,OUTPUT) ;
      digitalWrite(12,HIGH);
      
      Serial.println("Readings:");
      // Debug console
      Serial.begin(9600);
     timer.setInterval(Period, readings); 
      Blynk.begin(auth, ssid, pass);
    }
    
    void loop()
    {
      Blynk.run();
      timer.run();
    }

    This is the code for the final system. As you can see there is no other statements on  the loop function other than the blynk.run() which does the handling and interfacing of output data.

    Code Breakdown
    dist = sonar.ping_cm();

    This Function is for getting the distance and automatically convert it to cm

    Blynk.virtualWrite(V0, dept);                                
    Blynk.virtualWrite(V3, litrs);                                           
    Blynk.virtualWrite(V1, litrs);                                  
    Blynk.virtualWrite(V2,scale.get_units());

    This is for sending the data gathered from the hardware, to the Blynk server.

    Blynk.run();

    This is a main blynk routine responsible for keeping connection alive, sending data, receiving data, etc.

    BlynkTimer timer;

     This macro instance allows you to send data periodically with given intervals not interfering with Blynk library routines.

    These are the output data gathered from the hardware to the blynk application.
    This is the final output for our water level sensor. As you can see there are times that it reads 0, this is because the reception beam of the ultrasonic sensor is very wide but overall the reading from the sensor is accurate .
    This is the final output for the Load cell as well as the data read from the ultrasonic sensor.

    Conclusion

    The project needs a more thorough calibration and a fixed mechanical enclosure. Specially the load cell, we need to mount and fix it properly so that the platform is sturdy and stable across the full range of loads that can possibly be placed above it. But overall the data gathered and the functionality of the system is accurate. Also the Blynk platform offers a fast way to prototype an IoT application.

    Reference

    The post Water Level and Weight Monitoring using Waterproof Ultrasonic Sensor and HX711 Load Cell, with Blynk App appeared first on CreateLabz.

    ArduinoBlynkDistance sensorEsp32Esp32 dev boardHx711IotKnowledgebaseLoad cellPumpSolenoidUltrasonic sensorWater levelWater pumpWaterproofWifi

    Leave a comment

    All comments are moderated before being published