MCP3008 Pinout: How to use this ADC Converter

Posted by

MCP3008 Overview and Key Specifications

The MCP3008 is a successive approximation ADC that provides 10-bit resolution, 8 input channels, and SPI serial interface. It operates from a single 2.7V to 5.5V supply.

Key features and specifications include:

  • 10-bit resolution
  • 8 single-ended input channels
  • Programmable on-chip low-pass filter
  • SPI serial interface (modes 0,0 and 1,1)
  • Single supply operation: 2.7V to 5.5V
  • 200 ksps max. sampling rate at 5V
  • ± 1 LSB max DNL, ± 1 LSB max INL
  • 74 dB SINAD, 50 dB THD

MCP3008 Pinout and Pin Functions

The MCP3008 comes in a 16-pin PDIP or SOIC package. Here is the pinout:

Pin Name Function
1 CH0 Analog Input Channel 0
2 CH1 Analog Input Channel 1
3 CH2 Analog Input Channel 2
4 CH3 Analog Input Channel 3
5 CH4 Analog Input Channel 4
6 CH5 Analog Input Channel 5
7 CH6 Analog Input Channel 6
8 CH7 Analog Input Channel 7
9 DGND Digital Ground
10 CS/SHDN Chip Select / Shutdown Input (Active-Low)
11 DIN Serial Data In
12 DOUT Serial Data Out
13 CLK Serial Clock Input
14 AGND Analog Ground
15 VREF Reference Voltage Input
16 VDD Power Supply

The key pins to interface the MCP3008 to a microcontroller are:

  • VDD: Power supply, typically 2.7-5.5V. Often connected to the microcontroller’s power pin.
  • VREF: Reference voltage for the ADC. Can be same as VDD or a different stable reference voltage not exceeding VDD.
  • AGND: Analog ground reference for analog input signals.
  • DGND: Digital ground reference for digital signals and power. Connect to microcontroller GND.
  • CH0-CH7: Analog input channels that can range from 0 to VREF.
  • CLK: SPI clock signal driven by the microcontroller, up to 3.6 MHz.
  • DIN: SPI data line for sending commands from microcontroller to MCP3008.
  • DOUT: SPI data line for receiving conversion data from MCP3008 to microcontroller.
  • CS/SHDN: Active-low chip select signal. Pull low to enable the chip and start a conversion.

Connecting MCP3008 to a Microcontroller

To connect the MCP3008 to a microcontroller, you’ll typically need to make the following connections:

  1. VDD to the microcontroller’s power supply (e.g. 3.3V or 5V)
  2. AGND and DGND to the microcontroller’s ground
  3. VREF to VDD (or a stable voltage reference if using an external reference)
  4. DIN to the microcontroller’s SPI MOSI pin
  5. DOUT to the microcontroller’s SPI MISO pin
  6. CLK to the microcontroller’s SPI SCK pin
  7. CS/SHDN to a digital output pin on the microcontroller
  8. CH0-CH7 to analog signals you want to measure (sensors, potentiometers, etc)

Here’s an example wiring diagram connecting the MCP3008 to an Arduino Uno:

MCP3008     Arduino Uno
--------    -----------
VDD         3.3V
VREF        3.3V  
AGND        GND
DGND        GND
CLK         Pin 13 (SCK)
DOUT        Pin 12 (MISO)  
DIN         Pin 11 (MOSI)
CS/SHDN     Pin 10 (SS)
CH0         Analog Input 0
CH1         Analog Input 1
...

Communicating with the MCP3008

Communication with the MCP3008 is done over SPI using the microcontroller as the master device. The basic process to perform an analog-to-digital conversion is:

  1. Pull the CS/SHDN line low to enable the ADC.
  2. Send a 3-byte command to configure the conversion:
  3. First byte: 0b000001XX where XX is the channel number (0-7)
  4. Second and third bytes: Don’t care, can be 0x00
  5. Read 3 bytes from the DOUT pin. The conversion result will be in the last 2 bytes.
  6. Pull CS/SHDN high to complete the conversion.

The 10-bit conversion result is spread across the 2nd and 3rd bytes read back over SPI (with some extra “don’t care” bits). To get the final result, you shift the 2nd byte left by 8 bits and OR it with the 3rd byte:

result = (byte2 << 8) | byte3

This gives you a 10-bit number between 0-1023 corresponding to the voltage on the selected input channel. To convert this to an actual voltage:

voltage = result * (VREF / 1024)

Example Arduino Code

Here’s a simple Arduino sketch that reads the voltage on channel 0 of the MCP3008 and prints it to the serial monitor:

#include <SPI.h>

const int CS_PIN = 10;
const int CLK_PIN = 13;
const int MOSI_PIN = 11;
const int MISO_PIN = 12;

void setup() {
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH);

  SPI.begin();
  SPI.setBitOrder(MSBFIRST);

  Serial.begin(9600);
}

void loop() {
  int adcValue = readMCP3008(0);
  float voltage = adcValue * (3.3 / 1024.0);

  Serial.print("ADC Value: ");
  Serial.print(adcValue);
  Serial.print("   Voltage: ");
  Serial.println(voltage);

  delay(500);
}

int readMCP3008(int channel) {
  digitalWrite(CS_PIN, LOW);

  SPI.transfer(0b00000001);
  int highByte = SPI.transfer((channel & 0x07) << 4);
  int lowByte = SPI.transfer(0);

  digitalWrite(CS_PIN, HIGH);

  return ((highByte & 0x03) << 8) | lowByte;
}

This code uses the Arduino SPI library to communicate with the MCP3008. The readMCP3008 function implements the SPI command sequence described earlier.

Example Raspberry Pi Code

Here’s a Python script for the Raspberry Pi that reads all 8 channels of the MCP3008 using the hardware SPI interface:

import spidev 
import time

spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 3600000

def readMCP3008(channel):
    adc = spi.xfer2([1, (8 + channel) << 4, 0])
    data = ((adc[1] & 3) << 8) + adc[2]
    return data

while True:
    for i in range(8):
        value = readMCP3008(i)
        voltage = value * (3.3 / 1024.0)
        print("CH%d: %4d/1023 => %5.3f V" % (i, value, voltage))

    print("")
    time.sleep(0.5)

This uses the spidev module to access the SPI bus. The readMCP3008 function sends the 3-byte command and reads back the 10-bit result.

Frequently Asked Questions (FAQ)

What is the difference between the MCP3008 and MCP3004?

The MCP3008 has 8 input channels while the MCP3004 has only 4 channels. Otherwise, they have the same basic functionality and pinout (with fewer analog inputs on the MCP3004).

Can I use the MCP3008 with 5V microcontrollers like the Arduino Uno?

Yes, the MCP3008 is 5V tolerant, so it can be used with 5V microcontrollers without level shifters. However, the analog input range will still be limited by the VREF voltage.

What is the maximum sampling rate of the MCP3008?

The maximum sampling rate depends on the clock frequency and supply voltage. At 5V with a 3.6 MHz clock, the max sampling rate is approximately 200,000 samples per second (200 ksps).

Can I use multiple MCP3008s with the same microcontroller?

Yes, by connecting each MCP3008 to a separate CS/SHDN pin on the microcontroller, you can control multiple MCP3008s on the same SPI bus. Just be sure to drive the appropriate CS pin low before communicating with each chip.

What happens if the input voltage exceeds VREF?

If the input voltage goes above VREF, the ADC will simply return its maximum value (1023 for the MCP3008). Voltages significantly beyond VREF can damage the chip, so it’s a good idea to use voltage dividers or op-amp circuits to keep the input within an appropriate range.

I hope this article has been a helpful introduction to using the MCP3008 ADC with microcontrollers! Let me know if you have any other questions.

Leave a Reply

Your email address will not be published. Required fields are marked *