#!/usr/bin/python
     
import spidev
DEBUG = 0
     
spi = spidev.SpiDev()
spi.open(0,0)
    
# read SPI data from MCP3002 chip
def get_adc(channel):
	# Only 2 channels 0 and 1 else return -1
	if ((channel > 1) or (channel < 0)):
		return -1
	  
	# Send start bit, sgl/diff, odd/sign, MSBF
	# channel = 0 sends 0000 0001 1000 0000 0000 0000
	# channel = 1 sends 0000 0001 1100 0000 0000 0000
	# sgl/diff = 1; odd/sign = channel; MSBF = 0
	r = spi.xfer2([1,(2+channel)<<6,0])
	
	# spi.xfer2 returns same number of 8 bit bytes
	# as sent. In this case, 3 - 8 bit bytes are returned
	# We must then parse out the correct 10 bit byte from
	# the 24 bits returned. The following line discards
	# all bits but the 10 data bits from the center of
	# the last 2 bytes: XXXX XXXX - XXXX DDDD - DDDD DDXX
	ret = ((r[1]&31) << 6) + (r[2] >> 2)
	return ret     
     
print get_adc(0)
print get_adc(1)

@@ -14,119 +14,523 @@
 thon
-%0A     %0Aimport spidev%0ADEBUG = 0%0A     %0Aspi = spidev.SpiDev()%0Aspi.open(0,0)%0A    %0A# read SPI data from MCP3002 chip
+3%0A%0A# Tested on Raspberry Pi OS. Requirements: enable SPI, for example%0A# from raspi-config. The example is based on a SparkFun tutorial:%0A# https://learn.sparkfun.com/tutorials/python-programming-tutorial-getting-started-with-the-raspberry-pi/experiment-3-spi-and-analog-input%0A%0Aimport signal%0Aimport sys%0Aimport time%0Aimport spidev%0Aimport RPi.GPIO as GPIO%0A%0Aspi_ch = 0%0A%0A# Enable SPI%0Aspi = spidev.SpiDev(0, spi_ch)%0Aspi.max_speed_hz = 1200000%0A%0Adef close(signal, frame):%0A    sys.exit(0)%0A%0Asignal.signal(signal.SIGINT, close)%0A
 %0Adef
@@ -552,25 +552,36 @@
 l):%0A
-%09# Only 2
+%0A    # Make sure ADC
  channel
 s 0 
@@ -580,47 +580,34 @@
 nnel
+ i
 s 0 
-and 1 else return -1%0A%09
+or 1%0A    
 if 
-((
 channel 
 %3E 1)
@@ -606,688 +606,1110 @@
 nel 
-%3E 1) or (channel %3C 0)):%0A%09%09return -1%0A%09  %0A%09#
+!= 0:%0A        channel = 1%0A%0A    # Construct SPI message%0A    #  First bit (Start): Logic high (1)%0A    # 
  Se
+co
 nd 
-start bit, sgl/diff, odd/sign, MSBF%0A%09# channel = 0 sends 0000 0001 1000 0000 0000 0000%0A%09# channel = 1 sends 0000 0001 1100 0000 0000 0000%0A%09# sgl/diff = 1; odd/sign = channel; MSBF = 0%0A%09r = spi.xfer2(%5B1,(2+channel)%3C%3C6,0%5D)%0A%09%0A%09# spi.xfer2 returns same number of 8 bit bytes%0A%09# as sent. In this case, 3 - 8 bit bytes are returned%0A%09# We must then parse out the correct 10 bit byte from%0A%09# the 24 bits returned. The following line discards%0A%09# all bits but the 10 data bits from the center of%0A%09# the last 2 bytes: XXXX XXXX - XXXX DDDD - DDDD DDXX%0A%09ret = ((r%5B1%5D&31) %3C%3C 6) + (r%5B2%5D %3E%3E 2)%0A%09return ret
+bit (SGL/DIFF): 1 to select single mode%0A    #  Third bit (ODD/SIGN): Select channel (0 or 1)%0A    #  Fourth bit (MSFB): 0 for LSB first%0A    #  Next 12 bits: 0 (don't care)%0A    msg = 0b11%0A    msg = ((msg %3C%3C 1) + channel) %3C%3C 5%0A    msg = %5Bmsg, 0b00000000%5D%0A    reply = spi.xfer2(msg)%0A%0A    # Construct single integer out of the reply (2 bytes)%0A    adc = 0%0A    for n in reply:%0A        adc = (adc %3C%3C 8) + n%0A%0A    # Last bit (0) is not part of ADC value, shift to remove it%0A    adc = adc %3E%3E 1%0A%0A    # Calculate voltage form ADC value%0A    # considering the soil moisture sensor is working at 5V%0A    voltage = (5 * adc) / 1024%0A%0A    return voltage%0A%0Aif __name__ == '__main__':%0A    # Report the channel 0 and channel 1 voltages to the terminal%0A    try:%0A        while True:%0A            adc_0 = get_adc(0)%0A            adc_1 = get_adc(1)%0A  
      
-%0A
      
-%0A
 print
- get_adc(0)%0Aprint get_adc(1
+(%22ADC Channel 0:%22, round(adc_0, 2), %22V ADC Channel 1:%22, round(adc_1, 2), %22V%22)%0A            time.sleep(0.2)%0A%0A    except KeyboardInterrupt:%0A        GPIO.cleanup(
 )%0A

Add Python3 example for MCP3002