B4R Code Snippet Reading a water/soil sensor (3 pin red)

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

When the sensor is completely dry (no resistance) the logs will show 0(zero). With this particular sensor the more water there is on it, the more there is resistance, thus the reading goes higher.

********************* PROGRAM STARTING ****************
AppStart
0
it's dry
0
it's dry
515
It's Wet
542
It's Wet
657
It's Wet
653
It's Wet
693
It's Wet
672
It's Wet
659
It's Wet
653
It's Wet
649
It's Wet
649
It's Wet
649
It's Wet
635
It's Wet
583
It's Wet
578
It's Wet
580
It's Wet
639
It's Wet
656
It's Wet
648
It's Wet
648
It's Wet
599
It's Wet
557
It's Wet
550
It's Wet
535
It's Wet
385
It's Wet
375
It's Wet
369
It's Wet
362
It's Wet
355
It's Wet
355
It's Wet
353
It's Wet
389
It's Wet
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry

B4X:
'WIRE LEGEND for red 3 pin water sensor
'GND = GND
'VCC = 5V
'S (Sensor) = 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 V_Pin_A0 As Pin
    Private TmrReadings As Timer
End Sub

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

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

Arduino wiring
Water Sensor_bb.png


Actual sensor/module
Water-Level-Sensor.jpg
 
Last edited:

Beja

Expert
Licensed User
Longtime User
With this particular sensor the more water there is on it, the more there is resistance, thus the reading goes higher.

You mean dry is high resistance (impedance)
More water means less resistance, hence high value reading.
 

embedded

Active Member
Licensed User
Longtime User
L
You mean dry is high resistance (impedance)
More water means less resistance, hence high value reading.
More water level means current path is shorter than previous one.... shorter path having less resistance....less resistance...less voltage drop....high adc value..
 

Beja

Expert
Licensed User
Longtime User
L

More water level means current path is shorter than previous one.... shorter path having less resistance....less resistance...less voltage drop....high adc value..

Ok.. I think there was misunderstanding in my part.
The moisture sensor confused me and I don't know why it was put in the example.. Soil moisture sensor is completely buried in the soil.
Thanks
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
Ok.. I think there was misunderstanding in my part.
The moisture sensor confused me and I don't know why it was put in the example.. Soil moisture sensor is completely buried in the soil.
Thanks

Because it does not matter if it's placed in a cup of water or placed in soil, resistance changes the deeper the sensor is/more moist the sensor becomes.

Pint time, catcha on da flip side ;)
 

Beja

Expert
Licensed User
Longtime User
Because it does not matter if it's placed in a cup of water or placed in soil, resistance changes the deeper the sensor is/more moist the sensor becomes.

Pint time, catcha on da flip side ;)

You are right. If anything matters, soil moisture gives you more readings (for the same point) depending on the moisture level, while water only gives one value for each point on the scale.
 

Beja

Expert
Licensed User
Longtime User
SubName: Reading a water sensor (3 pin red)
Description: You can use this simple code to read a 3 pin rain/droplet/water sensor detector.

When the sensor is completely dry (no resistance) the logs will show 0(zero). With this particular sensor the more water there is on it, the more there is resistance, thus the reading goes higher.

********************* PROGRAM STARTING ****************
AppStart
0
it's dry
0
it's dry
515
It's Wet
542
It's Wet
657
It's Wet
653
It's Wet
693
It's Wet
672
It's Wet
659
It's Wet
653
It's Wet
649
It's Wet
649
It's Wet
649
It's Wet
635
It's Wet
583
It's Wet
578
It's Wet
580
It's Wet
639
It's Wet
656
It's Wet
648
It's Wet
648
It's Wet
599
It's Wet
557
It's Wet
550
It's Wet
535
It's Wet
385
It's Wet
375
It's Wet
369
It's Wet
362
It's Wet
355
It's Wet
355
It's Wet
353
It's Wet
389
It's Wet
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry
0
it's dry

B4X:
'WIRE LEGEND for red 3 pin water sensor
'GND = GND
'VCC = 5V
'S (Sensor) = 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 V_Pin_A0 As Pin
    Private TmrReadings As Timer
End Sub

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

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

Arduino wiring
View attachment 71238

Actual sensor/module
View attachment 71239

Hi Pete,
Will this example also work on UNO?
 
Top