Skip to content

Reading ADC channels using Raspberry Pi SPI interface #3

@ghost

Description

I am trying to read ADC channel 0 using SPI bus on Raspberry Pi and constantly outputting a 0.
Not sure why it is not working.
EDIT: I added sleep and wake up functions to try to get out of automatic sleep mode. It still seems to be outputting only 0.
Here is my updated code:

#!/usr/bin/python
import time
import sys
import spidev
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
spi=spidev.SpiDev()
SCLK=23
CS=26
MISO=21
spi.open(0,1)
spi.max_speed_hz = 4000000
spi.mode = 0
buff = 0
LTC_CONFIG_SLEEP = 1
currentLTCconfig = None

def Sleep():
  currentLTCconfig |= 0x01
  spi.xfer(currentLTCconfig)
  spi.xfer(0)    

def wake():
  wasSleep = currentLTCconfig & bin(LTC_CONFIG_SLEEP)
  currentLTCconfig &= ~bin(LTC_CONFIG_SLEEP)
  spi.xfer(currentLTCconfig)
  spi.xfer(0)
  if(wasSleep):
      delay(70)

def readADC(clkPin,csPin,misoPin,currentLTCconfig):
  buff=spi.xfer2([currentLTCconfig])
  newbuff = spi.xfer2([0])
  for y in buff:
      buff[y]<<8
      buff[y] |= 0xFF & (newbuff[y]>> 4) 
  return buff

if __name__=='__main__':
  try:
      while True:
          currentLTCconfig = 0x80
          val=readADC(SCLK,CS,MISO,currentLTCconfig)
          volt=val
          print('ADC value', str(volt))
  except KeyboardInterrupt:
          GPIO.cleanup()
          spi.close()
          sys.exit(0)
  #########################

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions