0
0
AutocadHow-ToBeginner · 4 min read

How to Use Water Level Sensor with Arduino: Simple Guide

To use a water level sensor with Arduino, connect the sensor's output pin to an Arduino analog input pin and power it with 5V and GND. Then, read the sensor value using analogRead() in your code to detect water level changes.
📐

Syntax

Here is the basic syntax to read a water level sensor value in Arduino:

  • pinMode(pin, INPUT); sets the sensor pin as input.
  • analogRead(pin); reads the sensor's analog voltage value (0-1023).
  • Serial.println(value); prints the sensor value to the serial monitor.
arduino
const int sensorPin = A0;  // Analog pin connected to sensor output

void setup() {
  Serial.begin(9600);       // Start serial communication
  pinMode(sensorPin, INPUT); // Set sensor pin as input
}

void loop() {
  int sensorValue = analogRead(sensorPin); // Read sensor value
  Serial.println(sensorValue);              // Print value
  delay(1000);                             // Wait 1 second
}
💻

Example

This example reads the water level sensor value and turns on an LED if the water level is above a threshold.

arduino
const int sensorPin = A0;  // Sensor output connected to analog pin A0
const int ledPin = 13;     // Built-in LED pin
const int threshold = 500; // Threshold for water level detection

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  Serial.print("Water Level Sensor Value: ");
  Serial.println(sensorValue);

  if (sensorValue > threshold) {
    digitalWrite(ledPin, HIGH); // Turn LED on if water level is high
  } else {
    digitalWrite(ledPin, LOW);  // Turn LED off otherwise
  }
  delay(1000);
}
Output
Water Level Sensor Value: 350 Water Level Sensor Value: 480 Water Level Sensor Value: 520 Water Level Sensor Value: 600 ...
⚠️

Common Pitfalls

  • Not connecting sensor power (VCC) and ground (GND) properly causes no readings.
  • Using digital pins instead of analog pins for sensor output will not work.
  • Ignoring sensor calibration can lead to wrong threshold values.
  • For sensors with digital output, reading analog pins will give incorrect results.
arduino
/* Wrong: Using digitalRead on analog sensor output pin */
const int sensorPin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop() {
  int sensorValue = digitalRead(sensorPin); // WRONG: should use analogRead
  Serial.println(sensorValue);
  delay(1000);
}

/* Correct: Use analogRead for analog sensor output */

void loop() {
  int sensorValue = analogRead(sensorPin); // Correct
  Serial.println(sensorValue);
  delay(1000);
}
📊

Quick Reference

Water Level Sensor Arduino Quick Tips:

  • Connect sensor VCC to 5V, GND to ground.
  • Connect sensor output to an analog input pin (e.g., A0).
  • Use analogRead() to get sensor values (0-1023).
  • Calibrate sensor by checking values at empty and full water levels.
  • Use thresholds in code to detect water presence or level.

Key Takeaways

Connect the water level sensor output to an Arduino analog pin and power it correctly.
Use analogRead() to read sensor values and interpret water level changes.
Calibrate your sensor to find proper threshold values for your setup.
Avoid using digitalRead() on analog sensor outputs to prevent wrong readings.
Test your connections and code with the serial monitor to verify sensor data.