saphy721
Posts: 2
Joined: Thu Jun 12, 2014 2:23 pm

I2C Photodiodes

Thu Jun 12, 2014 2:43 pm

I'm trying to read photodiodes off of an ADC that uses I2C with python. My co-worker wrote the code for it and there's a problem that I have to fix, but I'm pretty new to programming and I'm not sure where to start debugging. Any advice would be helpful. This is the code:

Code: Select all

import threading
import logging 
import Queue
import smbus

class Bus():

   def_init__(self, t):
      self.bus = smbus.SMBus(1)
      if t is 't': self.addr = 0b0110100
      if t is 'p': self.addr = 0b001011
      self.main = 0b10100000
      self.backup = 0b10100001
      self.current = 1
      self.cmd = self.main
      self.lock = threading.Lock()
      logging.info("Bus established")

   def switch (self): #to switch between primary and backup diodes
      if self.current ==0:
         self.current = 1
         cmd = self.backup
      elif self.current == 1:
         self.current = 0
         cmd = self.main
      with self.lock:
         self.bus.write_byte(self.addr, cmd)

   def read(self): #to read the difference between the two photodiodes
      with self.lock
         d0 = self.bus.read_byte(self.addr)
         d1 = self.bus.read_byte(self.addr)
         d2 = self.bus.read_byte(self.addr)
      d = d0<<16 + d1<<8 +d2
      d = (pos >>6)
      d &= 0xFFFF
      return d
and this is how I call it:

Code: Select all

myBus = Bus("t")
theta_reading = myBus.read()
print theta_reading
This is the ADC I'm using: http://www.linear.com/product/LTC2489

I put in some print statements to try to debug it and it looks like it stops after it reads d0.

Help?
Last edited by saphy721 on Thu Jun 12, 2014 4:09 pm, edited 1 time in total.

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: I2C Photodiodes

Thu Jun 12, 2014 4:00 pm

It would help if you let everyone know what ADC chip you are using (there are 100's of them)

Also repost and put your code in CODE blocks to look like this

Code: Select all

example code block
      another indented line
then all the indents come through so we can understand your code
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

saphy721
Posts: 2
Joined: Thu Jun 12, 2014 2:23 pm

Re: I2C Photodiodes

Thu Jun 12, 2014 4:09 pm

Oh ok I'll change that, thanks!

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: I2C Photodiodes

Thu Jun 12, 2014 5:48 pm

Considering we have no circuit for this I don't know if you have wired this up correctly. I have no idea why you need an ADC with differential mode inputs on photodiodes.

Right assuming
  1. Correct reference source has been attached
  2. the device is actually powered (check resistance with power off to power and gnd pins not pads from Pi)
  3. The device is correctly powered read the power between gnd on device and its supply pins
  4. The address selection is set correctly (measure pins of device to what they are supposed to be connected to using resistance with power off)
  5. You are using internal clock mode
  6. You actually have connections to I2C
What does the following command produce

Code: Select all

i2cdetect -y 1
Next your code has many problems I would suggest reading the datasheet thoroughly
  1. Your device addresses are wrong (see table4 on page 14 of datasheet and match them to what the address pins have been set to
  2. The device takes a reading and goes to sleep a read will give you a reading from after power up or last read or write operation, so a read you are doing could be from minutes ago if nothing else your read function should write channel to convert, wait about 100ms, then read to get current reading
I would expect in python the following

Code: Select all

      self.bus = smbus.SMBus(1)
      if t is 't': self.addr = 0b0110100
      if t is 'p': self.addr = 0b001011
To be written as

Code: Select all

      self.bus = smbus.SMBus(1)
      if t is 't':
            self.addr = 0b0110100
      if t is 'p':
            self.addr = 0b001011
But using correct values for the binary values ONLY the first of those is valid according to the datasheet for the device you have linked to.

Then tell us what exact problem you are getting, not I need to fix it, without any other clues.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

skycoop
Posts: 4
Joined: Thu Jun 12, 2014 9:24 pm

Re: I2C Photodiodes

Thu Jun 12, 2014 9:41 pm

So I am the mysterious coworker responsible for the code and the circuit that it is responsible for managing. I actually stumbled across this thread while trying to debug the problem, and it seems like you guys could help me figure out the problem.
The goal of this system is to get the voltage difference between two different photodiodes with relative speed, which is why we have a ADC capable of differential measurements.
I've partway traced back the issue in the code. I was expecting 3 bytes but one of them is completely missing. The only data I have been able to get from the ADC is 0b11000000 and 0b1. According to the datasheet, this would imply that the voltage difference between the diodes is greater than 2.5V, since SIG and MSB are both 1. This makes no sense, because when I measure it with a multimeter, the difference is more on the scale of 200mV (they're sitting flat under a light). On top of that, it's always supposed to send 3 bytes with the last 6 bits being 0's, but I am getting significantly less than 3 bytes with a last bit of 1. I think I might be reading the bits backwards, but I'm not at all sure....

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: I2C Photodiodes

Fri Jun 13, 2014 12:21 am

skycoop wrote:So I am the mysterious coworker responsible for the code and the circuit that it is responsible for managing. I actually stumbled across this thread while trying to debug the problem, and it seems like you guys could help me figure out the problem.
The goal of this system is to get the voltage difference between two different photodiodes with relative speed, which is why we have a ADC capable of differential measurements.
I've partway traced back the issue in the code. I was expecting 3 bytes but one of them is completely missing. The only data I have been able to get from the ADC is 0b11000000 and 0b1. According to the datasheet, this would imply that the voltage difference between the diodes is greater than 2.5V, since SIG and MSB are both 1. This makes no sense, because when I measure it with a multimeter, the difference is more on the scale of 200mV (they're sitting flat under a light). On top of that, it's always supposed to send 3 bytes with the last 6 bits being 0's, but I am getting significantly less than 3 bytes with a last bit of 1. I think I might be reading the bits backwards, but I'm not at all sure....
Possible cause EITHER of the Vin pins is greater than FS (or 0.5 for each end as the bias point is 0.5 * Vref), we don't know how you have connected Vref.

Check groundings of all parts including link to the photodiodes as you may have 200mV between them but be floating many volts above the ADC GND.

What power is being used on the ADC? What decouplers do you have on power and Vref (both inputs)?
Vref+ cannot be higher than Vcc and Vref- cannot be lower than GND

Remember that the Pi only accepts 3V3 input levels on I2C and if your device is using 5V do NOT use any pullups to 5V, the device excepts a high (minimum) to be 0.7 * Vcc if 5V that is 3V5 which is greater than the the Pi will produce so you need a level translater for the I2C. Also check you have no series resistance on the I2C lines as you may not be seeing ACK on I2C during transactions as that has to be BELOW 0V4 at the ADC.

You are probably only getting one byte and not checking errors on each byte as the ACK may not be happening. So ADC aborts the I2C transaction or is actually sittng waiting for I2C transaction to complete.

Your problems seem first of all to be voltage and matching interface levels digital and possibly analog

Without a lot more circuit details and a detailed scope trace of the transaction on I2C that is all I can say without a lot more detail.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

Return to “Interfacing (DSI, CSI, I2C, etc.)”