Digital Input/Ouput
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
def setup_digital(pin):
GPIO.setup(pin, GPIO.OUT)
def digital_on(pin):
print("digital_on", pin)
GPIO.output(pin, 1)
def digital_off(pin):
print("digital_off", pin)
GPIO.output(pin, 0)
def get_digital_staus(pin):
return GPIO.input(pin)
Analog Input
import spidev
import time
sample = 10
sampleValue = []
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 1000000
def adc_read(channel):
r = spi.xfer2([1, (0x08+channel)<<4, 0])
adc_out = ((r[1]&0x03)<<8) + r[2]
return adc_out
Custom Sensor
DHT22
pip3 install adafruit-circuitpython-dht
sudo apt-get install libgpiod2
import time
import board
import adafruit_dht
dhtDevice = adafruit_dht.DHT22(board.D22)
def read_temp():
return dhtDevice.temperature
def read_huminity():
return dhtDevice.humidity
PH Sensor
def phSensor_read(channel):
for i in range(sample):
adc = adc_read(channel)
sampleValue.append(adc)
time.sleep(0.10)
sampleValue.sort()
avg = 0
for i in range(2, 8):
avg += sampleValue[i]
phValue = float(avg * 5 / 1024 /6)
phValue = float(3.5 * phValue)
sampleValue.clear()
return phValue
Turbidity Sensor
def turbidity_read(channel):
sampleValue = []
for i in range(sample):
adc = adc_read(channel)
voltage = adc * (5.0 / 1024.0)
sampleValue.append(voltage)
sampleValue.sort()
avg = 0
for i in range(2, 8):
avg += sampleValue[i]
voltage = avg / 6
return voltage