Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Raspbery Pi Analog Input with ADC0832
#!/usr/bin/env python
#
# Analog Input with ADC0832 chip
#
# Datasheet: http://www.ti.com/lit/ds/symlink/adc0838-n.pdf
# Part of SunFounder LCD StarterKit
# http://www.sunfounder.com/index.php?c=show&id=21&model=LCD%20Starter%20Kit
#
import time
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
PIN_CLK = 18
PIN_DO = 27
PIN_DI = 22
PIN_CS = 17
# set up the SPI interface pins
GPIO.setup(PIN_DI, GPIO.OUT)
GPIO.setup(PIN_DO, GPIO.IN)
GPIO.setup(PIN_CLK, GPIO.OUT)
GPIO.setup(PIN_CS, GPIO.OUT)
# read SPI data from ADC8032
def getADC(channel):
# 1. CS LOW.
GPIO.output(PIN_CS, True) # clear last transmission
GPIO.output(PIN_CS, False) # bring CS low
# 2. Start clock
GPIO.output(PIN_CLK, False) # start clock low
# 3. Input MUX address
for i in [1,1,channel]: # start bit + mux assignment
if (i == 1):
GPIO.output(PIN_DI, True)
else:
GPIO.output(PIN_DI, False)
GPIO.output(PIN_CLK, True)
GPIO.output(PIN_CLK, False)
# 4. read 8 ADC bits
ad = 0
for i in range(8):
GPIO.output(PIN_CLK, True)
GPIO.output(PIN_CLK, False)
ad <<= 1 # shift bit
if (GPIO.input(PIN_DO)):
ad |= 0x1 # set first bit
# 5. reset
GPIO.output(PIN_CS, True)
return ad
if __name__ == "__main__":
while True:
print "ADC[0]: {}\t ADC[1]: {}".format(getADC(0), getADC(1))
time.sleep(1)

pelson commented Jan 29, 2015

Thanks for this code @HeinrichHartmann. I've had this successfully running a month ago, but it is no longer working for me. I'm wondering if it is my ADC chip that has burnt out. Could you do me a favour and tell me if connecting the Vcc to the 3v source, and GND to RPi's ground creates a circuit? For me, I'm getting 0 voltage through GND, which is leading me to believe my chip has broken (I have two, and both do the same thing though).

In looking at the ADC0832 datasheet I see there is a second set of 8 bits after the first set, why do you not read these?
The datasheet shows it taking about 20 clock cycles total to setup/read, are you using the CS line to 'reset' and not clock out the last 8 bits?
If so, why are you ignoring the last 8 bits??
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment