Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 25: Water Level Sensor Module

In this lesson, you’ll learn how to use an ESP32 Development Board for reading a water level sensor. We’ll cover continuously monitoring the sensor’s analog value and displaying it on the serial monitor. This project provides a great opportunity to grasp sensor integration and analog data reading with Arduino, making it ideal for beginners in electronics and microcontroller programming.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

Water Level Sensor Module

-

Breadboard

BUY

Wiring

../_images/Lesson_25_Water_Level_esp32_bb.png

Code

Code Analysis

  1. Initializing the Sensor Pin:

    Before using the water level sensor, it’s pin number is defined using a constant variable. This makes the code more readable and easier to modify.

    const int sensorPin = 25;
    
  2. Setting Up Serial Communication:

    In the setup() function, the baud rate for serial communication is set. This is crucial for the Arduino to communicate with the computer’s serial monitor.

    void setup() {
      Serial.begin(9600);  // Start serial communication at 9600 baud rate
    }
    
  3. Reading Sensor Data and Outputting to Serial Monitor:

    The loop() function continuously reads the sensor’s analog value using analogRead() and outputs it to the serial monitor using Serial.println(). The delay(100) function makes the Arduino wait for 100 milliseconds before repeating the loop, controlling the rate of data reading and transmission.

    void loop() {
      Serial.println(analogRead(sensorPin));  // Read the analog value of the sensor and print it to the serial monitor
      delay(100);                             // Wait for 100 milliseconds
    }