Interfacing ESP32 with a Water Level Sensor

Water level monitoring is an essential application in various industries, from water tanks to aquariums. If you’re looking to build a water level detection system, an ESP32 microcontroller paired with a water level sensor can be an excellent solution. In this blog post, we’ll guide you through the process of connecting an ESP32 to an analog water level sensor, reading its output, and displaying the data on a serial monitor.

What You Need:

  • ESP32 Board (e.g., ESP32 Dev Kit)
  • Water Level Sensor (typically analog or digital)
  • Jumper Wires
  • Breadboard (optional)
  • Arduino IDE for programming the ESP32

Wiring the ESP32 with the Water Level Sensor:

  • VCC (Water Sensor)3.3V (ESP32) (Note: Some sensors can work with 5V; check your sensor’s specification)
  • GND (Water Sensor)GND (ESP32)
  • Analog Output (Water Sensor)GPIO32 (ESP32)

Important Note:

The ESP32 has a 12-bit ADC, meaning the analog input range is from 0 to 4095. If your water level sensor operates at a voltage higher than 3.3V (like 5V), you’ll need to reduce the voltage using a voltage divider to prevent damaging the ESP32.

Code:

#define WATER_SENSOR_PIN 32  // Pin for analog input

void setup() {
  // Initialize serial communication at 115200 baud rate
  Serial.begin(115200);

  // Set the sensor pin as an input
  pinMode(WATER_SENSOR_PIN, INPUT);
}

void loop() {
  // Read the analog value from the water level sensor
  int sensorValue = analogRead(WATER_SENSOR_PIN);

  // Convert the sensor value to a percentage (0 to 100%)
  float waterLevelPercentage = (sensorValue / 4095.0) * 100;

  // Print the sensor value and water level percentage to the serial monitor
  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);
  Serial.print("  Water Level Percentage: ");
  Serial.println(waterLevelPercentage);

  // Wait for 1 second before taking another reading
  delay(1000);
}

Understanding the Code:

  • WATER_SENSOR_PIN: This is the GPIO pin on the ESP32 connected to the analog output of the water level sensor. In the example, we use GPIO32, but you can choose any ADC-capable pin on the ESP32.
  • analogRead(): This function reads the analog voltage from the sensor and returns a value between 0 and 4095. Since the ESP32’s ADC is 12-bit, the maximum value is 4095.
  • Water Level Calculation: The sensor output is a voltage that correlates with the water level. We convert the raw sensor value into a percentage to represent the water level from 0% (empty) to 100% (full). The formula used is:
    Water Level Percentage = (sensorValue / 4095.0) * 100
  • Serial Output: The code prints the sensor value and the calculated water level percentage to the serial monitor for easy monitoring.

Testing the Setup:

  1. After uploading the code to your ESP32, open the Serial Monitor in the Arduino IDE.
  2. You should see readings that represent the water level, in both the raw sensor value and the percentage.

Conclusion:

By following these steps, you’ve successfully interfaced an analog water level sensor with an ESP32. The ESP32 reads the analog output from the sensor, converts it to a percentage, and displays the water level in real-time on the serial monitor. This basic setup can be expanded to include features such as triggering alerts when the water level is too high or low, or sending the data to a cloud server for remote monitoring.