Measure/Detect water level

This experiment involves measuring water levels using a water sensor connected to a Raspberry Pi Pico. The Pico reads the analog signal from the water sensor, converts it to a digital value using its built-in ADC (Analog-to-Digital Converter), and prints the water level readings to the console. This setup can be used in various applications such as monitoring water tanks, flood detection systems, or even in smart gardening systems.

  1. Scope:

The scope of this experiment includes setting up the hardware and software necessary to measure water levels. It covers the connection of a water sensor to the Raspberry Pi Pico, the reading of analog values, and the implementation of a simple Python script to display these values. The experiment aims to demonstrate basic ADC usage and sensor interfacing with the Pico.



  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • Breadboard

  • Water Level Sensor

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.

  1. Connection Details:

  • Water Sensor Output: Connect to GPIO 28 on the Raspberry Pi Pico (ADC pin).

  • GND Pin: Connect to the Pico’s GND.

  • VCC Pin: Connect to the Pico’s 3.3V.



  1. Working Principle:

The water sensor outputs an analog voltage that varies with the presence and level of water. This analog signal is fed into the ADC pin of the Raspberry Pi Pico. The Pico’s ADC converts this analog voltage into a digital value, which can then be read and processed by the MicroPython script. The script periodically reads this digital value, which corresponds to the water level, and prints it to the console.

A water sensor, often used for detecting the presence or level of water, is designed to provide a signal that indicates whether water is present or absent. There are various types of water sensors, but the most common ones used with microcontrollers like the Raspberry Pi Pico are the analog water sensors, which measure the level of water based on changes in resistance or capacitance.

Working Principle

  1. Basic Concept:

    • Analog Water Sensor: This type of sensor outputs an analog voltage that varies depending on the water level or presence. The sensor typically has two or more conductive probes or plates that are exposed to the water.

    • Digital Water Sensor: This type of sensor outputs a digital signal (high or low) indicating whether the sensor has detected water.

  2. Analog Water Sensor:

    • Sensing Mechanism: The sensor consists of conductive plates or probes placed in a conductive material (such as water). When water is present, it changes the resistance between the plates. This change in resistance alters the output voltage of the sensor.

    • Output Voltage: The sensor produces a voltage that corresponds to the level of water. For example, if the sensor is fully submerged, it may produce a higher voltage, while if it is dry, the output voltage may be lower.

    • ADC Conversion: This analog voltage is read by an Analog-to-Digital Converter (ADC) on a microcontroller. The ADC converts the analog signal into a digital value that the microcontroller can process.

  3. Digital Water Sensor:

    • Sensing Mechanism: The sensor typically uses a pair of conductive probes. When water bridges the gap between these probes, it completes a circuit and triggers a digital output signal.

    • Output Signal: The sensor sends a digital signal (high or low) to indicate the presence of water. Some digital sensors may have a built-in comparator to generate this signal based on a predefined threshold.

Key Points

  • Resistance Variation: In many water sensors, the presence of water changes the resistance between the sensor probes, which in turn affects the output voltage.

  • Capacitive Sensing: Some water sensors use capacitive sensing, where the capacitance between plates changes with the water level.

  • Calibration: Sensors often need calibration to accurately reflect water levels, especially if the sensor’s output is nonlinear or affected by other factors.





  1. Application code:

  • MicroPython:

import machine

import utime


# Initialize the water sensor pin (connected to GPIO 28)

sensor = machine.ADC(28)


while True:

# Read the water level value (0-65535 range for 16-bit ADC)

value = sensor.read_u16()

print(value)

utime.sleep_ms(200) # Delay for 200 milliseconds before reading again

  • Embedded C:

#include "pico/stdlib.h"

#include "hardware/adc.h"


#define WATER_SENSOR_PIN 26 // ADC0 pin on the Pico


int main() {

// Initialize standard I/O

stdio_init_all();


// Initialize ADC

adc_init();

adc_gpio_init(WATER_SENSOR_PIN);

adc_select_input(0); // Select ADC0


while (true) {

// Read ADC value

uint16_t value = adc_read();


// Print value to the serial console

printf("Water Level: %d\n", value);


// Delay for 200 milliseconds

sleep_ms(200);

}


return 0;

}



  1. Hardware Image:



  1. Output:

The output of the experiment is the digital value read from the water sensor, which is printed to the console. This value reflects the water level detected by the sensor, with higher values generally indicating more water.

In this Experiment I dip the water sensor slowly and deeply into the water so the value of the water sensor gradually increases.

  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

cmake_minimum_required(VERSION 3.13)


# Define the project name and set C++ standard

project(water_sensor_project C CXX)


# Include the Pico SDK

include(pico_sdk_import.cmake)


# Initialize the Pico SDK

pico_sdk_init()


# Add the executable target

add_executable(water_sensor

main.c

)


# Link the Pico SDK libraries

target_link_libraries(water_sensor

pico_stdlib

)


# Enable USB serial

pico_enable_stdio_usb(water_sensor 1)


# Enable UART serial

pico_enable_stdio_uart(water_sensor 1)


# Set the location for the build output

pico_add_extra_outputs(water_sensor)

  1. References:



These references provide comprehensive dsjfoihdsih

adjfoajfaskl;k;lk;lk;lasdkf;lssssssdddkas;lfksa

asjkdfjasl;jdsa'

jasfdljaslkjflkasjugiodsueqoiurqweiouwqoipqwu]jiojfalkdjsalkjsal

joiurpoqe[uwq

jaeporuepqoureqwpouwqopiuwqpwquwqpouwqepoqweupowqe\

ikwqpoeiwqpoiewpqoiweq

owqieopwqiop

Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)