Lesson 5: Lake Water Level Monitor

0 10 Medium

Water is essential for human survival and plays a vital role in daily life and production. However, “water can carry a boat, but it can also overturn it.” Every year, flooding causes injuries and significant property loss.
For this reason, monitoring lake water levels is extremely important. Before modern technology existed, people carved water marks on stone pillars in lakes to record changes in water levels.

With the rapid development of science and technology, modern hydrological stations now use sensors—such as electronic water gauges, immersion-type level sensors, and ultrasonic level sensors—to measure water levels.
In this lesson, we will use the UNIHIKER M10 and an analog ultrasonic sensor to create a modern lake water level monitoring device.

Goal


Use an ultrasonic sensor to measure the water level in real time and draw a line chart on the UNIHIKER M10 screen to display water level changes.

Get to Know

1. Understand the concept of analog input signals
2. Learn the difference between digital and analog signals
3. Learn how the ultrasonic sensor works and how to use it

Materials List

HARDWARE LIST
1 UNIHIKER M10
1 USB Cable
1 Ultrasonic Distance Sensor
1 PH2.0-3Pin cable

Software:


Mind+ Programming Software x1(download at https://mindplus.cc/en)

Hands-on Practice


The Lake Water Level Monitor works by measuring the distance between the ultrasonic sensor and the water surface, converting that distance into water level height, and displaying the real-time water level change on the UNIHIKER screen. We will divide this project into three tasks:
Task 1: Read the Value of the Analog Ultrasonic Sensor
Read the value of the analog ultrasonic sensor and convert it into a distance reading to display on the UNIHIKER M10.
Task 2: Monitor Changes in Lake Water Level
Draw a schematic of the lake and display real-time dynamic data of the water level.
Task 3: Plot a Line Chart of Lake Water Levels
Measure the lake’s water level once every hour and plot the readings as a line chart on the UNIHIKER M10.

Task 1: Read the Value of the Analog Ultrasonic Sensor


1.Hardware Connection


The analog ultrasonic sensor is a dual-probe open-type distance module that outputs an analog voltage, which can be converted into distance.
Connect the sensor to the UNIHIKER M10’s analog pin P21, and then connect the UNIHIKER to the computer with a USB cable.
Note: For more information about the “analog ultrasonic sensor,” see the Knowledge Base.

2.Software Preparation


Open Mind+ and follow the diagram below to complete the software preparation process.

projectImage
3.Write a Program


After connecting the sensor to P21, how do we obtain the detected value?Use the “Read Analog Pin” block and select pin P21.

Next, use “show text” and “update text content” blocks to show the analog value on the UNIHIKER M10.

However, this analog value is not the distance we want. The analog ultrasonic sensor has a conversion formula: Distance = Analog Input × 520 / 4096

Make a variable “distance” to store the converted distance value. Then update the text display to show this variable instead of the analog value.

The converted value will contain many decimal places. For easier reading, use the “save decimal places” block to keep two decimal places.

4.Run the Program


Click Run. The UNIHIKER M10 will show the distance measured by the ultrasonic sensor in real time. A comparison between ruler measurement and ultrasonic measurement is shown below.
Note: Ultrasonic measurement has a certain error, typically about ±1% of the actual distance.

Task 2: Monitor Changes in Lake Water Level


1.Write a Program


Before coding, let’s analyze the display requirements,shown as following:First, the interface needs to show the current water level height detected by the ultrasonic sensor. Then, a simple lake-level trend graph should be drawn so that the water level changes can be monitored in real time.The interface display on the UNIHIKER board is shown below.

(1)Draw the Water Container Frame
The container frame is a simple open rectangle made of three line segments. Use the “display line segment” block to draw the frame.

How do we determine the warning water level?
Using a 15 cm cup as our “lake,” the warning level is set at 2/3 of the height. This corresponds to coordinate (10, 158) on the screen.Use “display line segment” block to create a red warning line, and add text “Warning: 10 cm”.

(2)Draw the Water Level Rise/Fall Display
At the “Python main program start”, hide the water level graph and the text.

Make another variable named “distance” to store the distance measured by the ultrasonic sensor from the water surface.

After obtaining the lake water height, how can we display this real-time height on the UNIHIKER screen? Use the “map” block to map the value of “distance” from 0–15 to the lake-level graphic range on the UNIHIKER M10 screen (318–78).
Make another variable named “current_level” to store the mapped value.
For easier reading, use the “save decimal places” block to keep only the integer part of the “distance” value.

Finally, use the “update number parameter” block to set the start Y-coordinate of the lake-level line to the value stored in “current_level”.

Use the “update number parameter” block to set the display position of the current water level text to “current_level ”− 10. Then, use the “join” block to combine the label “Current :”, the variable distance, and the unit (cm).
Finally, use the “update text content” block to refresh the displayed data in real time.
The complete program is shown below:
Note: Here, we use 15 − distance because the ultrasonic sensor is mounted at the top of the cup, giving a maximum height of 15 cm.The variable distance represents the distance measured from the sensor to the water surface. Therefore, the actual water level height is calculated as: Water Level Height = 15 − distance.

2.Run the Program


Run the program, and the UNIHIKER will dynamically display the rising and falling water level.
Note: For safety, no electronic component should come into direct contact with water.

3.Have a try


Currently, when the water level exceeds 10 cm, the red warning line is covered by the water level line.
Modify the program so that the warning line and warning text are always on top.
Hint: Place the warning line and text drawing blocks after the real-time water level drawing blocks.

Task 3: Plot a Line Chart of Lake Water Levels


1.Write a Program


In Task 2, we can observe real-time changes, but not trends—such as which period increased the fastest.
To visually analyze the changes, we can measure the water level at intervals and plot a line chart.

(1)Display the Coordinate Grid
To draw a line chart, import the coordinate grid image into the UNIHIKER file system.

Use Display Image to show the grid, and use Update Numeric Parameter to set its height to 320.

And then make two variables: “distance” and “y”. The distance variable stores the converted distance value, while the y variable stores the mapped Y-coordinate on the UNIHIKER screen.

(2)Plotting Points and Connecting Lines
Use the “fill circle” block to plot each point. First, determine the X and Y coordinates required by the block: the Y-coordinate comes from the variable “y”, and the X-coordinate starts at the intersection of the axes, with a spacing of 15 units between each point.
Make a variable named “x” and set its initial value to 20. After drawing each point, increase x by 15.


Use the “display line segment” block to connect points. When the second point appears on the UNIHIKER M10 screen, the first and second points should be connected with a line segment. When the variable “x” > 20, make another variable “y1” to store the previous point’s y value.
Thus, the starting point of the line segment has the coordinates (x − 15, y1), and the ending point has the coordinates (x, y).
The complete program is shown below:
Note:The wait 10-second is added so that the plotting and line-drawing effects can be observed quickly. If you want the system to measure the water level once every hour, simply change the delay duration to 3600 seconds.

2.Run the Program


Run the program. Add water or pour out water continuously—the UNIHIKER M10 will draw the water level line chart in real time.

Knowledge Base


1.What Is an Analog Signal


An analog signal has infinitely many values within a certain range. On UNIHIKER M10 analog pins, voltages from 0–3.3V are mapped to values 0–4095.


The sensor outputs a 0–4095 analog value, which can be converted to distance using:


Distance = Analog Input × 520 / 4096


From this, the maximum measurable distance is 520 cm.

2.Difference Between Analog and Digital Signals


A digital signal has only two possible values (0 V and 3.3 V).
On the UNIHIKER board, these correspond to high level and low level:


· A high level represents digital 1, which corresponds to 3.3 V.
· A low level represents digital 0, which corresponds to 0 V.
This means that if an LED is controlled using a digital signal, it can only have two states: on or off.

An analog signal can take any value between 0 and 3.3 V. When this voltage range is mapped to the UNIHIKER M10’s analog pin, it becomes a value between 0 and 4095.This means that if an LED is controlled using an analog signal, its brightness can have many different levels.
The LED’s brightness will vary smoothly with the analog value — from gradually brightening to gradually dimming.


3.Understanding the Analog Ultrasonic Sensor


The analog ultrasonic distance sensor is an open-type dual-probe ultrasonic module that outputs an analog voltage.
How does this module measure distance?
It contains two probes—one transmits the ultrasonic signal, and the other receives the reflected signal after it bounces back from an obstacle.

Since the UNIHIKER M10’s analog input pins use a 12-bit ADC, the returned 0–3.3 V signal is converted into a value between 0 and 4095.
Thus, when reading the analog value from pin P21, the sensor output will be a number within this range.
By substituting this analog value into the distance-conversion formula, the measured distance can be obtained.


4.Command Overview


Challenge


In this lesson, we learned how to use an ultrasonic sensor for distance measurement. However, the ultrasonic sensor actually outputs an analog voltage, and only after applying a conversion formula can we obtain the distance we want.
Is there a simpler way to read the distance directly?
Yes— the pinpong library provides a dedicated module for the analog ultrasonic distance sensor.
Click “Extensions”, then find “Analog Ultrasonic ranging” under the pinpong library and add it.

The analog ultrasonic sensor module includes only two blocks:
· Analog Ultrasonic Sensor Initial Pin
· Read Analog Ultrasonic wave(cm)
The first block is used to initialize the pin, and the second block reads the distance detected by the ultrasonic sensor. Try using these blocks to measure distance. The usage is shown below:

icon Lesson 5.zip 832KB Download(0)
License
All Rights
Reserved
licensBg
0