I am currently working on a project that requires me to read analog voltages for 16 different sensors and convert them to angle readings. To accomplish this I decided to utilize the MCP3008 ADC chip along with a rpi and python as that was something that I had done before for other projects that required a similar process. unlike my previous project however, I have to use 2 MCP3008 chips as they can only read 8 channels each. I know that this should be possible based on the way that spi communication works and the way that the MCP3008 chips work but I am running in to some problems getting both chips to work simultaneously. To preface, I am using adafruits mcp3xxx library to work with the chips. The first thing that I tried was putting both chips on the same spi bus using different chip select. the code that I used for that looked something like this
import busio
import digitalio
import board
import time
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
# this function is used to calculate angle of displacement
def Angle(Vmin,Vmax,R,Vt):
return ((Vt-Vmin)/(Vmax-Vmin))*R
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
#create the chip select(s)
cs0 = digitalio.DigitalInOut(board.CE0)
cs1 = digitalio.DigitalInOut(board.CE1)
#create the mcp object(s)
mcp0 = MCP.MCP3008(spi,cs0)
mcp1 = MCP.MCP3008(spi,cs1)
#create the analog channels
chan0_0 = AnalogIn(mcp0, MCP.P0)
chan1_0 = AnalogIn(mcp1, MCP.P0)
R_elbow = 180;
from CalibratedVoltages import*
while True:
# A_elbow = Angle(VminElbow,VmaxElbow,R_elbow,chan0_0.voltage)
# time.sleep(1)
# A_knee = Angle(VminKnee, VmaxKnee, R_knee,chan1_0.voltage)
# print('elbow angle:', A_elbow,'degrees')
print('0',chan0_0.voltage)
print('1',chan1_0.voltage)
time.sleep(2)
print('update')
With this setup however, any readings taken on the mcp1 object were dependent on the readings taken on the mcp0 object for some reason. For example, if you were to take a reading on channel 0 of mcp1 instead of ranging from 0 to 3.3 volts as it should have it ranged from 0 to whatever was being read on channel 0 of the mcp0 object. This was the same for all of the channels on the mcp1 object.
Unable to figure out why this was happening, I decided to try to simply put each MCP3008 chip on a separate spi bus to prevent interference due to a shared bus which is what I assumed was the causing the error. I did a dtoverlay to enable the spi1 bus and then changed my code to something more like this.
import busio
import digitalio
import board
import time
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
# this function is used to calculate angle of displacement
def Angle(Vmin,Vmax,R,Vt):
return ((Vt-Vmin)/(Vmax-Vmin))*R
# create the spi bus
spi0 = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
spi1 = busio.SPI(clock=21, MISO=19, MOSI=20)
#create the chip select(s)
cs0 = digitalio.DigitalInOut(board.D5)
cs1 = digitalio.DigitalInOut(board.D9)
#create the mcp object(s)
mcp0 = MCP.MCP3008(spi0,cs0)
mcp1 = MCP.MCP3008(spi1,cs1)
#create the analog channels
chan1_0 = AnalogIn(mcp1, MCP.P0)
chan0_0 = AnalogIn(mcp0, MCP.P0)
#chan0_1 = AnalogIn(mcp0, MCP.P1)
#chan0_2 = AnalogIn(mcp0, MCP.P2)
#chan0_3 = AnalogIn(mcp0, MCP.P3)
#chan0_4 = AnalogIn(mcp0, MCP.P4)
#chan0_5 = AnalogIn(mcp0, MCP.P5)
#chan0_6 = AnalogIn(mcp0, MCP.P6)
R_elbow = 180;
from CalibratedVoltages import*
while True:
# A_elbow = Angle(VminElbow,VmaxElbow,R_elbow,chan0_0.voltage)
# time.sleep(1)
# A_knee = Angle(VminKnee, VmaxKnee, R_knee,chan1_0.voltage)
# print('elbow angle:', A_elbow,'degrees')
print('0',chan0_0.voltage)
print('1',chan1_0.voltage)
# print('2',chan0_2.voltage)
# print('3',chan0_3.voltage)
# print('4',chan0_4.voltage)
# print('5',chan0_5.voltage)
# print('6',chan0_6.voltage)
time.sleep(2)
print('update')
With this set up, only the MCP chip on the spi1 bus works and the other chip returns 0.0 on all channels regardless of what voltage is attached to it. Even if you change the code back to only utilizing the spi0 bus, the chip on the spi0 bus continues to return all 0.0 readings until and unless I reboot the rpi and run the code for just spi0 bus readings without ever running any of the lines that set up anything for the spi1 bus. At this point I am fairly lost and can only assume that it has something to do with one of the library's that I am using and I chose the MCP3008 chips because I knew there was an easy to use library available from adafruit and I wanted to avoid having to put a lot of effort in to this part of the project specifically. If anyone has had this same kind of issue and knows how to solve it or even just take a guess it would be very appreciated. A solution to either of the problems that were described would allow me to finish the project.