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
Note
The Raspberry Pi does not have analog input capabilities, so it needs a module like the PCF8591 ADC DAC Converter Module to read analog signals for processing.
In this lesson, we’ll learn how to read from a water level sensor using a Raspberry Pi. You’ll find out how to connect a water level sensor module to the PCF8591 for analog-to-digital conversion and monitor its output in real-time with Python.
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 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi 5 |
|
- |
|
Wiring
Code
import PCF8591 as ADC # Import PCF8591 module
import time # Import time for delay
ADC.setup(0x48) # Initialize PCF8591 at address 0x48
try:
while True: # Continuously read and print
print(ADC.read(1)) # Read from Water level sensor module at AIN1
time.sleep(0.2) # Delay of 0.2 seconds
except KeyboardInterrupt:
print("Exit") # Exit on CTRL+C
Code Analysis
Import Libraries:
This section imports necessary Python libraries. The
PCF8591library is used for interacting with the PCF8591 module, andtimeis for implementing delays in the code.import PCF8591 as ADC # Import PCF8591 module import time # Import time for delay
Initialize PCF8591 Module:
Here, the PCF8591 module is initialized. The address
0x48is the I²C address of the PCF8591 module. This is necessary for the Raspberry Pi to communicate with the module.ADC.setup(0x48) # Initialize PCF8591 at address 0x48
Main Loop and Reading Data:
The
tryblock includes a continuous loop that consistently reads data from the water level sensor module. TheADC.read(1)function captures the analog input from the sensor connected to channel 1 (AIN1) of the PCF8591 module. Incorporating atime.sleep(0.2)creates a 0.2-second pause between each reading. This not only helps in reducing CPU usage on the Raspberry Pi by avoiding excessive data processing demands, but also prevents the terminal from being overrun with rapidly scrolling information, making it easier to monitor and analyze the output.try: while True: # Continuously read and print print(ADC.read(1)) # Read from Water level sensor module at AIN1 time.sleep(0.2) # Delay of 0.2 seconds
Handling Keyboard Interrupt:
The
exceptblock is designed to catch a KeyboardInterrupt (like pressing CTRL+C). When this interrupt occurs, the script prints “exit” and stops running. This is a common way to gracefully exit a continuously running script in Python.except KeyboardInterrupt: print("exit") # Exit on CTRL+C