Experiment 27: Water Level Sensor Experiment
From Diustou Wiki
Contents
Arduino
Experimental Phenomenon
- Print the measured distance through the serial port using the module.
Circuit Connection
Reference Program
double temp,data; void setup() { Serial.begin(9600); } void loop() { temp=analogRead(A0); Serial.print("val = "); // Serial output of current value of val Serial.print(temp); if(temp < 10) { // If val is less than 10 Serial.println(" | dry"); // Indicates it's very dry, no water } else if(temp < 475) { // If less than 475 Serial.println(" | water level: ~ 0-1 cm"); // Water level is approximately 0-1 cm, and so on for the following statements } else if(temp < 515) { Serial.println(" | water level: ~ 1-2 cm"); } else if(temp < 535) { Serial.println(" | water level: ~ 2-3 cm"); } else if(temp < 550) { Serial.println(" | water level: ~ 3-4 cm"); } else { Serial.println(" | water level: over 4 cm"); } delay(1000); // Wait for 1 second }
Raspberry Pi
Circuit Connection
Program Execution
Python
- Install the gpiozero library
- You can use the following command to install the library:
sudo apt update sudo apt install python3-gpiozero
- For other systems on the Raspberry Pi, you can use the following command to install the library:
sudo pip3 install gpiozero
- Run the following command to view the GPIO pin definitions on the Raspberry Pi:
pinout
- Enable the SPI interface
sudo raspi-config Select Interfaces oOptions -> SPI -> “Would you like the SPI interface to be enabled? ”Select Yes -> “The SPI interface is enabled” Select Yes -> Select finish
- Shut down the Raspberry Pi. With the power off, connect the corresponding modules to the circuit according to the provided circuit connection, and then start the Raspberry Pi.
- Download the Raspberry Pi reference example, unzip the file, copy it to the user directory, and run it:
cd /home/pi/raspberrypi/27/python_gpiozero python sensor.py
- You will see the Raspberry Pi continuously printing the voltage value of the potentiometer. To exit, press ctrl+C.
- For more commands, please refer to the gpiozero documentation