Controlling an LED (5/5): Water Level Sensor with LED Indicator and Relay Control
01 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.
02 Hardware and Software Components
Gather everything below before you start. This is your checklist — names, models, and versions only.
Hardware Components
| Component | Description |
|---|---|
| Arduino Uno x 1 | a small microcontroller board that acts as the “brain” of electronic projects |
| LED x 3 | emits light when current flows through it |
| 220 ohm resistor x 3 | passive two-terminal electrical component that implements electrical resistance in a circuit |
| Relay | an electromechanical switch that allows low‑voltage microcontrollers to safely control high‑voltage or high‑current devices |
| Water level sensor | detects the presence or height of water in a container or environment by measuring conductivity or resistance between exposed traces |
| Breadboard x 1 | allows you to quickly build temporary circuits without soldering, used for prototyping |
| Air Pump (optional) | used to provide airflow for inflating objects, circulating air in enclosures, or driving bubbles in water tanks |
Software Tools
| Software | Version / Details |
|---|---|
| Arduino IDE | official software used to write, compile, and upload code to Arduino boards and other microcontrollers |
03 Application Discussion
Here is what each component does and why it is part of this project.
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 | Function |
|---|---|
| IN | Pin used to control relay via Arduino |
| GND | Ground connection |
| VCC | Voltage Source |
| Output Terminals | 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
04 Hardware Setup
Wire the components to the board using the tables below.

Connections to Arduino
| Pin / Signal | Connects To |
|---|---|
| Red LED VCC | Pin D13 |
| Green LED VCC | Pin D12 |
| Blue LED VCC | Pin D11 |
| LED GND Pins | Ground Pin |
| Water Level Sensor VCC | 5V Pin |
| Water Level Sensor GND | Ground Pin |
| Water Level Sensor Signal | Pin A0 |
| Relay IN Pin | Pin D8 |
05 Code
Read the Code Breakdown section to understand what each part does.
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);
}
}
06 Code Breakdown
Here is what each part of the code does. Read this after uploading.
Key Functions
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)
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
07 Testing and Calibration
After uploading, verify each of the following to confirm the system is working correctly.
Calibrating Water 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.
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


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


High water level reading
Sensor Readings
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 |
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);
}
08 System Demonstration
Video Demonstration
09 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
10 References
- 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/
11 Project Authors
- Nolf Ivan Labios
Quality Checked By:
- Lemuel Jess Azaria
- Kyla Jade Entorum
