Using 4-CHANNEL 16-BIT 0-10V ANALOG TO DIGITAL CONVERTER with Raspberry Pi

So I have connected my 4-CHANNEL 16-BIT 0-10V ANALOG TO DIGITAL CONVERTER to my raspberry pi with the i2c shield and cable, verifying connections here. The problem is the code does not seem to be getting any readings from the ADC. I see the ADC on pi with i2cdetect at address 68, and I have a variable power supply hooked up to the ADC just to ensure proper voltage readings prior to using a sensor. I’m now suspecting the code I got here for the MCP3428. Here is the code:

indent preformatted text by 4 spaces
'''**************************************************************************/
I2C 16-Bit 4-Channel Analog to Digital Converter I2C Mini Module
Firmware v1.0 - Python
Author - Yadwinder Singh
4-Channel ADC with 16-Bit Resolution
4 Differential Analog Inputs
Programmable x1-x8 Gain Amplifier
Upto 8 Devices per I2C Port
Up to 3.4MHz Communication Speed
0x68 I2C Start Address 
Hardware Version - Rev A.
Platform - Raspberry Pi
/**************************************************************************/'''
#Setting Up smbus libraries
import smbus
import time
bus = smbus.SMBus(1)



#MCP3426, MCP3427 & MCP3428 addresses are controlled by address lines A0 and A1
# each address line can be low (GND), high (VCC) or floating (FLT)
MCP3428_DEFAULT_ADDRESS			= 0x68
MCP3428_CONF_A0GND_A1GND		= 0x68
MCP3428_CONF_A0GND_A1FLT		= 0x69
MCP3428_CONF_A0GND_A1VCC		= 0x6A
MCP3428_CONF_A0FLT_A1GND		= 0x6B
MCP3428_CONF_A0VCC_A1GND		= 0x6C
MCP3428_CONF_A0VCC_A1FLT		= 0x6D
MCP3428_CONF_A0VCC_A1VCC		= 0x6E
MCP3428_CONF_A0FLT_A1VCC		= 0x6F

# /RDY bit definition
MCP3428_CONF_NO_EFFECT			= 0x00
MCP3428_CONF_RDY			= 0x80

# Conversion mode definitions
MCP3428_CONF_MODE_ONESHOT		= 0x00
MCP3428_CONF_MODE_CONTINUOUS		= 0x10

# Channel definitions
#MCP3425 have only the one channel
#MCP3426 & MCP3427 have two channels and treat 3 & 4 as repeats of 1 & 2 respectively
#MCP3428 have all four channels
MCP3428_CONF_CHANNEL_1			= 0x00
MCP3428_CHANNEL_2			= 0x20
MCP3428_CHANNEL_3			= 0x40
MCP3428_CHANNEL_4			= 0x60


# Sample size definitions - these also affect the sampling rate
# 12-bit has a max sample rate of 240sps
# 14-bit has a max sample rate of  60sps
# 16-bit has a max sample rate of  15sps
MCP3428_CONF_SIZE_12BIT			= 0x00
MCP3428_CONF_SIZE_14BIT			= 0x04
MCP3428_CONF_SIZE_16BIT			= 0x08
MCP3428_CONF_SIZE_18BIT			= 0x0C

# Programmable Gain definitions
MCP3428_CONF_GAIN_1X			= 0x00
MCP3428_CONF_GAIN_2X			= 0x01
MCP3428_CONF_GAIN_4X			= 0x02
MCP3428_CONF_GAIN_8X			= 0x03

 #Default values for the sensor
ready = MCP3428_CONF_RDY
channel = MCP3428_CONF_CHANNEL_1
mode = MCP3428_CONF_MODE_CONTINUOUS
rate = MCP3428_CONF_SIZE_12BIT
gain = MCP3428_CONF_GAIN_1X
VRef = 2.048 # 2.048 Volts


# Power on and prepare for general usage.
def initialise():
# Default :Channel 1,Sample Rate 15SPS(16- bit),Gain x1 Selected

setRate(ready)
setChannel(channel)
setMode(mode)
setSample(rate)
setGain(gain)


# Set Ready Bit 
#In read mode ,it indicates the output register has been updated with a new conversion.
#In one-shot Conversion mode,writing Initiates a new conversion.
def setRate(ready) :

	bus.write_byte(MCP3428_DEFAULT_ADDRESS, ready)
	

#Set Channel Selection
#C1-C0: Channel Selection Bits
#00 = Select Channel 1 (Default)
#01 = Select Channel 2
#10 = Select Channel 3 
#11 = Select Channel 4 
def setChannel(channel) :

bus.write_byte(MCP3428_DEFAULT_ADDRESS,channel)


#Set Conversion Mode
#1= Continous Conversion Mode
#0 = One-shot Conversion Mode
def setMode(mode) :

bus.write_byte(MCP3428_DEFAULT_ADDRESS,mode)

#Set Sample rate selection bit
# 00 : 240 SPS-12 bits
# 01 : 60 SPS 14 bits
# 10 : 15 SPS 16 bits
def setSample(rate) :

bus.write_byte(MCP3428_DEFAULT_ADDRESS,rate)

#Set the PGA gain
# 00 : 1 V/V
# 01 : 2 V/V
# 10 : 4 V/V
# 11 : 8 V/V
def setGain(gain) :

bus.write_byte(MCP3428_DEFAULT_ADDRESS,gain)    

#Get the measurement for the ADC values  from the register
#using the General Calling method

def getadcread() :

data = bus.read_i2c_block_data(MCP3428_DEFAULT_ADDRESS,0x00,2)
value = ((data[0] << 8) | data[1])
if (value >= 32768):
	value = 65536 - value
return value

# The output code is proportional to the voltage difference b/w two analog points
#Checking the conversion value
#Conversion of the raw data into 
# Shows the output codes of input level using 16-bit conversion mode

def getconvert():

code = getadcread()
#setSample(rate)
N = 12 # resolution,number of bits
voltage = VRef*code*0.00034
#voltage = (2 * VRef* code)/ (2**N)
return voltage

#Initialising the Device.
initialise()

voltage = 0.00

while True:

time.sleep(0.5)
voltage = getconvert()
print ("		MCP3428 Readings ")
print ("\nVoltage of the source is :",voltage,"volts\n")
print ("		**********************************\n")

Hi,
try this lib and print the raw adc value.


the values should change as you change the input voltage.

make sure the polarity of the input voltage is correct.
Thanks

Hi we made sure the other channels were consistently 0 and then modified your code to only print channel 1, we also had to modify some of the print commands, as we are running on python 3. the issue is that the readings fluctuate but are never what they should be. Here is the values of voltage printed going from 0 to 5 volts:

channel 1:
0.991144
channel 1:
0.875336
channel 1:
1.076768
channel 1:
1.707552
channel 1:
2.75352
channel 1:
2.11904
channel 1:
1.815968
channel 1:
2.179408
channel 1:
2.2256080000000003
channel 1:
1.843688
channel 1:
2.021712
channel 1:
3.823512
channel 1:
3.4329680000000002
channel 1:
3.230304
channel 1:
3.707704
channel 1:
3.739736
channel 1:
3.30792
channel 1:
4.956336
channel 1:
5.49164
channel 1:
5.63024
channel 1:
5.643792
channel 1:
6.891192
channel 1:
7.349496
channel 1:
7.338408
channel 1:
7.32732
channel 1:
7.345184
channel 1:
7.988288
channel 1:
8.723176
channel 1:
8.883336
channel 1:
9.086
channel 1:
9.18764
channel 1:
9.155608
channel 1:
9.185792
channel 1:
8.937544
channel 1:
8.958488000000001
channel 1:
8.968960000000001
channel 1:
8.798944

it wont show correct because the code was written for 0-20V ADC and you have a 0-10V.
You will need to change this formula
voltage = (raw_adc * 0.000616)
this is how you can get the calculation formula
use your meter to measure the input voltage, lets say its readings 5V
and now print the raw_adc values , lets say is "x"
so to calculate the voltage the multiplication factor will be = x/5

Thanks

Thank you so much for your help, using a factor of .000336 seems to be given fairly accurate reading, with some fluctuations. This code is written to use a 16-bit resolution correct?

yes, its for 16bit.
You can change to 12bit and it will reduce the fluctuations.