如何解决如何在pythonmicropython中将电容性土壤湿度传感器v1.2的adc输出转换为实际湿度百分比?
下面是基本测试代码的示例,您可以从中读取传感器的模拟值。 但是,较高的值是最干燥的地方。严格地,水接近0的值。这与我对水分/湿度如何工作的基本知识相反-土壤/空气中的水分更多-水分/湿度更多。 将其转换为可用的土壤水分百分比的最有效方法是什么?
html
解决方法
我假设您使用“ Capacitive Soil Moisture Sesnort 1.2”
这是该传感器的工作方式:原始值越高,水分越低。这样:先测量水中的原始值,然后再测量空气中的原始值
现在湿了就有价值,干了就有价值。
然后只是数学。定义以下功能:
def remap(value,leftMin,leftMax,rightMin,rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
# return int(rightMin + (valueScaled * rightSpan))
return rightMin + (valueScaled * rightSpan)
在代码中的某些位置调用此函数:
moist_percent = remap(
moistureADC.read(),1000,2200,# these values comes from experiment above when wet and dry
100,1
)
您在变量中具有百分比值
moist_percent
最终应用将是这样的:
from machine import Pin,ADC
from time import sleep
soil_moisture = ADC(Pin(34))
adc.width(ADC.WIDTH_12BIT) # set 12 bit return values (returned range 0-4095)
def remap(value,rightMax):
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
valueScaled = float(value - leftMin) / float(leftSpan)
return rightMin + (valueScaled * rightSpan)
while True:
soil_moisture_value = remap(
soil_moisture.read(),# these values comes from experiment above when wet and dry
100,1 # or you can map to whatever values you wish
)
print(soil_moisture_value)
sleep(2)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。