title: RaspBerry pi B rev2 - Issue while sampling a LM335 (temp. sensor) thru a MCP3208 ADC via SPI in Python 3
body: <p>I tried to interface a RaspBerry pi with a LM335 temperature sensor this week-end. I'm using a MCP 3208 micro controller (channel 0) to interface the sensor. My goal is to collect samples data in SPI mode with python 3 scripts (classes).
  I've checked the wiring and everything seems OK for me, I'but I'am a beginner, not really aware of Electronic concepts.
  On the software side , I've installed quick2wire that claims to be <strong>python 3</strong> compatible. In fact I want to lead the micro-controller with Python 3 API's (not thru shell calls) </p>
  
  <h2>Components</h2>
  
  <ul>
  <li><p>Raspberry pi REV2 model B with Rasbian-wheezy / Quick2wire installed. <em>/dev/spix.y devices are listed</em>.</p></li>
  <li><p>MCP3208 ADC : 12 bits ADC / SPI. I'm using CS0 from the GPIO. The sensor is connected to channel 0 (<strong>B</strong>). <a href="http://ww1.microchip.com/downloads/en/DeviceDoc/21298c.pdf" rel="nofollow noreferrer">see datasheet</a>.</p></li>
  <li><p>LM335 : temperature sensor. Outputs 10mV / °K. Min 5muA / Max 5 mA. It's connected to the MCP3208 channel #0 (<strong>A</strong>). <a href="http://pdf.datasheetcatalog.com/datasheet/nationalsemiconductor/DS005698.PDF" rel="nofollow noreferrer">see datasheet</a></p></li>
  <li><p>220 ohms resistor (<strong>C</strong>). set up regarding LM335 outputs and desired temperature range coverage with my own calculations : May be a problem ...</p></li>
  </ul>
  
  <h2>Schematics extract</h2>
  
  <p>The LM335 (zener diode like) is connected as :</p>
  
  <p><img src="https://i.stack.imgur.com/zZNrn.jpg" alt="LM335 / MCP3208 channel wiring"></p>
  
  <h2>Wiring</h2>
  
  <p>Components are wired as shown bellow. Note that the yellow link is connected behind the cobbler kit on the CS0 SPI channel.</p>
  
  <p><img src="https://i.stack.imgur.com/PfZyO.jpg" alt="Using a pi cobbler kit"></p>
  
  <h2>Quick2wire</h2>
  
  <p>I use the bellow script to query the CS0/Channel 0 GPIO interface. Unfortunately, I've not found usefull informations on the quick2wire-python-api API's. I've just copy/paste an example found as it was written in the same goal. I'm not sure if it really works :</p>
  
  <pre><code>#!/usr/bin/env python3
  
  from quick2wire.spi import *
  import sys, time
  
  try:
          channel = int(sys.argv[1])
  except:
          channel = 0
  
  MCP3208 = SPIDevice(channel, 0)
  
  while True:
          try:
                  response = MCP3208.transaction(writing_bytes(0x41, 0x13), reading(1))
                  print ("output = %i" % ord(response[0]))
                  time.sleep(1)
          except KeyboardInterrupt:
                  break
  </code></pre>
  
  <p>The script outputs :</p>
  
  <pre><code>output = 0 
  output = 0 
  output = 0 
  output = 0 
  output = 0 
   ....
  </code></pre>
  
  <p>The result is the same with the channel 1 ( with argv = 1)</p>
  
  <p>As the MCP3208 Din (probe output) receives  voltage (see bellow) quick2wire should read at 18°C (rawghly my home inside temperature today)</p>
  
  <ul>
  <li>3,3 V / 2^12 = 805 muA as I understand as "<em>digital step</em>"</li>
  <li>18°C + 273°C = 291 => 2,91 V on the micro controller <strong>Din</strong> pin</li>
  <li>and then return 2 910 / 0.805 = <strong>3 615</strong></li>
  </ul>
  
  <p>Am I wrong ?</p>
  
  <h2>Controls</h2>
  
  <p>I've no oscilloscope, the only measures I can read are :</p>
  
  <ul>
  <li>Voltage is 2.529 V at <strong>B</strong> checkpoint and 0,5 V (+/-5%) on the other MCP3208 channels</li>
  </ul>
  
  <p>Note : the adjust pin is not used on the LM335 so results way not be accurate but voltage is here !</p>
  
  <p>Seems to be a problem on the quick2wire side I think. But which ?</p>
  
  <h2>Code</h2>
  
  <p>The quick2wire.spi.SPIDevice class lakes of détails on the transfers parameter in terms of structure, content and output response format. </p>
  
  <pre><code>def transaction(self, *transfers):
      """
      Perform an SPI I/O transaction.
  
      Arguments:
      *transfers -- SPI transfer requests created by one of the reading,
                    writing, writing_bytes, duplex or duplex_bytes 
                    functions.
  
      Returns: a list of byte sequences, one for each read or duplex
               operation performed.
      """
      transfer_count = len(transfers)
      ioctl_arg = (spi_ioc_transfer*transfer_count)()
  
      # populate array from transfers
      for i, transfer in enumerate(transfers):
          ioctl_arg[i] = transfers[i].to_spi_ioc_transfer()
  
      ioctl(self.fd, SPI_IOC_MESSAGE(transfer_count), addressof(ioctl_arg))
  
      return [transfer.to_read_bytes() for t in transfers if t.has_read_buf]
  </code></pre>
  
  <p><em>Another question :</em></p>
  
  <ul>
  <li>how to set SPI configuration values like mode, clock speed, bits per word, LSB ... and so on.</li>
  </ul>
  
  <p><strong>Thanks in advance for your help</strong>.</p>