The source code I have below works for a two analog voltages(water level detection and temperature sensor). The code is set right now for just these two to run and it does work. If there is a problem the finally at the end will throw an error and I think this has something to do with the SPI bus. When I try to incorporate a dht11 temperature and humidity sensor that has a digital input, it will run for a couple seconds and then through the error. I tried it again and now it says that it is unable to set line (mcp = Adafruit....) to input. I think this problem is because both sensor are fighting over SPI bus for adafruit. I am not an expert programmer, I just want something that will work at the same time, so three total sensors(two channel voltages and 1 sensor output). Many lines of code I have commented out because I have combined different code. Some lines may be valuable for the temp and humidity sensor.
from time import sleep
import board
import adafruit_dht
import psutil
#
#import RPi.GPIO as GPIO
#import spidev
import Adafruit_MCP3008
#spi = spidev.SpiDev()
#spi.open(0, 0)
#spi.max_speed_hz = 250000
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(14, GPIO.OUT)
#GPIO.setup(15, GPIO.OUT)
mcp = Adafruit_MCP3008.MCP3008(clk=11, cs=8, miso=9, mosi=10)
#sensor = adafruit_dht.DHT11(board.D23)
def poll_sensor(channel):
"""Poll MCP3002 ADC
Args:
channel (int): ADC channel 0 or 1
Returns:
int: 10 bit value relating voltage 0 to 1023
"""
assert 0 <= channel <= 1, 'ADC channel must be 0 or 1.'
# First bit of cbyte is single=1 or diff=0.
# Second bit is channel 0 or 1
if channel:
cbyte = 0b11000000
else:
cbyte = 0b10000000
# Send (Start bit=1, cbyte=sgl/diff & odd/sign & MSBF = 0)
r = spi.xfer2([1, cbyte, 0])
# 10 bit value from returned bytes (bits 13-22):
# XXXXXXXX, XXXX####, ######XX
return ((r[1] & 31) << 6) + (r[2] >> 2)
try:
while True:
#temp = sensor.temperature
# humidity = sensor.humidity
# print("Temperature: {}*C Humidity: {}% ".format(temp, humidity))
#channel = 0
# channeldata = poll_sensor(channel)
# voltage = round(((channeldata * 3300) / 1024), 0)
#print('Voltage (mV): {}'.format(voltage))
#print('Data : {}\n'.format(channeldata))
# if voltage < 50:
# Green
# GPIO.output(14, GPIO.HIGH)
# GPIO.output(15, GPIO.LOW)
#elif voltage < 1800:
# Yellow
# GPIO.output(14, GPIO.HIGH)
# GPIO.output(15, GPIO.HIGH)
# else:
# Red
# GPIO.output(14, GPIO.LOW)
# GPIO.output(15, GPIO.HIGH)
print('Reading MCP3004 values, press Ctrl-C to quit...')
# Print nice channel column headers.
# print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
# print('-' * 57)
# Main program loop.
# Read all the ADC channel values in a list.
values = [0]*4
for i in range(4):
# The read_adc function will get the value of the specified channel (0-7).
values[i] = mcp.read_adc(i)
# Print the ADC values.
print('|Channel 0 ={0:>4} | Channel 1 ={1:>4} | '.format(*values))
# Pause for half a second.
sleep(0.5)
finally: # run on exit
spi.close() # clean up
#GPIO.cleanup()
#print ("\n All cleaned up.")