In this guide, we will learn how to interface a water level sensor with an ESP32 microcontroller. The water level sensor is used to detect the water level in a container or tank. By measuring the analog output of the water level sensor using the ESP32's ADC, we can monitor the water level and use it for various automation purposes.
The goal is to interface the water level sensor with an ESP32 microcontroller, to accurately measure the water level in a container or tank using the analog output of the water level sensor. The measured water level will be used for monitoring purposes.
To successfully complete this project, you will need the following components:
ESP32 development board
Water level sensor
Jumper wires
Connect the water level sensor to the ESP32 development board as follows:
In theory, we can provide power to the water sensor by connecting the sensor's VCC and GND pins to ESP32's 3.3V and GND pins, respectively. However, that way is not recommended in practice. If we supply power to the water sensor constantly, the water sensor is electrochemical and corrodes faster by the moist environment. So it is better to provide power to the water sensor only and only when reading the value from the sensor. To do so, We can connect the sensor's VCC pin to an ESP32's digital pin, and program the ESP32's pin to provide power only when required
Connect the power pin of the water level sensor to a GPIO 4 (D4) of the ESP32 .
Connect the signal pin of the water level sensor to GPIO 32 (D32) of the ESP32.
And finally connect GND of the sensor to GND of ESP32.
Now let's understand and write the C code to check the water level. Follow the steps below:
Open VS Code and create a new sample project with the name water_level (Follow previous guides to see how to do it).
Now navigate to the "Main" folder of the project and then open the Main.c file.
Copy and paste the following code into Main.c or download the code from here:
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
#define POWER_PIN GPIO_NUM_4
#define SENSOR_PIN ADC1_CHANNEL_4 // GPIO32
static esp_adc_cal_characteristics_t adc1_chars;
int water_level = 0;
void app_main(void)
{
// power pin
gpio_set_direction(POWER_PIN, GPIO_MODE_OUTPUT);
gpio_set_level(POWER_PIN, 0);
// Configure ADC1
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 0, &adc1_chars);
// Configure ADC resolution to 12 bits
adc1_config_width(ADC_WIDTH_BIT_12);
// Set attenuation of ADC1 channel 4 to 11dB (3.9V)
adc1_config_channel_atten(SENSOR_PIN, ADC_ATTEN_DB_11); // ADC1_CHANNEL_4 = GPIO32
while (1)
{
// Read ADC1 Channel 4
gpio_set_level(POWER_PIN, 1);
water_level = adc1_get_raw(SENSOR_PIN);
gpio_set_level(POWER_PIN, 0);
printf("Water Level is: %d\n", water_level);
printf("\n");
vTaskDelay(5000 / portTICK_PERIOD_MS); // wait 5 seconds
}
}
Now at the bottom navbar of VS Code select the port, flash method and click on the Build, flash and monitor icon.
Note: During the flashing process of your board, you may encounter an error. If this happens, there's no need to worry. Simply flash the board again and when you see the message "connecting..........." appear, press and hold the boot button on your board for 1 or 2 seconds. Keep holding it until the dots on the screen start disappearing or the further flashing process begins. This action will help resolve any issues and ensure a successful flashing of your board.
After uploading the code to the ESP32, the program will continuously monitor the water level. The serial monitor will display the readings from the water level sensor. Adjust the water level in the container or tank (by partially dipping or fully immersing the sensor) and observe the changes in the displayed readings. Note these values and use them in your application to do automation tasks.
In this guide, you have learned how to interface a water level sensor with an ESP32 microcontroller. By reading the analog output of the water level sensor using the ESP32's ADC, you can monitor the water level in a container or tank. This information can be used for automating tasks, such as controlling water pumps, sending notifications, or displaying the water level on a user interface. Experiment with different water levels and observe the corresponding analog readings to understand the behavior of the sensor and its applications in your projects.