Lesson 15 Water Level Sensor

Share for us

Introduction

In this lesson, we will use a water level sensor to measure the depth of the water and display the result on I2C LCD1602.

Components

– 1 * SunFounder Uno board

– 1 * Breadboard

– 1 * USB data cable

– 1 * Water Level Sensor

– 1 * I2C LCD1602

– Several jumper wires

Experimental Principle

Water level sensor is a module that can sense the depth of water, whose core part is an amplification circuit composed of a transistor and several comb-shape PCB cables. When placed in water, the comb-shape cable will change its resistance with the depth of the water and convert the depth signal into an electrical signal. With an ADC on the SunFounder Uno board dealing with that signal, we can know the water depth changes.

Experimental Procedures

Step 1: Build the circuit

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)

Note: Here you need to add a library. Refer to the description in Lesson 4 previously in the manual.

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

Now, if you immerse the sensor in water, you can see the depth to which the sensor is soaked displayed on the I2C LCD1602.

Code

/************************************************
* name:Water Level Detection
* function: if you immerse the sensor in water, 
* you can see the depth to which the sensor is soaked displayed on the I2C LCD1602.
* connection:
* I2C LCD1602 SunFounder Uno R3
* VCC 5V
* GND GND
* SDA A4
* SCL A5
* Water Level Sensor SunFounder Uno R3
* – GND
* + VCC
* S A0
************************************************/
//Email:support@sunfounder.com
//Website:www.sunfounder.com#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line displayconst int waterSensor = 0;
int waterValue = 0;void setup()
{
  lcd.init(); //initialize the lcd
  lcd.backlight(); //open the backlight
  lcd.setCursor(0,0);
  lcd.print(” Water Sensor “);
}void loop()
{
  int waterValue = analogRead(waterSensor); // get water sensor value
  lcd.setCursor(6,1); //place cursor at 6 column,2 row
  lcd.print(waterValue); //value displayed on lcd
  delay(200); //delay 200ms
  lcd.setCursor(0,1);//place cursor at 1 column,2 row
  lcd.print(” “); //Add 16 spaces to make sure the remaining characters in Line 1, if any, are cleared
}