Difference between revisions of "ADC (MCP3002) + Raspberry Pi 2"

From ESE205 Wiki
Jump to navigation Jump to search
Line 6: Line 6:
 
[[File:ADC schem.png|200px|thumb|left|Connections from the ADC to the Raspberry Pi, ignore pin layout of cobbler and use pin layout of diagram above.]]
 
[[File:ADC schem.png|200px|thumb|left|Connections from the ADC to the Raspberry Pi, ignore pin layout of cobbler and use pin layout of diagram above.]]
 
Make sure that whatever analog device you are using is hooked up to the right pin of the ADC and that it has resistors to prevent damaged circuitry.
 
Make sure that whatever analog device you are using is hooked up to the right pin of the ADC and that it has resistors to prevent damaged circuitry.
 
+
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 +
<br>
 
=Programming the ADC=
 
=Programming the ADC=
 
+
'''Install spidev using the follow terminal commands:'''
 +
<blockquote>
 +
<code>sudo apt-get install python-dev python-pip</code><br>
 +
<code>sudo pip install python</code><br>
 +
<code>sudo mod probe spi_bcm2708</code><br>
 +
<code>sudo pip install spidev</code><br>
 +
<code>echo spi_bcm2708 | sudo tee -a /etc/modules</code><br>
 +
</blockquote>
 +
'''Use read() method to read data from the ADC into python'''
 +
<source lang="python">
 +
def read(adc_channel=0, spi_channel=0):
 +
    spi = spidev.SpiDev()
 +
    spi.open(0, spi_channel)
 +
    spi.max_speed_hz = 1200000 # 1.2 MHz
 +
    cmd = 128
 +
    if adc_channel:
 +
        cmd += 32
 +
    reply_bytes = spi.xfer2([cmd, 0])
 +
    reply_bitstring = ''.join(bitstring(n) for n in reply_bytes)
 +
    reply = reply_bitstring[5:15]
 +
    spi.close()
 +
    return int(reply, 2) / 2**10
 +
</source>
  
  

Revision as of 23:01, 3 May 2016

Wiring the ADC

Raspberry Pi 2 Model B pin layout
  1. Locate SPI pins on the Raspberry Pi - In order to properly locate pins on the ADC you must be looking at it right side up. There is a small divet in the top and while that is facing up you may follow the visuals posted here. For the Serial Peripheral Device hookup you will require four SPI pins MOSI, MISO, and SCLK pins for timing and general communication between the ADC and Pi board, and the CS0 pin for receiving the data. Follow the diagram to connect correctly to the board.
  2. Hook up to power - You man hook up the voltage to a 3.3V pin as long as your data sheet does not say otherwise, and the ground to a GND pin for general power.
  3. Wire up analog input - To read analog data through the ADC you have to connect CH0 to your analog source. We only used CH0 so we connected CH1 to the ground as to not confuse data. For the analog photocell input we built a voltage divider to pull up the analog signal when receiving light. We matched the resistance of the voltage divider with the theoretical minimum voltage of the photocell.
Connections from the ADC to the Raspberry Pi, ignore pin layout of cobbler and use pin layout of diagram above.

Make sure that whatever analog device you are using is hooked up to the right pin of the ADC and that it has resistors to prevent damaged circuitry.










Programming the ADC

Install spidev using the follow terminal commands:

sudo apt-get install python-dev python-pip
sudo pip install python
sudo mod probe spi_bcm2708
sudo pip install spidev
echo spi_bcm2708 | sudo tee -a /etc/modules

Use read() method to read data from the ADC into python

def read(adc_channel=0, spi_channel=0):
    spi = spidev.SpiDev()
    spi.open(0, spi_channel)
    spi.max_speed_hz = 1200000 # 1.2 MHz
    cmd = 128
    if adc_channel:
        cmd += 32
    reply_bytes = spi.xfer2([cmd, 0])
    reply_bitstring = ''.join(bitstring(n) for n in reply_bytes)
    reply = reply_bitstring[5:15]
    spi.close()
    return int(reply, 2) / 2**10