345|4

1万

帖子

13

资源

版主

【RPi PICO】土壤湿度指示器 [复制链接]

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

 

image.png

 

需要的材料

 

  • Raspberry Pi Pico
  • 一个Sparkfun土壤湿度传感器
  • WS2812B(Neopixel)环或带

image.png

 

连线图

  • 引脚2-Neopixel ring的数据信号
  • 引脚7-湿度传感器的电源
  • 引脚40-Neopixel环的电源(直接从USB端口)
  • 引脚38-Neopixel环的地
  • 引脚27-湿度传感器的模拟信号
  • 引脚23-湿度传感器的地

image.png

赞赏

1

查看全部赞赏


回复

1万

帖子

13

资源

版主

代码

编程前,需要先安装circuitpython到PICO板,并下载必要的绑定库(请参考PICO文档的链接)。

 

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")

 


回复

795

帖子

2

资源

版主

测评写的不错,就喜欢你这样的测评帖子,感谢老铁的分享!期待后续


回复

114

帖子

0

资源

一粒金砂(中级)

这种湿度传感器要不了多久就坏了,换个电容式的吧,或者自己用两个不锈钢片自制个模拟的。


回复

278

帖子

2

资源

版主

感谢分享

回复
您需要登录后才可以回帖 登录 | 注册

最新文章 更多>>
    关闭
    站长推荐上一条 1/6 下一条

    About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

    站点相关: 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

    北京市海淀区知春路23号集成电路设计园量子银座1305 电话:(010)82350740 邮编:100191

    电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2021 EEWORLD.com.cn, Inc. All rights reserved
    快速回复 返回顶部 返回列表