[Diao Ye learns programming] Arduino hands-on (24) --- water level sensor module 3

insert image description here

The reference to 37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, in accordance with the concept of true knowledge (must be hands-on), for the purpose of learning and communication, here I am going to try and do more experiments one by one. Whether it is successful or not, it will be recorded ——Small progress or unsolvable problems, I hope to be able to throw bricks and spark jade.

[Arduino] 168 kinds of sensor module series experiment (data code + simulation programming + graphic programming)
Experiment 24: water level sensor drop sensor water depth detection module water level detection module Water depth Sensor

insert image description here

(6) The return status of the experimental serial port (slowly pour water into the cup, and the water level gradually rises)

insert image description here

insert image description here

insert image description here
(9) Hold the water level sensor and calibrate the serial port value of the actual water level. The dynamic waveform during calibration is shown in the figure below

insert image description here

(10) Scene graph during calibration experiment

The actual calibration experimental serial port value is between 100-240 (approximate value)

insert image description here
2. Program 2: Used to detect the presence of water (leakage, overflow, flood, rain, etc.)
(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序二:用于检测水的存在(泄漏、溢出、洪水、雨水等情况)
*/

#define LED_PIN     13
#define POWER_PIN   7
#define SIGNAL_PIN  A0
#define THRESHOLD   100

int value = 0; // 存储传感器值的变量

void setup() {
    
    
  Serial.begin(9600);
  pinMode(LED_PIN,   OUTPUT);   // 将 D13 引脚配置为输出,接LED
  pinMode(POWER_PIN, OUTPUT);   // 将 D7 引脚配置为配置为输出,接水位传感器+
  digitalWrite(POWER_PIN, LOW); // 关闭传感器
  digitalWrite(LED_PIN,   LOW); // 关闭LED
  Serial.println("检测准备就绪!");
}

void loop() {
    
    
  digitalWrite(POWER_PIN, HIGH);  // 打开传感器
  delay(10);                      // 等待 10 毫秒
  value = analogRead(SIGNAL_PIN); // 从传感器读取模拟值
  digitalWrite(POWER_PIN, LOW);   // 关闭传感器

  if (value > THRESHOLD) {
    
    
    Serial.println("检测到水");
    digitalWrite(LED_PIN, HIGH);  // 打开 LED
  } else {
    
    
    digitalWrite(LED_PIN, LOW);   // 关闭LED
  }
}

(2) The return status of the experimental serial port

insert image description here
(3) Experimental scene diagram (when water is detected, the LED will be lit)

insert image description here
3. Program 3: Detecting water conditions through water sensors
(1) Experimental open source simulation programming (Linkboy V4.63)

insert image description here
(2) The return status of the experimental serial port

insert image description here
4. Program 4: Measure the water level in three levels (low green LED, medium blue LED, high red LED)
(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序四:分三档测量水位(低绿色LED,中蓝色LED,高红色LED)
  使用:5MM三色RGB全彩LED模块
*/

void setup() {
    
    
  Serial.begin(9600);
  pinMode(8, OUTPUT); // 设置D8为输出
  pinMode(9, OUTPUT); // 设置D9为输出
  pinMode(10, OUTPUT); // 设置D10为输出
}

void loop() {
    
    
  int s1 = analogRead(A0);
  Serial.println(s1); //串口输出传感器值
  delay(50);

  if (s1 > 350 && s1 < 480 )
  {
    
    
    digitalWrite(10, HIGH);    // 中水位时蓝色LED亮
    Serial.println("中水位");
  }
  else
  {
    
    
    digitalWrite(10, LOW);       // 否则熄灭
  }

  if (s1 < 350 )
  {
    
    
    digitalWrite(9, HIGH);    // 低水位时绿色LED亮
    Serial.println("低水位");
  }
  else
  {
    
    
    digitalWrite(9, LOW);    // 否则熄灭
  }

  if (s1 > 480 )
  {
    
    
    digitalWrite(8, HIGH);  // 高水位时红色LED亮
    Serial.println("高水位");
  }
  else
  {
    
    
    digitalWrite(8, LOW);  // 否则熄灭
  }
  delay(1500);
}

(2) The return status of the experimental serial port

insert image description here
(3) Experimental scene diagram

insert image description here

5. Program 5: High water level sound and light alarm
(1) Experimental open source graphics programming (Mind+, Mixly, learning while editing)

insert image description here

(2) Experimental scene diagram

insert image description here

6. Program 6: Use the 1602 LCD display to display the water level sensor value
(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序六:使用1602 LCD显示屏显示水位传感器值
*/

#include <LiquidCrystal_I2C.h>
// 用接口引脚的编号初始化库
LiquidCrystal_I2C lcd(0x27, 16, 2);
// 将 LCD 地址设置为 0x27,用于 16 个字符和 2 行显示常量
int waterSensor = 0;
int waterValue = 0;

void setup() {
    
    
  lcd.init(); //初始化液晶屏
  lcd.backlight(); //打开背光
  lcd.setCursor(0, 0);
  lcd.print("Water Sensor");
}

void loop() {
    
    
  int waterValue = analogRead(waterSensor); //获取水传感器值
  lcd.setCursor(6, 1); //将光标放在6列2行
  lcd.print(waterValue); //值显示在lcd上
  delay(200); //延迟200ms
  lcd.setCursor(0, 1); //将光标放在1列2行
  //添加16个空格以确保第1行中剩余的字符(如果有)被清除
  lcd.print("                ");
}

(2) Experimental scene map (not placed in the water cup, the sensor value is 0)

insert image description here

(3) Put it in the water cup, the value of the water level sensor is 480

insert image description here

7. Procedure 7: Obtain the analog signal data of the water level sensor

(1) Arduino reference open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序七:获取水位传感器模拟信号数据
*/

int val = 0; //定义一个变量 val ,初始化值为0

void setup() {
    
    
  Serial.begin(9600); // 设置波特率为9600
}

void loop() {
    
    
  val = analogRead(A0); // 获取A0模拟口的数据,并赋值给 val 变量
  Serial.print("val = "); // 串口输出 val 当前的数据
  Serial.print(val);

  if(val < 200) {
    
     // 如果 val 小于200
    Serial.println(" | dry"); // 说明很干,没有水
  } else if(val < 450) {
    
     // 如果 小于 450
Serial.println(" | water level: ~ 0-1 cm"); 
// 水位大约在0-1cm ,以下语句如此类推
  } else if(val < 500) {
    
    
    Serial.println(" | water level: ~ 1-2 cm");
  } else if(val < 540) {
    
    
    Serial.println(" | water level: ~ 2-3 cm");
  } else if(val < 600) {
    
    
    Serial.println(" | water level: ~ 3-4 cm");
  } else {
    
    
    Serial.println(" | water level: over 4 cm");
  }
  delay(1000); // 等待1秒
}

(2) The return status of the experimental serial port

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/132242722