Mini Water Level Sensor & Controller With LCD Display

by DP - 987636 in Circuits > Arduino

511 Views, 4 Favorites, 0 Comments

Mini Water Level Sensor & Controller With LCD Display

20230125_095228(0).jpg

Description:


This device is a water level controller and can be used to control the levels of water tanks, containers even as well as dams. It works by receiving a signal from the water level sensor, the central component of this circuit. The value read by this sensor is displayed on the LCD screen in percentage values and the RGB lights up accordingly. Meaning if the water level is extremely high, the LCD will display text alarming the user, the RGB will turn red and a Piezo buzzer will sound off an alarm. A slide switch, controlled by the user, will have the ability to operate a servo motor, which, in this case, represent, can represent floodgates or valves or even taps which can allow excess water to flow out. 


Purpose:


The purpose of this project is that the entire system checks the water level and if it exceeds the limit, all the user has to do is turn on a slide switch for a desired time to control the water level, say in a dam or a water tank or a container. This makes the project useful in real life and has seveal


Attached is a video demonstrating the project at work:

Supplies

components.png

To build this project, you will be needing the following components:


  1. 1 Arduino Uno R3
  2. 1 Breadboard
  3. 1 Resistor, 330 Ω
  4. 1 Capacitor, 10µF
  5. 1 Slide Switch 
  6. 1 NPN Transistor
  7. 1 AND Logic Gate
  8. 1 RGB LED
  9. 1 Water level sensor [Not shown in the attached image]
  10. 1 Servo motor
  11. 1 LCD I2C 16 x 2 display
  12. Jumper cables and wires [preferably male to female cables]

Idea and Research

I found a couple of similar project sonline which used a Water Level sensor and a LCD display to display the value. I was inspired by these projects to make a similar one but with more functions. I found these projects lacked functionality and decided to give this idea a proper function.

I decided to give it a real-life application by making project able to control the water level and alert the user better. I added the Servo motor to represent that the water levels can be controlled by valves or floodgates if the water level becomes too high, or even too low. I added the RGB LED to alert the user using colours. If the RGB was red, that meant that the user has to control the water level by turning on the slideswitch.

https://www.thegeekpub.com/236571/arduino-water-level-sensor-tutorial/

https://srituhobby.com/water-level-sensor-tutorial-how-to-use-water-level-sensor/

How It Functions and Components

20230126_084206.jpg

I used components like:

  • LCD I2C - to display various text and the percentage value of the water level
  • Servo motor - represents the control mechanism which can control the water level
  • Water level sensor - sense the water level in a container and sends an analog signal back to the Arduino

  • Slideswitch - allows the user to control the Servo
  • Transistor - only allows a given current to pass through if is receives a signal, like a electrical switch
  • AND Gate - can only send a signal if both pins are receiving a current

  • RGD LED - lights up in a different colour according to the water level


If the water level is extremely high, the LCD will display text alarming the user, the RGB will turn red. A signal will be sent out to the AND gate while the other signal will be sent out to the collector pin of the transistor waiting to be sent to the AND gate. A slide switch, controlled by the user, will have the ability to send the signal to the base pin of the transistor, allowing current to flow through the emitter to the AND Gate. The AND Gate, now receiving both signals, will send a signal to the Arduino which controls the servo motor which, in this case, can represent floodgates or valves or even taps which can allow excess water to flow out.

Wiring Components

20230126_083831.jpg
20230126_084557.jpg
20230123_095935.jpg
20230123_095929.jpg
20230123_103730.jpg
20230123_104158.jpg
20230123_103750.jpg
water sensor schematic.jpg

Connect the ground and power rails of the breadboard with the 5V and GND pins of the Arduino

RGB LED:

  1. Connect the red, green and blue pins to digital pins 9, 10 and 11 respectively
  2. Connect the common anode leg to the power rail using a 330 Ohm resistor


Servo Motor:

  1. Connect the power and ground pins of the servo to the power and ground rails through a 10uf Capacitor
  2. Connect the signal pin to digital pin 6 on the Arduino


Water level sensor:

  1. Connect the power pin of the sensor to digital pin 7 on the Arduino
  2. Connect the signal pin of the sensor to analog pin A1 on the Arduino
  3. Connect the ground pin of the sensor to the ground rail


LCD I2C:

  1. Connect the power and the ground pins of the LCD to the power and ground rails
  2. Connect the SDA and SCL pins of the LCD to the SDA and SCL pins on the Arduino, above digital pin 13.


AND Gate:

  1. Connect the ground pin of the gate, same side but fartherst from the indent on the gate, to the ground rail.
  2. Connect the power pin of the gate, opposite the indent, to the power rail.
  3. Connect of the pins to the power rail
  4. While the other to the emitter pin of the transistor
  5. The OUTPUT pin of the two pins stated above, must be connected to digital pin 2 on the Arduino


Slideswitch and the transistor:

  1. Connect the power and ground legs of the switch to power and ground
  2. Connect the signal pin of the switch to the base pin of the transistor
  3. Connect the collector pin of the transistor to the power rail.

Code (part 1) - Variables

variables.png

In this part of the code, we insert the essential libraries, required variables and allocate each Arduino pin as needed.

  • Libraries are files which provide your code with extra functionality and allow the Arduino to interact with different components like the Servo motor and the LCD


  1. The first library we need is "Wire.h", which allows the Arduino to use its SDA and SCL pins (above digital pin 13) to interact with components like a LCD I2C
  2. The second library we need is "LiquidCrystal_I2C.h", which is the library needed to use a LCD I2C
  3. The third library we need is "Servo.h", which is the library needed to use a Servo motor


  • Next we address the dimensions of the LCD with "LiquidCrystal_I2C lcd(0x27,16,2)" which basically states that the LCD has 16 columns and 2 rows


  • Thereafter, we name the servo as "myServo". You can name your servo motor anything you like but it is important to name so it can addressed later in the code


  • For the last part, we allocate each component with a pin on the Arduino and insert different variables. The water level sensor will require an analog pin as it can read from a range of 0 to 600, unlike a digital pin which reads only 1 and 0

Code (Part 2) - Setup

void setup.png

In this part of the code, we:

  • Attach the servo to digital pin 6


  • Initialize the LCD and turn on its backlight


  • Turn on the serial monitor [Useful to test out to the project in earlier stages]


  • Set the Arduino pins as either input or output, depending on the use


You can also code a start-up sequence to indicate that the project starts and works properly. Here, I made the LCD print "Loading..." and "Please wait" as well as turned on the RGB LED to blue.

Code (Part 3) - Measuring the Water Level

void waterlevel.png

This part of the code measures the water level through the sensor and sets a variable as the value measured.

  • First we turn on the sensor and wait 10 milliseconds


  • Then we state that "val" will be equal to whatever value is read by the sensor, from 0 to 600


  • Next, we print the value on the Serial monitor. [HELPFUL IN EARLIER STAGES]


  • Last, we wait 0.1 seconds and turn off the sensor

The reason why we need to control the sensor by controlling the power is that prolonged contact with the water while the sensor is on can damage it and shorten its lifespan.

Code (Part 4) - Display

void leveldisplay 1.png
void leveldisplay 2.png

This part of the code does the main job of displaying the value to the user and alerting them. We:

  • First map out of the variables val and valPercent. We want valPercent (0 to 100) to be proportional to val (0 to 600). Meaning if the value read by the sensor is 300, then valPercent, or the value in percentage, will be 50%


  • Next we setup "if" and "else if" commands.
  1. If the value is below 50, the LCD displays that the container is almost empty and RGB turns blue.
  2. If the value is below 100 but above 50, the LCD displays that the water level is low and RGB turns green.
  3. If the value is below 300 but above 100, the LCD displays that the container is filled and RGB remains green.
  4. If the value is below 500 but above 300, the LCD displays that the container is filled and RGB turns orange.
  5. If the value is above 500, the LCD displays that the container is full and may overflow and RGB turns red.


  • Then, we code it so that the Arduino wait for 1 second, clears the LCD and repeats "void levelDisplay()" again


  • So, based on the water level, we display the water level in percentage and warn the user accordingly
  1. lcd.print("text" + String(variable)) - prints text and a value in the same line
  2. setColor (0,255,0) - turns on the RGB to a specific colour, here its green
  3. "if" and "else if" are used because the Arduino will look if the condition in the 'if" is met or not. If not, it will keeping moving onto the next commands until it satisfies the conditions of one command.

Code (Part 5) - Servo Motor and Final Loop

void servo and loop.png

These two are the shortest parts of the code and thus have been combined into one section.

  • void servoControl() reads if controlPin is high or low.


  • If high, then it will write the Servo to 90 degrees to indicate some sort of control mechanism as been turned on to control the water level.


  • When it turns low, it will write the Servo back to 0 degrees.


  • This allows the user to quickly control the Servo without any delay.

  • void loop() is the part of the code where all the commands, like waterLevel and levelDisplay, must be located in order for the Arduino to execute them.

Final Image and Modifications

20230123_104229.jpg

The project can be modified in various ways to fit the user's needs and wants. These modifications can be minor, like extending wires or using stronger sensors, or they could be larger and more complex modifications like:

  • The OUTPUT components can be modified or even replaced to build even more advanced systems.
  • The system can be coded to have a variety of functions other than reading the water sensor and operating the servo.

The user would also requires components, eresistors, wires and breadboards or different sizes and strengths if they choose to modify or replace their OUTPUT or INPUT components.