I want to read 3 analogue sensors simultaneously. I have successfully connected 2 sensors using a MCP 3002 A/D converter which is connected to CS0 of SPI 0 of raspberry pi and I have activated 3 more CS pins for SPI 1.
I have connected the 3rd sensor on SPI 1 bus on CS2 with MOSI, MISO and SCLK of respective SPI buses, but when I run the program 2 sensors give readings while the 3rd sensor just shows 0.0.
I think there is a problem with my code but i cant seem to figure it out.
I am attaching the code as well. Help will be really appreciated.
import time import spidev import RPi.GPIO as GPIO spi_ch = 0 spi = spidev.SpiDev(0, spi_ch) spi.max_speed_hz = 1200000 def read_adc(adc_ch, vref = 3.3): msg = 0b11 msg = ((msg << 1) + adc_ch) << 5 msg = [msg, 0b00000000] reply = spi.xfer2(msg) adc = 0 for n in reply: adc = (adc << 8) + n adc = adc >> 1 voltage = (vref * adc) / 1024 return voltage spi_ch1 = 2 spi1 = spidev.SpiDev(1, spi_ch) spi1.max_speed_hz = 1200000 def read1_adc(adc1, vref = 3.3): msg = 0b11 msg = ((msg << 1) + adc1) << 5 msg = [msg, 0b00000000] reply = spi1.xfer2(msg) adc = 0 for n in reply: adc = (adc << 8) + n adc = adc >> 1 voltage1 = (vref * adc) / 1024 return voltage1 try: while True: adc_0 = read_adc(0) adc_1 = read_adc(1) adc1=read1_adc(0) print("Ch 0:", adc_0, "V Ch 1:", adc_1, "V ch:2" ,adc1 , "V") time.sleep(0.2) finally: GPIO.cleanup()