Arduino Water Level Sensor with Python Live Data Plotting

In this article, you will learn how to use an Arduino Water level sensor and plot the data in Python. Creating this project will let you do serial communication from Arduino to Python. This is very useful if you are gathering data live or constantly monitoring liquids.

Arduino Water Level Sensor with Python Live Data Plotting: Creating the Device

Here are the steps in interfacing the sound module in Arduino.

  1. Gathering the Components

    The first thing to do is to collect the hardware components for the Arduino device.
    Arduino Uno
    Water Level Sensor

  2. Connecting the Components

    Connect the components to the Arduino Uno. Please refer to the wiring diagram below.

  3. Coding the Arduino

    Third step is about coding the Arduino device.

  4. Upload the Sketch

    After coding the Arduino, upload the sketch.

  5. Coding with Python

    Next is to create a project in Python and write codes that will read the serial monitor of the Arduino, save it in a CSV file, and plot it on a graph.

Watch the video here to see the full running source code of the Arduino Water Level Sensor with Python Live Data Plotting

Arduino Water Level Sensor with Python Live Data Plotting: Detailed Explanation

To start this project, you need the following:

QtyComponent
1Arduino UNO
1Water Level Sensor

Arduino UNO

We will be using an Arduino Uno microprocessor board. Arduino Uno is suitable for any projects and is the cheapest and widely used microprocessor board in the Arduino family. This is great for all kinds of IoT projects.

Arduino Uno
Arduino Uno

Water Level Sensor

Below is a Water level Sensor. It has 3 pins for Power, Ground, and Signal. Also, it has a Power LED indicator. The sensor gives a signal when the exposed parallel conductors are submerged into a liquid. The more it is submerged, the greater the data signal value.

Keep in mind that the range of the value is based on the liquid. Better test it on the intended liquid you are planning to use it.

A warning – you have to be careful when handling the sensor though. You can only submerge the sensor up to exposed conductors. Also, you cannot submerge it in a long period of time.

Wiring Diagram

For the wiring, you just need to connect the Signal to an Analog pin and the Ground to the Ground pin. To power it though you need it to connect it to a Digital pin. Digital pins can also power sensors and in this case, it is used to power the sensor to help with the longevity of the sensor. Providing it with constant power (while constantly in contact with water) can destroy the sensor.

Water Sensor Wiring Diagram
Water Sensor Wiring Diagram

Arduino Water Level Sensor with Python Live Data Plotting: Arduino Code

For the code, you just need to allow the sensor to read every second and display it on the serial monitor. No texts or labels – just the value. Upload this to the Arduino board.

Python Code

Now that our device is ready, you can now create live data plotting. First is to install pyserial and matplotlib. Pyserial is the package that lets you communicate with the Arduino while the matplotlib creates the data plotting.

To do this, open PyCharm 2021 and go to View>Tool Windows, Python Packages.

packages window
packages window

A window will appear below. search for pyserial and matplotlib. Click install and wait for it to finish. after that you are good to go.

install packages
Search and install packages

Now create a new project in PyCharm and paste the code below.

Output

Now, run the program. Successful readings will result to a higher value while zero means the sensor is dry.

Water level sensor live data plotting
Water level sensor live data plotting

Summary

And that’s it! You have successfully created an Arduino Water Level Sensor with Python Live Data Plotting! All you need to do is code the sensor and assemble. Then, create a project in Python and install Pyserial and Matplotlib packages, check the code above and run the program! This project is versatile and can be combined with other projects.

Download

Inquiries

Feel free to write your questions about the Arduino Water Level Sensor with Python Live Data Plotting at the comments below.

Leave a Comment