2442 views|4 replies

1w

Posts

25

Resources
The OP
 

【RPi PICO】Soil Moisture Indicator [Copy link]

 

Source: https://andywarburton.co.uk/raspberry-pi-pico-soil-moisture-sensor/

Required Materials

  • Raspberry Pi Pico
  • A Sparkfun Soil Moisture Sensor
  • WS2812B (Neopixel) Ring or Strip

Wiring Diagram

  • Pin 2 - Neopixel ring data signal
  • Pin 7 - Power supply for humidity sensor
  • Pin 40 - Power for the Neopixel ring (directly from the USB port)
  • Pin 38 - Ground for the Neopixel ring
  • Pin 27 - Analog signal from humidity sensor
  • Pin 23 - Ground of humidity sensor

Latest reply

Thanks for sharing  Details Published on 2021-2-17 21:13

赞赏

1

查看全部赞赏

 
 

1w

Posts

25

Resources
2
 

Code

Before programming, you need to install CircuitPython to the PICO board and download the necessary binding libraries (please refer to the link of PICO documentation).

import time
import board
import neopixel
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction, Pull

# how frequently to take readings
DELAY = 600

# Update this to match the number of NeoPixel LEDs connected to your board.
num_pixels = 16

# setup the moisture sensor power pin and turn it off by default
sensor_power = DigitalInOut(board.GP7)
sensor_power.direction = Direction.OUTPUT
sensor_power.value = False

# set the analog read pin for the moisture sensor
sensor_signal = AnalogIn(board.GP27)

# set up the noepixels
pixels = neopixel.NeoPixel(board.GP1, num_pixels, auto_write=False)
pixels.brightness = 0.2

# some variables for internal use, you shouldn't have to worry about them
calibrate_count = 0
auto_calibrate = True
SENSOR_MAX = 0
SENSOR_MIN = 9999

# set the neopixels to blue
for i in range(num_pixels):
    pixels[i] = (0,0,255)
pixels.show()

print("=============")
print("calibrating sensor")
print("=============\n\n")

while True:

    if auto_calibrate == True:

        # enable the sensor power
        sensor_power.value = True

        # take a reading from the sensor and make it a little easier to read
        value = round(sensor_signal.value / 100)

        # disable the sensor power
        sensor_power.value = False

        print("reading:", value)

        if value > SENSOR_MAX:
            SENSOR_MAX = value

        if value < SENSOR_MIN:
            SENSOR_MIN = value

        calibrate_count += 1

        if(calibrate_count > 100):
            print("\n-------------------")
            print("MIN:", SENSOR_MIN)
            print("MAX:", SENSOR_MAX)
            print("-------------------\n")
            time.sleep(5)

            # wipe pixels
            for i in range(num_pixels):
                pixels[i] = (0,0,0)
            pixels.show()
            
            auto_calibrate = False

        time.sleep(0.2)

    else:

        # take a reading from the sensor and make it a little easier to read
        value = round(sensor_signal.value / 100)
        print("reading:", value)

        # disable the sensor power
        sensor_power.value = True

        # crazy math to turn value into percentage
        percent = round(((value - SENSOR_MIN) / (SENSOR_MAX - SENSOR_MIN)) * 100)
        print("Percent:", percent)

        # more crazy math to convert percentage to number of pixels
        show_leds = round(100 / (100 / num_pixels) * percent / 100)
        print("leds to show:", show_leds)

        # print values for the plotter
        print((value, percent, show_leds))

        # change colour depending on quantity
        if(show_leds < 8):
            color = (255,0,0)
        else:
            color = (0,255,0)

        # make sure we don't ever try to show more leds than we have
        # because that will crash the script
        if(show_leds > num_pixels):
            show_leds = num_pixels
        
        # if water levels are critically low, show them all
        if(show_leds <= 3):
            show_leds = num_pixels

        # wipe pixels
        for i in range(num_pixels):
            pixels[i] = (0,0,0)

        # turn on neopixels
        for i in range(show_leds):
            pixels[i] = color

        pixels.show()

        # wait for required delay
        time.sleep(DELAY)

        print("\n-------------------\n")

 
 
 

1942

Posts

2

Resources
3
 

The review is well written. I like review posts like yours. Thank you for sharing! Looking forward to the follow-up

 
 
 

128

Posts

0

Resources
4
 

This type of humidity sensor will break down soon. Replace it with a capacitive type, or make your own analog one using two stainless steel sheets.

 
 
 

1412

Posts

3

Resources
5
 
Thanks for sharing
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list