ADC (MCP3202) + Raspberry Pi 3

From ESE205 Wiki
Jump to navigation Jump to search

Circuitry/Wiring

MCP3202 ADC layout
  1. Orient the ADC in the right direction - The semicircle on the top of the ADC should face upwards. If it is flipped upside down, all the wiring will be wrong, and can be difficult to debug.
  2. Wire the SPI Pins - There are specific pins on the Pi 3 for SPI. The MCP3202 gets wired from the CLK to the SPI_CLK pin (23 on the Pi), the DOut to SPI_MOSI (19), DIn to SPI_MISO (21), and CS to SPI_CE0 (24).
  3. Hook up Voltage and Ground the ADC - There is a positive and negative end on the ADC for voltage. Wire the positive end, Vdd, to a 3v3 pin (1 or 17), and the negative end, Vss, to any Ground.
  4. Wire the Analog Input - Analog input goes into CH0 and CH1. If only one of the channels is needed, wire the other one up to a Ground.
Pin map of Raspberry Pi 3

Software/Coding

There is a great library to install and import from Adafruit made for the MCP3008 that will also work with the MCP3202. To install type the following into the command line:

sudo apt-get install git build-essential python-dev
cd ~
git clone https://github.com/adafruit/Adafruit_Python_MCP3008.git
cd Adafruit_Python_MCP3008
sudo python setup.py install

When programming with the ADC, a library such as bcm2835 can make it easy to manage the MCP3202, or simply:

import Adafruit_MCP3008

Then it is easy to read from the ADC. Configure it by setting an mcp object created with the correct pins:

# Software SPI configuration:
CLK  = 23
MISO = 21
MOSI = 19
CS   = 24
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)

Finally, use the mcp.read_adc(i) function, where i is which channel the ADC is reading from, to return a digital value.