57 lines
1.2 KiB
Python
57 lines
1.2 KiB
Python
import time
|
|
|
|
import adafruit_mcp3xxx.mcp3008 as MCP
|
|
import board
|
|
import busio
|
|
import digitalio
|
|
import RPi.GPIO as gp
|
|
|
|
from adafruit_mcp3xxx.analog_in import AnalogIn
|
|
|
|
|
|
def test_relay_on_off(rly=None):
|
|
# use GPIO numbering
|
|
gp.setmode(gp.BCM)
|
|
|
|
gp.setup(rly, gp.OUT)
|
|
gp.output(rly, gp.HIGH)
|
|
|
|
try:
|
|
while True:
|
|
gp.output(rly, gp.LOW)
|
|
print("Relay 1 ON")
|
|
time.sleep(1)
|
|
gp.output(rly, gp.HIGH)
|
|
print("Relay 1 OFF")
|
|
time.sleep(1)
|
|
except KeyboardInterrupt:
|
|
gp.cleanup()
|
|
|
|
|
|
def test_sensor_in_out_water():
|
|
# create the spi bus
|
|
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
|
|
|
|
# create the chip select
|
|
cs = digitalio.DigitalInOut(board.D5)
|
|
|
|
# create the mcp object
|
|
mcp = MCP.MCP3008(spi, cs)
|
|
|
|
# create an analog input channel on pin 5
|
|
analog_ic = AnalogIn(mcp, MCP.P5)
|
|
|
|
try:
|
|
while True:
|
|
print(f"Raw ADC value: {analog_ic.value:.2f}")
|
|
print(f"ADC Voltage value: {analog_ic.voltage:.3f}")
|
|
time.sleep(.5)
|
|
except KeyboardInterrupt:
|
|
return
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Relay 1 on GPIO21
|
|
#test_relay_on_off(21)
|
|
test_sensor_in_out_water()
|