I've got the classic Iduion Water Sensor (Link) and I want it to simply detect the presence of water but I get unreliable readings when using it like this:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
Power = 18
Sensor = 23
GPIO.setup(18,GPIO.OUT)
GPIO.setup(23,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.output(Power, 1)
time.sleep(0.2)
print("Sensor Power: ON")
i = 1
while i <= 100:
state = GPIO.input(Sensor)
if state == 1:
print("Stop")
print(str(state))
i = 100
i += 1
time.sleep(0.5)
GPIO.output(Power, 0)
print("Sensor OFF")
The Sensor works as it should by printing 1
and stopping the program when detecting water but after some time running the code it keeps printing 0
although there's water on the sensor.
Edit: Fixed the link. Iduino is just a name that shows when googling the standard Water Sensor for Arduino an Pi.
I'm aware that 3.3 Volts from the GPIO might not be sufficient but I don't get why it works reliably from time to time. I'm powering the Pi with 5V 1A. I am aware that this is an analog sensor. I tried to get rid of the internal pull down solution and now it works quite good:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
Power = 18
Sensor = 23
GPIO.setup(18,GPIO.OUT)
GPIO.setup(23,GPIO.IN) ---> Only difference compared to code above
GPIO.output(Power, 1)
time.sleep(0.2)
print("Sensor Power: ON")
i = 1
while i <= 100:
state = GPIO.input(Sensor)
if state == 1:
print("Stop")
print(str(state))
i = 100
i += 1
time.sleep(0.5)
GPIO.output(Power, 0)
print("Sensor OFF")