MCP3008 - 8-Channel 10-Bit ADC With SPI Interface

You will be redirected back to this guide once you sign in, and can then subscribe to this guide.

Product guide

Python & CircuitPython

It's easy to use an MCP3008 8-channel ADC with Python or CircuitPython and the Adafruit CircuitPython MCP3xxx module. This module allows you to easily write Python code to add extra digital inputs and outputs.

You can use this ADC with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

Connect your MCP3008 to your CircuitPython board using a standard SPI connection.

Here's an example of wiring a MCP3008 to a Feather M0 board:

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.

Here's the Raspberry Pi wired to the MCP3008:

CircuitPython Installation of MCP3xxx Library

You'll need to install the Adafruit CircuitPython MCP3xxx library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle. Our CircuitPython starter guide has a great page on how to install the library bundle.

For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

Before continuing make sure your board's lib folder or root filesystem has the adafruit_mcp3xxx and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of MCP3xxx Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the device we'll initialize it and read the analog inputs from the Python REPL.

Run the following code to import the necessary modules, initialize the SPI connection, assign a chip select pin, and create the MCP3008 object:

import busio import digitalio import board import adafruit_mcp3xxx.mcp3008 as MCP from adafruit_mcp3xxx.analog_in import AnalogIn spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) cs = digitalio.DigitalInOut(board.D5) mcp = MCP.MCP3008(spi, cs)
import busio import digitalio import board import adafruit_mcp3xxx.mcp3008 as MCP from adafruit_mcp3xxx.analog_in import AnalogIn spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) cs = digitalio.DigitalInOut(board.D5) mcp = MCP.MCP3008(spi, cs)

Next, we'll create an analog input channel on the MCP3008 pin 0:

channel = AnalogIn(mcp, MCP.P0)
channel = AnalogIn(mcp, MCP.P0)

Now you're ready to read the raw ADC value and the channel voltage with the following properties:

For example, to print the raw ADC value and the channel voltage, run the following:

print('Raw ADC Value: ', channel.value) print('ADC Voltage: ' + str(channel.voltage) + 'V')
print('Raw ADC Value: ', channel.value) print('ADC Voltage: ' + str(channel.voltage) + 'V')

adafruit_products_MCP3008_REPL_print_output.png

You can run the same code in a loop. Then try rotating the potentiometer to see the values change.

import time while True: print('Raw ADC Value: ', channel.value) print('ADC Voltage: ' + str(channel.voltage) + 'V') time.sleep(0.5)
import time while True: print('Raw ADC Value: ', channel.value) print('ADC Voltage: ' + str(channel.voltage) + 'V') time.sleep(0.5)

adafruit_products_MCP3008_REPL_loop_output.png

Even though the MCP3008 is a 10-bit ADC, the value returned is a 16-bit number to provide a consistent interface across ADCs in CircuitPython

That's all there is to getting started with the MCP3008 and CircuitPython!

Full Example Code
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import busio import digitalio import board import adafruit_mcp3xxx.mcp3008 as MCP from adafruit_mcp3xxx.analog_in import AnalogIn # create the spi bus spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) # create the cs (chip select) cs = digitalio.DigitalInOut(board.D5) # create the mcp object mcp = MCP.MCP3008(spi, cs) # create an analog input channel on pin 0 chan = AnalogIn(mcp, MCP.P0) print("Raw ADC Value: ", chan.value) print("ADC Voltage: " + str(chan.voltage) + "V")
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import busio import digitalio import board import adafruit_mcp3xxx.mcp3008 as MCP from adafruit_mcp3xxx.analog_in import AnalogIn # create the spi bus spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) # create the cs (chip select) cs = digitalio.DigitalInOut(board.D5) # create the mcp object mcp = MCP.MCP3008(spi, cs) # create an analog input channel on pin 0 chan = AnalogIn(mcp, MCP.P0) print("Raw ADC Value: ", chan.value) print("ADC Voltage: " + str(chan.voltage) + "V")

Text editor powered by tinymce.