Experiment 27: Water Level Sensor Experiment

From Diustou Wiki
Revision as of 17:28, 12 February 2025 by Yousimaier17 (talk | contribs) (Created page with "*Basic Experiment Kits For Arduino *Basic Experiment Kits For Raspberry Pi == Arduino == === Experimental Phenomenon === * Print the measured distance through the seri...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Arduino

Experimental Phenomenon

  • Print the measured distance through the serial port using the module.

Circuit Connection

  • 实验二十七:水位传感器实验 接线.png

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

  • 实验二十七:水位传感器实验 接线1.png

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

Example Download

Related Examples

Basic Experiment Kits and Examples for Arduino and Raspberry Pi
教程名2
  • 列表2
教程名3
  • 列表3
教程名4
  • 列表4
教程名5
  • 列表5
教程名6
  • 列表6