As the title states, I am attempting to use the Grow Moisture Sensors directly on my Raspberry Pi, without using the Grow Hat. I’ve connected Ground to Ground, Sig to Pin 4, and VCC to 5V.
I’ve tried two attempts, based on what I’ve seen on the Pimoroni Github, but I think I’m missing something, because the numbers don’t seem to change as the soil gets dryer (however there IS a clear difference between air and soil readings).
Attempt 1:
def count_pulse(channel):
global pulse_count
pulse_count += 1
def getSoilMoisturePimoroni(sensor_pin):
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor_pin, GPIO.IN)
GPIO.add_event_detect(sensor_pin, GPIO.RISING, callback=count_pulse)
pulse_count = 0
start_time = time.time()
time.sleep(10)
current_time = time.time()
elapsed_time = current_time - start_time
pulses_per_second = pulse_count / elapsed_time
print(f"Pulses per second: {pulses_per_second:.2f}")
Attempt 2:
GPIO.setmode(GPIO.BCM)
pulse_pin = 4
GPIO.setup(pulse_pin, GPIO.IN)
pulse_count = 0
last_state = GPIO.input(pulse_pin)
start_time = time.time()
try:
while True:
current_state = GPIO.input(pulse_pin)
if current_state != last_state:
pulse_count += 1
last_state = current_state
# Calculate the time elapsed
elapsed_time = time.time() - start_time
# If a sufficient amount of time has passed, calculate the frequency
if elapsed_time >= 10.0:
frequency = pulse_count / elapsed_time
print(f"Pulse Frequency: {frequency:.2f} Hz")
pulse_count = 0
start_time = time.time()
Am I getting the basic concept right? I’m trying to count the number of pulses per second, and the number of pulses should go up as the soil is more moist, and down as the soil gets dryer, correct?