数字触摸开关Touch

1. 简介

这是一款可以进行液位校准、自检以及灵敏度可调的非接触式液位传感器,适用于平面的非金属容器的液位检测,板载LED灯可实时反馈传感器的状态。有两种数据输出方式:UART模式和IO模式。两种模式皆可进行自检和校准。该传感器模块可直连Arduino或者其他主控器,应用于水箱检测、浇花、鱼缸等液位检测等场景。

注意:UART模式下可以通过串口协议修改灵敏度,IO模式下需要通过调节电阻阻值来调节灵敏度。

2. 产品参数

  • 信号类型:UART、IO
  • 工作电压:3.3 ~ 5VDC
  • 工作温度:-40 ~ 85℃
  • 液位检测通道:1 通道
  • 容器厚度:< 2mm
  • 灵敏度等级可配置,0 ~ 7,等级数值越大,灵敏度越低

3. 引脚说明

4. 主要 API 接口函数介绍

/**
 * @brief DFRobot_Nilometer abstract class constructor. Construct serial port detection object.(eUARTDetecteMode)
 * @param s:  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object
 * @param en: The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor.
 */
  DFRobot_Nilometer(Stream *s, int en);//eUARTDetecteMode
/**
 * @brief DFRobot_Nilometer abstract class constructor.Construction level one-to-one detection object.(eLevelDetecteMode)
 * @param out:  The IO pin of MCU which is connected to OUT pin of Non-contact liquid level sensor.
 * @param en: The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor.
 * @param test :  The IO pin of MCU which is connected to TEST pin of Non-contact liquid level sensor. 
 * @param s:  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object
 */
  DFRobot_Nilometer(int out, int en, int test, Stream *s);//eLevelDetecteMode
  ~DFRobot_Nilometer();
/**
 * @brief liquide level sensor initialization.
 * @return initialization state:
 * @n      0:  sucess
 * @n      0xAA(170):代表传感器从来没有校准过,需要先对传感器进行校准(UART)
 * @n      -1:  fail
 */
  int begin();
/**
 * @brief Detect the presence or absence of water.
 * @return water state:
           true: There is water at this location.
           false: There is no water at this location.
 */
  bool detectWater();
/**
 * @brief Self check which can update to get the current sensitivity and calibration mode.
 * @n In UART detected mode: You must use TX, RX and EN pin of sensor.
 * @n In level one-to-one detection mode: You must use OUT, TEST, EN, and TX pin of sensor
 * @return selfCheck update state:
 * @n      true: update sucess, and you can call getSensitivity to get current sensitivity ,and to call getCalibrationMode to get current calibration mode.
 * @n      false: update fail.
 */
  bool selfCheck();
/**
 * @brief Water level calibration, note: Before calibration, it is necessary to ensure that there is no water 
 * @n in the water detection area, and do not touch the water detection area.
 * @n In level one-to-one detection mode: You must use EN and TEST pin of the sensor can successfully calibrated.
 * @n In UART detected mode: You must use EN pin of the sensor can successfully calibrated.
 * @return Calibration state:
 * @n      true: Calibration sucess.
 * @n      false: Calibration fail.
 */
  bool calibration();
  
/**
 * @brief Get calibration mode of sensor.
 * @n @n note:Before using this function,you need to selfCheck function to update sensitivity ache.
 * @return Calibration mode:
           0 or CALIBRATION_MODE_LOWER_LEVEL: Only calibrate the lower water level.
           1 or CALIBRATION_MODE_LOWER_AND_UPPER_LEVEL: Calibrate the upper and lower water level.
           0xFF:   error mode.
 */
  uint8_t getCalibrationMode();
/**
 * @brief Get calibration mode of sensor's description.
 * @param mode  calibration mode.
 * @n     CALIBRATION_MODE_LOWER_LEVEL or 0           :  only calibrate the lower water level.
 * @n     CALIBRATION_MODE_LOWER_AND_UPPER_LEVEL or 1 :  Up and down water level calibration.
 * @n     2~255                                       :  Error calibration mode
 * @return description: the string of calibration mode of sensor's description.
 */
  String getCalibModeDescription(uint8_t mode);
/**
 * @brief Get sensitivity level of the channel of sensor.
 * @n @n note:Before using this function,you need to selfCheck function to update sensitivity ache.
 * @return sensitivity level: 
 * @n     eSensitivityLevel0 or 0: Sensitivity level 1, the most Sensitivity level.
 * @n     eSensitivityLevel1 or 1: Sensitivity level 2
 * @n     eSensitivityLevel2 or 2: Sensitivity level 3
 * @n     eSensitivityLevel3 or 3: Sensitivity level 4
 * @n     eSensitivityLevel4 or 4: Sensitivity level 5
 * @n     eSensitivityLevel5 or 5: Sensitivity level 6
 * @n     eSensitivityLevel6 or 6: Sensitivity level 6
 * @n     eSensitivityLevel7 or 7: Sensitivity level 7,the least Sensitivity Level.
 * @n     0xFF                   : Error sensitivity level value
 */
  uint8_t getSensitivity();
/**
 * @brief Clear recive buffer of UART, only use in UART deteceted mode.
 */
  void flush();
/**
 * @brief If you use calibration, you can call the function to detected that the calibration is completed.
 * @n Before using this function, you need to call LowerWaterLevelCalibration or LowerWaterLevelCalibration and UpperWaterLevelCalibration.
 * @return calibration state:
 * @n      true: calibration completed.
 * @n      false: calibration failed.
 */
  bool checkCalibrationState();
  /**
 * @brief DFRobot_SCW8916B_IO abstract class constructor.Construction level one-to-one detection object.(eLevelDetecteMode)
 * @param out  :  The IO pin of MCU which is connected to OUT pin of Non-contact liquid level sensor.
 * @param en   :  The IO pin of MCU which is connected to EN pin of Non-contact liquid level sensor. 
 * @param test :  The IO pin of MCU which is connected to TEST pin of Non-contact liquid level sensor. 
 * @param s    :  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object.
 * @n note: If you call selfCheck, you must use en, test and s, and if you call lowerWaterLevelCalibration or UpperWaterLevelCalibration,
 * @n you must use en and test pin.
 */
  DFRobot_SCW8916B_IO(int out, int en = -1, int test = -1, Stream *s = NULL);
  /**
 * @brief DFRobot_SCW8916B_UART abstract class constructor. Construct serial port detection object.(eUARTDetecteMode)
 * @param s:  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object
 * @param en: The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor.when you call selfCheck,
 * @n setSensitivityLevel, LowerWaterLevelCalibration,or UpperWaterLevelCalibration function, you must use EN pin.
 */
  DFRobot_SCW8916B_UART(Stream *s, int en = -1);
/**
 * @brief Set sensitivity level of the channel of sensor.The higher the sensitivity level, the lower the sensitivity.Ranging 0~7.
 * @n note: This function only support eUARTDetecteMode, not using eLevelDetecteMode.
 * @param level:   the enum varible of eSensitivityLevel_t or 0~7
 * @n     eSensitivityLevel0 or 0: Sensitivity level 0, the most Sensitivity level.
 * @n     eSensitivityLevel1 or 1: Sensitivity level 1
 * @n     eSensitivityLevel2 or 2: Sensitivity level 2
 * @n     eSensitivityLevel3 or 3: Sensitivity level 3
 * @n     eSensitivityLevel4 or 4: Sensitivity level 4
 * @n     eSensitivityLevel5 or 5: Sensitivity level 5
 * @n     eSensitivityLevel6 or 6: Sensitivity level 6
 * @n     eSensitivityLevel7 or 7: Sensitivity level 7,the least Sensitivity Level.
 *
 * @return status: return config state.
 * @n      true:  Set sensitivity sucess.
 * @n      false:  Set sensitivity fail.
 */
  bool setSensitivityLevel(eSensitivityLevel_t level);
  bool setSensitivityLevel(uint8_t level);

5. 使用教程

5.1 准备

5.2 连接示意图


5.3 样例代码 1 - selfCheck

代码介绍:模块进行自检。

样例代码的目的:通过模块自检,可以检测模块的接线是否正确。自检成功后,可以获得模块当前设置的灵敏度和液位校准模式等信息。

注意:UART模式和IO模式都可进行自检,区别在于接线不同。

IO模式传感器引脚与主控引脚对应图

注:IO模式下使用传感器模块的TX引脚是为了在Arduino IDE的串口监视器中看到打印信息。

sensor pin Leonardo/Mega2560/M0 UNO ESP8266 ESP32
TEST 13 13 13/D2 13/D7
OUT 10 10 15/D4 5/D8
EN 2 2 2/D5 2/D9
VCC VCC VCC VCC VCC
GND GND GND GND GND
RX No connection No connection No connection No connection
TX Serial1 RX1 4 4/D7(RX) D3

UART模式传感器引脚与主控引脚对应图

sensor pin Leonardo/Mega2560/M0 UNO ESP8266 ESP32
TEST No connection No connection No connection No connection
OUT No connection No connection No connection No connection
EN 2 2 2/D5 2/D9
VCC VCC VCC VCC VCC
GND GND GND GND GND
RX Serial1 TX1 5 5/D6(TX) D2
TX Serial1 RX1 4 4/D7(RX) D3
#include "DFRobot_SCW8916B.h"
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
#include <SoftwareSerial.h>
#endif

//默认打开,此时使用UART通信, 屏蔽后使用IO口通信
#define UART_COMMUNICATION

#define EN     2      /**<The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor>*/

#ifdef UART_COMMUNICATION
/**
 * @brief DFRobot_SCW8916B_UART abstract class constructor. Construct serial port detection object.(eUARTDetecteMode)
 * @param s:  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object
 * @param en: The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor.when you call selfCheck,
 * @n setSensitivityLevel, LowerWaterLevelCalibration,or UpperWaterLevelCalibration function, you must use EN pin.
 */
  #if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
  DFRobot_SCW8916B_UART liquid(/*s =*/&mySerial, /*en =*/EN);
  #else
  DFRobot_SCW8916B_UART liquid(/*s =*/&Serial1, /*en =*/EN);
  #endif
#else
  #if defined(ESP32)
  #define OUT     5          /**<The IO pin of MCU which is connected to the OUT pin of Non-contact liquid level sensor>*/
  #else
  #define OUT     10          /**<The IO pin of MCU which is connected to the OUT pin of Non-contact liquid level sensor>*/
  #endif
  #define TEST    13          /**<The IO pin of MCU which is connected to the TEST pin of Non-contact liquid level sensor>*/
/**
 * @brief DFRobot_SCW8916B_IO abstract class constructor.Construction level one-to-one detection object.(eLevelDetecteMode)
 * @param out  :  The IO pin of MCU which is connected to OUT pin of Non-contact liquid level sensor.
 * @param en   :  The IO pin of MCU which is connected to EN pin of Non-contact liquid level sensor. 
 * @param test :  The IO pin of MCU which is connected to TEST pin of Non-contact liquid level sensor. 
 * @param s    :  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object.
 * @n note: If you call selfCheck, you must use en, test and s, and if you call lowerWaterLevelCalibration or UpperWaterLevelCalibration,
 * @n you must use en and test pin.
 */
  #if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
  DFRobot_SCW8916B_IO liquid(/*out =*/OUT, /*en =*/EN, /*test =*/TEST, /*s =*/&mySerial);
  #else
  DFRobot_SCW8916B_IO liquid(/*out =*/OUT, /*en =*/EN, /*test =*/TEST, /*s =*/&Serial1);
  #endif
#endif
void setup() {
  Serial.begin(115200);                                                                               //Serial USB Initialization
  while(!Serial){                                                                                     //Wait for USB serial port to connect. Needed for native USB port only
  }

#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  mySerial.begin(9600);
#elif defined(ESP32)
  Serial1.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#else
  Serial1.begin(9600);
#endif

  Serial.print("Initialization sensor...");
  int error = 0;
/**
 * @brief liquide level sensor initialization.
 * @return initialization state:
 * @n      0:  sucess
 * @n      0xAA(170):代表传感器从来没有校准过,需要先对传感器进行校准(UART)
 * @n      -1:  fail
 */
  while((error = liquid.begin()) != 0){
      Serial.print("failed. \nError code: ");
      Serial.println(error);
      if(error == ERR_CALIBRATION_CODE){
          Serial.println("You need to use calibration.ino to calibration sensor.");
      }else{
          Serial.println("Please check whether the hardware connection or configuration parameter is wrong.");
      }
      delay(1000);
      Serial.print("Initialization sensor...");
  }
  Serial.println("done.");

/**
 * @brief Self check which can update to get the current sensitivity and calibration mode.
 * @n In UART detected mode: You must use TX, RX and EN pin of sensor.
 * @n In level one-to-one detection mode: You must use OUT, TEST, EN, and TX pin of sensor
 * @return selfCheck update state:
 * @n      true: update sucess, and you can call getSensitivity to get current sensitivity ,and to call getCalibrationMode to get current calibration mode.
 * @n      false: update fail.
 */
  bool flag = liquid.selfCheck();
  if(flag){
      Serial.println("Self check sucess.");
      Serial.print("Current Sensitivity level(0~7): ");
      Serial.println(liquid.getSensitivity());

      Serial.print("Cailibration mode: ");
      /**
       * @brief Get calibration mode of sensor.
       * @n @n note:Before using this function,you need to selfCheck function to update sensitivity ache.
       * @return Calibration mode:
                 0 or CALIBRATION_MODE_LOWER_LEVEL: Only calibrate the lower water level.
                 1 or CALIBRATION_MODE_LOWER_AND_UPPER_LEVEL: Calibrate the upper and lower water level.
                 0xFF:   error mode.
       */
      uint8_t mode = liquid.getCalibrationMode();
      Serial.println(mode);
      Serial.print("Cailibration mode description: ");
      /**
       * @brief Get calibration mode of sensor's description.
       * @param mode  calibration mode.
       * @n     CALIBRATION_MODE_LOWER_LEVEL or 0           :  only calibrate the lower water level.
       * @n     CALIBRATION_MODE_LOWER_AND_UPPER_LEVEL or 1 :  Up and down water level calibration.
       * @n     2~255                                       :  Error calibration mode
       * @return description: the string of calibration mode of sensor's description.
       */
      Serial.println(liquid.getCalibModeDescription(mode));
  }else{
      Serial.println("Self check failed.");
  }

}

void loop() {

}

结果

串口打印结果

5.4 样例代码 2 - calibration

代码介绍:液位检测模块进行校准操作。

样例代码的目的:通过校准使传感器模块更加适用于当前环境或解决长时间使用后测量不准确的问题。

注意:UART模式和IO模式都可进行校准,区别在于接线不同。

IO模式传感器引脚与主控引脚对应图

注:IO模式下使用传感器模块的TX引脚是为了在Arduino IDE的串口监视器中看到打印信息。

sensor pin Leonardo/Mega2560/M0 UNO ESP8266 ESP32
TEST 13 13 13/D2 13/D7
OUT 10 10 10/D3 5/D8
EN 2 2 2/D5 2/D9
VCC VCC VCC VCC VCC
GND GND GND GND GND
RX No connection No connection No connection No connection
TX Serial1 RX1 4 4/D7(RX) D3

UART模式传感器引脚与主控引脚对应图

sensor pin Leonardo/Mega2560/M0 UNO ESP8266 ESP32
TEST No connection No connection No connection No connection
OUT No connection No connection No connection No connection
EN 2 2 2/D5 2/D9
VCC VCC VCC VCC VCC
GND GND GND GND GND
RX Serial1 TX1 5 5/D6(TX) D2
TX Serial1 RX1 4 4/D7(RX) D3
#include "DFRobot_SCW8916B.h"
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
#include <SoftwareSerial.h>
#endif

#define STEP1_START_CALIB       1
#define STEP2_END_CALIB         2

//默认打开,此时使用UART通信, 屏蔽后使用IO口通信
#define UART_COMMUNICATION

#define EN     2      /**<The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor>*/

#ifdef UART_COMMUNICATION
/**
 * @brief DFRobot_SCW8916B_UART abstract class constructor. Construct serial port detection object.(eUARTDetecteMode)
 * @param s:  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object
 * @param en: The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor.when you call selfCheck,
 * @n setSensitivityLevel, LowerWaterLevelCalibration,or UpperWaterLevelCalibration function, you must use EN pin.
 */
  #if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
  DFRobot_SCW8916B_UART liquid(/*s =*/&mySerial, /*en =*/EN);
  #else
  DFRobot_SCW8916B_UART liquid(/*s =*/&Serial1, /*en =*/EN);
  #endif
#else
  #if defined(ESP32)
  #define OUT     5          /**<The IO pin of MCU which is connected to the OUT pin of Non-contact liquid level sensor>*/
  #else
  #define OUT     10          /**<The IO pin of MCU which is connected to the OUT pin of Non-contact liquid level sensor>*/
  #endif
  #define TEST    13          /**<The IO pin of MCU which is connected to the TEST pin of Non-contact liquid level sensor>*/
/**
 * @brief DFRobot_SCW8916B_IO abstract class constructor.Construction level one-to-one detection object.(eLevelDetecteMode)
 * @param out  :  The IO pin of MCU which is connected to OUT pin of Non-contact liquid level sensor.
 * @param en   :  The IO pin of MCU which is connected to EN pin of Non-contact liquid level sensor. 
 * @param test :  The IO pin of MCU which is connected to TEST pin of Non-contact liquid level sensor. 
 * @param s    :  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object.
 * @n note: If you call selfCheck, you must use en, test and s, and if you call lowerWaterLevelCalibration or UpperWaterLevelCalibration,
 * @n you must use en and test pin.
 */
  #if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
  DFRobot_SCW8916B_IO liquid(/*out =*/OUT, /*en =*/EN, /*test =*/TEST, /*s =*/&mySerial);
  #else
  DFRobot_SCW8916B_IO liquid(/*out =*/OUT, /*en =*/EN, /*test =*/TEST, /*s =*/&Serial1);
  #endif
#endif

void setup() {
  Serial.begin(115200);
  while(!Serial){                                                     //Waiting for USB Serial COM port to open.
  }

#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  mySerial.begin(9600);
#elif defined(ESP32)
  Serial1.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#else
  Serial1.begin(9600);
#endif
/**
 * @brief liquide level sensor initialization.
 * @return initialization state:
 * @n      0:  sucess
 * @n      0xAA(170):代表传感器从来没有校准过,需要先对传感器进行校准(UART)
 * @n      -1:  fail
 */
  int errCode = liquid.begin();
  Serial.print("Error code: ");
  Serial.println(errCode);
/**
 * @brief Self check which can update to get the current sensitivity and calibration mode.
 * @n In UART detected mode: You must use TX, RX and EN pin of sensor.
 * @n In level one-to-one detection mode: You must use OUT, TEST, EN, and TX pin of sensor
 * @return selfCheck update state:
 * @n      true: update sucess, and you can call getSensitivity to get current sensitivity ,and to call getCalibrationMode to get current calibration mode.
 * @n      false: update fail.
 */
  bool selfFlag = liquid.selfCheck();
  if(selfFlag){
      Serial.println("Self check sucess.");
      Serial.print("Cailibration mode: ");
      /**
       * @brief Get calibration mode of sensor.
       * @n @n note:Before using this function,you need to selfCheck function to update sensitivity ache.
       * @return Calibration mode:
                 0 or CALIBRATION_MODE_LOWER_LEVEL: Only calibrate the lower water level.
                 1 or CALIBRATION_MODE_LOWER_AND_UPPER_LEVEL: Calibrate the upper and lower water level.
                 0xFF:   error mode.
       */
      uint8_t mode = liquid.getCalibrationMode();
      Serial.println(mode);
      Serial.print("Cailibration mode description: ");
      /**
       * @brief Get calibration mode of sensor's description.
       * @param mode  calibration mode.
       * @n     CALIBRATION_MODE_LOWER_LEVEL or 0           :  only calibrate the lower water level.
       * @n     CALIBRATION_MODE_LOWER_AND_UPPER_LEVEL or 1 :  Up and down water level calibration.
       * @n     2~255                                       :  Error calibration mode
       * @return description: the string of calibration mode of sensor's description.
       */
      Serial.println(liquid.getCalibModeDescription(mode));
      if (mode == CALIBRATION_MODE_LOWER_LEVEL){
          bool calibFlag = false;
          uint8_t calibStep = STEP1_START_CALIB;
          Serial.println("Keep the water inspection area free of water and do not touch the water inspection area in 2s");
          delay(2000);          /**<延时2秒,校准前准备,请保证在校准期间,保持检水区域无水,且不要触摸检水区域*/
          Serial.println("Starting calibration...");
          while(1){
              if(calibStep == STEP1_START_CALIB){
                 /**
                  * @brief Water level calibration, note: Before calibration, it is necessary to ensure that there is no water 
                  * @n in the water detection area, and do not touch the water detection area.
                  * @n In level one-to-one detection mode: You must use EN and TEST pin of the sensor can successfully calibrated.
                  * @n In UART detected mode: You must use EN pin of the sensor can successfully calibrated.
                  * @return Calibration state:
                  * @n      true: Calibration sucess.
                  * @n      false: Calibration fail.
                  */
                  calibFlag = liquid.calibration();
                  if(!calibFlag){
                      Serial.println("Calibration failed. \n");
                      continue;
                  }
                  calibStep = STEP2_END_CALIB;
              }
              if(calibStep == STEP2_END_CALIB){
                  /**
                   * @brief If you use calibration, you can call the function to detected that the calibration is completed.
                   * @n Before using this function, you need to call LowerWaterLevelCalibration or LowerWaterLevelCalibration and UpperWaterLevelCalibration.
                   * @return calibration state:
                   * @n      true: calibration completed.
                   * @n      false: calibration failed.
                   */
                  calibFlag = liquid.checkCalibrationState();
                  if(!calibFlag){
                      Serial.println("Calibration failed.");
                      Serial.println("Keep the water inspection area free of water and do not touch the water inspection area in 2s");
                      delay(2000);          /**<延时2秒,校准前准备,请保证在校准期间,保持检水区域无水,且不要触摸检水区域*/
                      Serial.println("Starting calibration...");
                      calibStep = STEP1_START_CALIB;
                      continue;
                  }
                  break;
              }
          }
          Serial.println("End calibration.");
      }else{
          Serial.println("Does not support this calibration mode");
      }
  }else{
      Serial.println("Self check failed");
  }

}

void loop() {
  bool flag = liquid.detectWater();      /**<Detect whether there is water at water level------>true: have water, false: no water.*/
  if(flag){
       Serial.print("Have water: ");
  }else{
       Serial.print("No water:   ");
  }
  Serial.println(flag);                 /**<Print water state at the water level, 0: No water, 1: Have water. */
}

结果

串口打印结果

5.5 样例代码 3 - setAndGetSensitivity

代码介绍:设置传感器模块的灵敏度。

样例代码的目的:通过设置模块的灵敏度,使传感器模块更适合当前使用环境。

注意:只有UART模式可以通过软件修改传感器模块的灵敏度,IO模式需要通过修改电阻的阻值来贴姐灵敏度


#include "DFRobot_SCW8916B.h"
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
#include <SoftwareSerial.h>
#endif

#define EN              2    /**<The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor>*/

/**
 * @brief DFRobot_SCW8916B_UART abstract class constructor. Construct serial port detection object.(eUARTDetecteMode)
 * @param s:  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object
 * @param en: The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor.when you call selfCheck,
 * @n setSensitivityLevel, LowerWaterLevelCalibration,or UpperWaterLevelCalibration function, you must use EN pin.
 */
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
DFRobot_SCW8916B_UART liquid(/*s =*/&mySerial, /*en =*/EN);
#else
DFRobot_SCW8916B_UART liquid(/*s =*/&Serial1, /*en =*/EN);
#endif
void setup() {
  Serial.begin(115200);
  while(!Serial){
  }

#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  mySerial.begin(9600);
#elif defined(ESP32)
  Serial1.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
#else
  Serial1.begin(9600);
#endif

  Serial.print("Initialization sensor...");
  int error = 0;
/**
 * @brief liquide level sensor initialization.
 * @return initialization state:
 * @n      0:  sucess
 * @n      0xAA(170):代表传感器从来没有校准过,需要先对传感器进行校准(UART)
 * @n      -1:  fail
 */
  while((error = liquid.begin()) != 0){
      Serial.print("failed. \nError code: ");
      Serial.println(error);
      if(error == ERR_CALIBRATION_CODE){
          Serial.println("You need to use calibration.ino to calibration sensor.");
      }else{
          Serial.println("Please check whether the hardware connection or configuration parameter is wrong.");
      }
      delay(1000);
      Serial.print("Initialization sensor...");
  }
  Serial.println("done.");
  
  Serial.print("Set sensitivity level...");
/**
 * @brief Set sensitivity level of the channel of sensor.The higher the sensitivity level, the lower the sensitivity.Ranging 0~7.
 * @n note: This function only support eUARTDetecteMode, not using eLevelDetecteMode.
 * @param level:   the enum varible of eSensitivityLevel_t or 0~7
 * @n     eSensitivityLevel0 or 0: Sensitivity level 0, the most Sensitivity level.
 * @n     eSensitivityLevel1 or 1: Sensitivity level 1
 * @n     eSensitivityLevel2 or 2: Sensitivity level 2
 * @n     eSensitivityLevel3 or 3: Sensitivity level 3
 * @n     eSensitivityLevel4 or 4: Sensitivity level 4
 * @n     eSensitivityLevel5 or 5: Sensitivity level 5
 * @n     eSensitivityLevel6 or 6: Sensitivity level 6
 * @n     eSensitivityLevel7 or 7: Sensitivity level 7,the least Sensitivity Level.
 *
 * @return status: return config state.
 * @n      true:  Set sensitivity sucess.
 * @n      false:  Set sensitivity fail.
 */
  while(liquid.setSensitivityLevel(liquid.eSensitivityLevel2) != true){
      Serial.print(".");
      delay(2);
  }
  Serial.println("sucess.");
  
  Serial.print("Self check to update sensitivity...");
/**
 * @brief Self check which can update to get the current sensitivity and calibration mode.
 * @n In UART detected mode: You must use TX, RX and EN pin of sensor.
 * @n In level one-to-one detection mode: You must use OUT, TEST, EN, and TX pin of sensor
 * @return selfCheck update state:
 * @n      true: update sucess, and you can call getSensitivity to get current sensitivity ,and to call getCalibrationMode to get current calibration mode.
 * @n      false: update fail.
 */
  while(liquid.selfCheck() != true){
      Serial.print(".");
      delay(2);
  }
  Serial.println("done");
  Serial.print("Current sensitivity level(0~7): ");
/**
 * @brief Get sensitivity level of the channel of sensor.
 * @n @n note:Before using this function,you need to selfCheck function to update sensitivity ache.
 * @return sensitivity level: 
 * @n     eSensitivityLevel0 or 0: Sensitivity level 1, the most Sensitivity level.
 * @n     eSensitivityLevel1 or 1: Sensitivity level 2
 * @n     eSensitivityLevel2 or 2: Sensitivity level 3
 * @n     eSensitivityLevel3 or 3: Sensitivity level 4
 * @n     eSensitivityLevel4 or 4: Sensitivity level 5
 * @n     eSensitivityLevel5 or 5: Sensitivity level 6
 * @n     eSensitivityLevel6 or 6: Sensitivity level 6
 * @n     eSensitivityLevel7 or 7: Sensitivity level 7,the least Sensitivity Level.
 * @n     0xFF                   : Error sensitivity level value
 */
  Serial.println(liquid.getSensitivity());
}

void loop() {
}

UART模式传感器引脚与主控引脚对应图

sensor pin Leonardo/Mega2560/M0 UNO ESP8266 ESP32
TEST No connection No connection No connection No connection
OUT No connection No connection No connection No connection
EN 2 2 2/D5 2/D9
VCC VCC VCC VCC VCC
GND GND GND GND GND
RX Serial1 TX1 5 5/D6(TX) D2
TX Serial1 RX1 4 4/D7(RX) D3

结果

5.6 样例代码 4 - detect

代码介绍:检水测试。

样例代码的目的:通过调取接口函数来进行液位检测。

注意:UART模式和IO模式都可进行校准,区别在于接线不同。

IO模式传感器引脚与主控引脚对应图

注:IO模式下当检测有水时板载LED会点亮。

sensor pin Leonardo/Mega2560/M0 UNO ESP8266 ESP32 microbit
TEST No connection No connection No connection No connection No connection
OUT 10 10 10/D3 5/D8 8/P8
EN No connection No connection No connection No connection No connection
VCC VCC VCC VCC VCC VCC
GND GND GND GND GND GND
RX No connection No connection No connection No connection No connection
TX No connection No connection No connection No connection No connection

UART模式传感器引脚与主控引脚对应图

sensor pin Leonardo/Mega2560/M0 UNO ESP8266 ESP32 microbit
TEST No connection No connection No connection No connection x
OUT No connection No connection No connection No connection x
EN No connection No connection No connection No connection x
VCC VCC VCC VCC VCC x
GND GND GND GND GND x
RX Serial1 TX1 5 5/D6(TX) D2 x
TX Serial1 RX1 4 4/D7(RX) D3 x
#include "DFRobot_SCW8916B.h"
#if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
#include <SoftwareSerial.h>
#endif

//默认打开,此时使用UART通信, 屏蔽后使用IO口通信
#define UART_COMMUNICATION

#ifdef UART_COMMUNICATION
/**
 * @brief DFRobot_SCW8916B_UART abstract class constructor. Construct serial port detection object.(eUARTDetecteMode)
 * @param s:  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object
 * @param en: The IO pin of MCU which is connected to the EN pin of Non-contact liquid level sensor.when you call selfCheck,
 * @n setSensitivityLevel, LowerWaterLevelCalibration,or UpperWaterLevelCalibration function, you must use EN pin.
 */
  #if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
  SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
  DFRobot_SCW8916B_UART liquid(/*s =*/&mySerial);
  #else
  DFRobot_SCW8916B_UART liquid(/*s =*/&Serial1);
  #endif
#else
  #if defined(ARDUINO_BBC_MICROBIT)
    #define OUT     8          /**<The IO pin of MCU which is connected to the OUT pin of Non-contact liquid level sensor>*/
  #elif defined(ESP32)
    #define OUT     5          /**<The IO pin of MCU which is connected to the OUT pin of Non-contact liquid level sensor>*/
  #else
    #define OUT     10          /**<The IO pin of MCU which is connected to the OUT pin of Non-contact liquid level sensor>*/
  #endif
/**
 * @brief DFRobot_SCW8916B_IO abstract class constructor.Construction level one-to-one detection object.(eLevelDetecteMode)
 * @param out  :  The IO pin of MCU which is connected to OUT pin of Non-contact liquid level sensor.
 * @param en   :  The IO pin of MCU which is connected to EN pin of Non-contact liquid level sensor. 
 * @param test :  The IO pin of MCU which is connected to TEST pin of Non-contact liquid level sensor. 
 * @param s    :  The class pointer object of Abstract class, here you can fill in the pointer to the serial port object.
 * @n note: If you call selfCheck, you must use en, test and s, and if you call lowerWaterLevelCalibration or UpperWaterLevelCalibration,
 * @n you must use en and test pin.
 */
 DFRobot_SCW8916B_IO liquid(/*out =*/OUT);
#endif

void setup() {
  Serial.begin(115200);
  while(!Serial){
  }

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  
#ifdef UART_COMMUNICATION
  #if defined(ARDUINO_AVR_UNO)||defined(ESP8266)
    mySerial.begin(9600);
  #elif defined(ESP32)
    Serial1.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
  #else
    Serial1.begin(9600);
  #endif
#endif

  Serial.print("Initialization sensor...");
  int error = 0;
/**
 * @brief liquide level sensor initialization.
 * @return initialization state:
 * @n      0:  sucess
 * @n      0xAA(170):代表传感器从来没有校准过,需要先对传感器进行校准(UART)
 * @n      -1:  fail
 */
  while((error = liquid.begin()) != 0){
      Serial.print("failed. \nError code: ");
      Serial.println(error);
      if(error == ERR_CALIBRATION_CODE){
          Serial.println("You need to use calibration.ino to calibration sensor.");
      }else{
          Serial.println("Please check whether the hardware connection or configuration parameter is wrong.");
      }
      delay(1000);
      Serial.print("Initialization sensor...");
  }
  Serial.println("done.");
}

void loop() {
  bool flag = liquid.detectWater();                      /**<Detect whether there is water at water level------>true: have water, false: no water.*/
  if(flag){
      digitalWrite(LED_BUILTIN, HIGH);                   /**<If have water turn the LED on (HIGH is the voltage level)*/
      Serial.print("Have water: ");
  }else{
       digitalWrite(LED_BUILTIN, LOW);                   /**<If no water turn the LED off by making the voltage LOW*/
       Serial.print("No water:   ");
  }
  Serial.println(flag);                                  /**<Print water state at the water level, 0: No water, 1: Have water. */
}

结果

疑难解答

更多问题及有趣的应用,请访问论坛

更多

DFshopping_car1.png DFRobot商城购买链接
category: Product Manual category: DFR Series category: Sensors category:source category:Diagram