I am trying to get this script working but failed despite days of grappling blind, without enough knowledge.
The input of CH1) of the mcp3008 varies between 3,3 and 1,6V by 10k +10k potmeter divider.
Please be assured that there is no wiring fault, grant me that much after 50 years in ICT.
Code: Select all
#!/usr/bin/python
# Import Libraries
import spidev
import time
# SPI intialisation
spi = spidev.SpiDev()
spi.open(0,0)
# Function to get the readings from the mcp3008
def read_spi(channel):
spi.max_speed_hz = 1000000
spidata = spi.xfer2([1,(8+channel)<<4,0])
print("Raw ADC: {}".format(spidata))
data = ((spidata[1] & 3) << 8) + spidata[2]
return data
try:
while True:
channeldata = read_spi(7)
voltage = round(((channeldata * 3300) / 1024),0)
print("Data (dec) {}".format(channeldata))
print("Data (bin) {}".format(('{0:010b}'.format(channeldata))))
print("Voltage (mV): {}".format(voltage))
print("--------------------")
time.sleep(1)
except KeyboardInterrupt:
spi.close()
The output seems to rise and fall cyclically. The valeus rise to 265 and then falls to 0 and rises again and so on.
Varying the potmeter has no visible effect on the reading.
Any suggestions?
Thanks