简体   繁体   English

optomax 数字液体传感器 - python for raspberry pi

[英]optomax digital liquid sensor - python for raspberry pi

I've found arduino code for this adafruit digital liquid sensor here我在这里找到了这个 adafruit 数字液体传感器的 arduino 代码


    // Liquid level detection using an SST sensor
    // When a liquid touches the tip of the sensor,
    // an LED at pin 13 turns on.
    
    // Pins
    const int LIQUID_SENSOR_PIN = 7;
    const int LED_PIN = 13;
    
    void setup() { 
      pinMode(LIQUID_SENSOR_PIN, INPUT);
      pinMode(LED_PIN, OUTPUT);
      digitalWrite(LED_PIN, LOW);
    }
    
    void loop() {
    
      // Read sensor. If liquid touches the tip, the sensor will 
      // read 0V. Turn on LED if liquid is present.
      int isDry = digitalRead(LIQUID_SENSOR_PIN);
      if ( isDry ) {
        digitalWrite(LED_PIN, LOW);
      } else {
        digitalWrite(LED_PIN, HIGH);
      }
    
      delay(200);
    }

but nothing written in python for the raspberry pi - (i'm not bothered about triggering an led just the syntax) - has anyone done it ?但是没有为树莓派用 python 写的任何东西 - (我不担心触发 LED 只是语法) - 有没有人做过?

You can use the Raspberry Pi's GPIO pins to read digital sensors.您可以使用 Raspberry Pi 的 GPIO 引脚读取数字传感器。 To do this in Python, I recommend that you check out the WiringPi-Python project .要在 Python 中执行此操作,我建议您查看WiringPi-Python 项目

First you need to install the library.首先,您需要安装库。

pip install wiringpi

Then you could use the following (untested) code to read the sensor and display the results on the LED:然后您可以使用以下(未经测试的)代码读取传感器并在 LED 上显示结果:

import wiringpi
import time

LOW = 0
HIGH = 1
LIQUID_SENSOR_PIN = 23
LED_PIN = 24

wiringpi.wiringPiSetupGpio()

wiringpi.pinMode(LIQUID_SENSOR_PIN, wiringpi.GPIO.INPUT)
wiringpi.pinMode(LED_PIN, wiringpi.GPIO.OUTPUT)
wiringpi.digitalWrite(LED_PIN, LOW)

while True:
    pin_state = wiringpi.digitalRead(LIQUID_SENSOR_PIN)
    is_dry = (pin_state == 0)

    if is_dry:
        wiringpi.digitalWrite(LED_PIN, LOW)
    else:
        wiringpi.digitalWrite(LED_PIN, HIGH)

    time.sleep(0.2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM