Controlling an LED (5/5): Water Level Sensor with LED Indicator and Relay Control

Overview:

The final part of the Controlling LED series will be introducing an analog sensor (water level sensor) and the use of relay to toggle on or off an external device.  This project will also be quite familiar with previous projects such as the Music reactive LEDs using Sound Module and Turning on/off multiple LEDs and buzzer in terms of the setup and the sensor calibration. 

 

Hardware Used

  • Arduino Uno x 1

  • LED x 3
  • 
    
  • 220 ohm resistor x 3
  • 
    
  • Relay
  • Water level sensor
  • Breadboard x 1
  • 
    
  • Air Pump (optional)
  •  

    Software Used

    • Arduino IDE

     

    Application Discussion

    This project makes use of a water sensor as an input and relay and LEDs as output. An air pump will be connected to the relay output for the purpose of demonstrating the relay turning on or off a device only. First, the water sensor needs to be calibrated similar with the sound sensor. This is to ensure that the output will have a better grasp of its operating values. This will also turn on or off the LED based on the water level that the sensor detects. After this project, an automated air pump with water level indicator will be created next.

    Water Level Sensor

    The water level sensor works in a very straightforward manner. It detects the water level and thus is also quite helpful in detecting leakage. This sensor is composed of three pins and exposed copper traces.

    Pin No.

    Pin Name

    1

    GND

    2

    VCC

    3

    Signal

     

    The copper traces of the sensor are evenly distributed between power and sense traces right next to each other alternately. The power traces are connected to power while the sense traces are connected to Ground. These copper traces have resistance and when water falls over adjoining traces, it completes the circuit, current will flow (as water is a good conductor) and an analog signal is generated on the Signal (S) pin. Depending on how deep the sensor is in the water the resistance will vary. It acts as a variable resistor, thus, having the same concept as that of a potentiometer.

    The disadvantage of this sensor, however, is its short lifespan. This sensor is prone to corrosion when the probe is applied with power constantly. It is advised to only power the sensor when taking readings. 

    In this project, the LEDs are turned ON or OFF by the readings collected from the water level sensor.

    In order to know the readings from the water level sensor, this project makes use of the Serial Monitor. This process is similar to the Sound Sensor calibration done in the previous project.

     

    Relay

    A relay is an electrically operated switch that can control large currents while being operated by a relatively small current. This switch can be operated without physically toggling it on and off. The Arduino only operates at 5V at max, thus, it cannot directly control high voltage devices. However, a relay module allows the Arduino to control AC powered devices such as lamps and fans.

    How it works:

    A relay module has 3 control pins and 3 output terminals. The output terminals: normally close (NC), common (COM), and normally open (NO) are the high voltage terminals. These pins connect to the high powered device to be controlled. Meanwhile, the 3 control pins consists of the IN, GND, and VCC.

    Control Pins

    Pin Name

    Function

    IN

    Pin used to control relay via Arduino

    GND

    Ground connection

    VCC

    Voltage Source

     

    Output Terminals

    Pin Name

    Function

    NC

    Normally closed pin

    COM

    Common pin

    NO

    Normally open pin

     

    The typical relay has five pins, three of which are the output terminals while the remaining two are the coil. When connecting an output device on a relay, the main electricity enters the COM terminal. The NC and NO pins are used depending on how the user wants the device to operate. The normally closed (NC) configuration is utilized when the user wants the relay to be ON by default. The normally open (NO) configuration will have the relay open until a signal is sent from the Arduino to close the circuit.

    Default State – NC Closed

    The contact of the switch are only moved when current flows through the coil. Since the contact switch is connected to the normally close (NC) terminal by default, when the coil is energized the relay becomes OPEN. On the other hand, when current stops flowing through the coil, the contact switch returns to its initial state, that is CLOSED.

    Energized Coil – NC Open

     

    Set-up the Hardware

     

    Set-up the Software

    Open the Arduino IDE and upload the code on the Arduino.

    The full Arduino code can be found in Github here. You can use the code from there or copy below.

     

    int led_red=13;
    int led_grn=12;
    int led_blu=11;
    int relay =8;
    
    int waterSensor=A0;
    int waterOut=0;
    
    void setup() {
      
      pinMode(led_blu,OUTPUT);
      pinMode(led_grn,OUTPUT);
      pinMode(led_red,OUTPUT);
      pinMode(relay,OUTPUT);
    
      pinMode (waterSensor,INPUT);
      Serial.begin(9600);
    }
    
    void loop() {
      
      waterOut=analogRead(waterSensor);
    
      int waterMap = map(waterOut, 0, 1023, 0, 255);
      int level = constrain(waterMap, 0,170);
      
      Serial.println(level);
    
      if(level>20&&level<130)
       { 
        digitalWrite(led_blu, HIGH);
        digitalWrite(led_grn, LOW);
        digitalWrite(led_red, LOW);
        digitalWrite(relay,LOW);
       }
      
      else if(level>130&&level<145)
       {
        digitalWrite(led_blu, LOW);
        digitalWrite(led_grn, HIGH);
        digitalWrite(led_red, LOW);
        digitalWrite(relay,LOW);
       }
      
      else if(level>145&&level<165)
       {
        digitalWrite(led_blu, LOW);
        digitalWrite(led_grn, LOW);
        digitalWrite(led_red, HIGH);
        digitalWrite(relay,HIGH); 
       }
    }

    Code Breakdown

    • int var
      – Creates a variable integer.

    • pinMode(pin,mode) 
      – Configures the identified pin to behave as an input or an output.

    • analogRead
      – Reads an analog voltage between 1 to 5V and converts to digital. Digital reading can range from 0-1023 depending on the input used

    • map (fromLow, fromHigh, toLow, toHigh) 
      – Used to compress the sensor readings from a wider range to a smaller one

    • constrain()
      – It is used to contain the output values within range. This precaution is done because using map() does not guarantee that the output values can remain within its “to” range.

    • Serial.begin(9600)
      –  tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second.

    • Serial.println
      – prints data to serial port with a 'new line' at the end

    • digitalWrite
      – This sets the designated pin to a HIGH(5V) or LOW(0V) value

     

    Calibrating Water Level Sensor

    Once the code is uploaded, we need to obtain the values that the water sensor provides. This is done so we can determine the values at specific depths in the water level sensor where we want to label as LOW, MED, and HIGH. Each level will turn ON a specific LED in a 3-LED indicator setup. In other words, the LEDs light up on its designated water level value.

    Open the serial monitor by clicking the "magnifying glass" icon on the upper right side of the Arduino IDE.

    Arduino Serial Monitor

     

    It will be noticeable that as the sensor is submerged in water the value shown in the serial monitor changes. 

    When the sensor is not submerged, the serial monitor will print a series of 0. However, as the sensor is gradually submerged, the values also increase. Take note of the values for the following levels accordingly: LOW, MED, HIGH.
    Let's take a look on the values I obtained in my calibration process:
    First, I submerged the tip of the sensor in water:
    Low water level reading
     
    I was able to obtain readings from 10 to 122. However, I adjusted this to 20-130 in my code. There is no problem with adjusting the values. See if you're satisfied with how your LED lights up from the range you've set.
    Second, I further submerged the sensor until half of it was inside the water.

    Mid water level reading 

     

    Lastly, I recorded the values the sensor reading when it was fully submerged in water.

    High water level reading

     

    After this process, you should have determined a range of values between a minimum and a maximum value for each level. In my process I was able to identify the following

    Water Level

    Range

    Low

    20 – 130

    Medium

    130 – 145

    High

    145 – 165

     

    IMPORTANT

    Note that the sensor readings depend on the level of impurities from the water used. The mineral and impurities in water is what makes it conductive. Thus, there may be a difference in reading and the recorded values in this calibration process may or may not work for you.

    This is why it is important to calibrate your sensor according to the actual values it reads on certain water levels. Also, feel free to adjust the range of your recorded values until you are satisfied with your result. 

     

    Distributing the values with the 3 LED indicators

    Now that the values have been noted, input these in the code below on each if and else if conditions.

    
    if(level>20 && level<130) //LOW level
       { 
        digitalWrite(led_blu, HIGH);
        digitalWrite(led_grn, LOW);
        digitalWrite(led_red, LOW);
        digitalWrite(relay,LOW);
       }
      
      else if(level>130 && level<145) //MED level
       {
        digitalWrite(led_blu, LOW);
        digitalWrite(led_grn, HIGH);
        digitalWrite(led_red, LOW);
        digitalWrite(relay,LOW);
       }
      
      else if(level>145 && level<165) //HIGH level
       {
        digitalWrite(led_blu, LOW);
        digitalWrite(led_grn, LOW);
        digitalWrite(led_red, HIGH);
        digitalWrite(relay,HIGH); 
       }


    Note that we turned ON the relay only when the water level reaches the HIGH level. This will trigger the air pump to change state. You can choose to toggle the relay on a different level or condition depending on your application.

     

    Video Setup

     

    Conclusion

    In this project, we made use of LEDs as water level indicators for water level sensor. We also made use of a relay to control an air pump. Note that the air pump can easily be replaced with other outputs such as water pump, light bulb, or fan. The input sensor can also be replaced with other analog sensors as you see fit for your project.

     

    Checkout other tutorials in the Controlling an LED series:

    Adjusting LED Brightness using a Potentiometer

    Adjusting LED brightness using a Photoresistor

    Turning on/off multiple LEDs and buzzer

    Music reactive LEDs using Sound Module

     

    Reference

    https://bit.ly/393MfLX

    https://lastminuteengineers.com/water-level-sensor-arduino-tutorial/

    https://electropeak.com/learn/ake-a-liquid-level-indicator-with-arduino/

    https://lastminuteengineers.com/two-channel-relay-module-arduino-tutorial/

    https://osoyoo.com/2017/08/28/arduino-lesson-1-channel-relay-module/

    ArduinoArduino kitArduino unoLedMultiple ledsPumpRelayRelay moduleSensorWater level

    Leave a comment

    All comments are moderated before being published