B4R Code Snippet Reading a FC-37 water sensor (2 pin black)

Discussion in 'B4R Code Snippets' started by Peter Simpson, Aug 20, 2018.

  1. Peter Simpson

    Peter Simpson Expert Licensed User

    SubName: Reading a water sensor (2 pin black)
    Description: You can use this simple code to read a 2 pin rain/droplet/water sensor detector.

    When the sensor is completely dry (no water resistance) the logs will show the value 1023. With this particular sensor the more water there is on it the lower the value will go. I personally found that the value 750 is a good water warning trigger (that is basically 1 drop of water).

    ********************* PROGRAM STARTING ****************
    AppStart
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1015
    It's dry
    1016
    It's dry
    1018
    It's dry
    1019
    It's dry
    1020
    It's dry
    1020
    It's dry
    778
    It's dry
    734
    It's wet
    720
    It's wet
    703
    It's wet
    706
    It's wet
    705
    It's wet
    703
    It's wet
    706
    It's wet
    707
    It's wet
    709
    It's wet
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry
    1023
    It's dry

    Code:
    'WIRE LEGEND for black 2 pin water sensor
    'GND = GND
    'VCC = 5V
    'A0 = A0

    '*************************
    '***        BOARD TYPE        ***
    '***    Mega or Mega 2560  ***
    '*************************

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.
        Public Serial1 As Serial
        
    Private Sensor_A0 As Pin
        
    Private TmrReadings As Timer
        
    Private sensortrigger As Int = 750
    End Sub

    Private Sub AppStart
        Serial1.Initialize(
    115200)
        
    Log("AppStart")
        Sensor_A0.Initialize(
    0, Sensor_A0.AnalogRead)
        TmrReadings.Initialize(
    "Readings_Tick"1000)
        TmrReadings.Enabled = 
    True
    End Sub

    Sub Readings_Tick
        
    Log(Sensor_A0.AnalogRead)
        
    If Sensor_A0.AnalogRead < sensortrigger Then Log("It's wet"Else Log("It's dry")
    End Sub
    Tags: Water, Sensor, Arduino, ESP

    Actual sensor/module
    Modulo-sensor-de-detecao-de-chuva.jpg
     
    Last edited: Aug 20, 2018
  2. Beja

    Beja Expert Licensed User

    Hello all
    Today I am using Peter's code in my project, but I need to use two water sensors instead of one as explained above, I tried to modify Pete's code
    but nor sure if it work.. (will test on Uno as soon as I get to there.)
    Here's the modified code. please advice if there's anything wrong with this code. Thanx. B

    Code:
    'WIRE LEGEND for black 2 pin water sensor
    'GND = GND
    'VCC = 5V
    'A0 = A0

    '*************************
    '***        BOARD TYPE        ***
    '***    Mega or Mega 2560  ***
    '*************************

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.
        Public Serial1 As Serial
        
    Private Sensor_A0 As Pin

    ‘=========================================

    Private Sensor_A1 As Pin

    ‘=========================================
        
    Private TmrReadings As Timer
        
    Private sensortrigger As Int = 750

    ‘=========================================

    Private sensortrigger2 As Pin = 90

    ‘=========================================
    End Sub

    Private Sub AppStart
        Serial1.Initialize(
    115200)
        
    Log("AppStart")
        Sensor_A0.Initialize(
    0, Sensor_A0.AnalogRead)
        TmrReadings.Initialize(
    "Readings_Tick"1000)
        TmrReadings.Enabled = 
    True
    End Sub

    Sub Readings_Tick
        
    Log(Sensor_A0.AnalogRead)
        
    If Sensor_A0.AnalogRead < sensortrigger Then Log("It's wet"Else Log("It's dry")

    ‘=========================================

        
    Log(Sensor_A1.AnalogRead)
        
    If Sensor_A1.AnalogRead > sensortrigger2Then Log("It's dry"Else Log("It's wet")

     ‘=========================================
    End Sub
     
    Johan Schoeman likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice