# Analog Inputs for Raspberry Pi Using the MCP3008

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/001/187/medium800/raspberry_pi_twist.jpg?1396769834)

Teaching the Raspberry Pi how to read analog inputs is easier than you think! The Pi does not include a hardware [analog-to-digital converter](https://en.wikipedia.org/wiki/Analog-to-digital_converter), but an external ADC (such as the [MCP3008](https://www.adafruit.com/products/856)) can be used, along with some SPI code in Python to read external analog devices.

Here is a short list of some analog inputs that could be used with this setup:

- [potentiometer](http://www.adafruit.com/products/356)
- [photocell](http://www.adafruit.com/products/161)
- [force sensitive resistor (FSR)](http://www.adafruit.com/products/166)
- [temperature sensor](http://www.adafruit.com/products/165)
- [2-axis joystick](http://www.adafruit.com/products/512)

This guide uses a&nbsp;potentiometer to control the volume of an audio tone, but the code can be used as the basis for any kind of analog-input project.

# Analog Inputs for Raspberry Pi Using the MCP3008

## Connecting the Cobbler to a MCP3008

# To follow this tutorial you will need

- [MCP3008 DIP-package ADC converter chip](https://www.adafruit.com/products/856 "Link: https://www.adafruit.com/products/856")
- [10K trimer](https://www.adafruit.com/products/356 "Link: https://www.adafruit.com/products/356") [or panel mount potentiometer](https://www.adafruit.com/products/562)
- [Adafruit T-Cobbler Plus](https://www.adafruit.com/product/2028) or for an older 26-pin Pi an&nbsp;[Adafruit Pi Cobbler](https://www.adafruit.com/products/914 "Link: https://www.adafruit.com/products/914")
- [Full-size breadboard](https://www.adafruit.com/products/239)&nbsp;
- [Breadboarding wires](https://www.adafruit.com/category/82 "Link: https://www.adafruit.com/category/82")

And of course a working Raspberry Pi.

# Why we need an ADC

The Raspberry Pi computer does not have a way to read analog inputs. It's a digital-only computer. Compare this to the Arduino, AVR or PIC microcontrollers that often have 6 or more analog inputs! Analog inputs are handy because many sensors are analog outputs, so we need a way to make the Pi analog-friendly.  
  
We'll do that by wiring up an [MCP3008 chip](https://www.adafruit.com/products/856) to it. The [MCP3008](https://www.adafruit.com/products/856) acts like a "bridge" between digital and analog. It has 8 analog inputs and the Pi can query it using 4 digital pins. That makes it a perfect addition to the Pi for integrating simple sensors like [photocells](http://learn.adafruit.com/photocells),&nbsp;[FSRs](http://learn.adafruit.com/force-sensitive-resistor-fsr) or&nbsp;potentiometers, [thermistors](http://learn.adafruit.com/thermistor), etc.!  
  
 [Let's check the datasheet of the MCP3008 chip.](http://www.adafruit.com/datasheets/MCP3008.pdf "Link: http://www.adafruit.com/datasheets/MCP3008.pdf")&nbsp;On the first page in the lower right corner there's a pinout diagram showing the names of the pins:

![](https://cdn-learn.adafruit.com/assets/assets/000/001/222/medium800/raspberry_pi_mcp3008pin.gif?1447977143)

# Wiring Diagram

In order to read analog data we need to use the following pins:

**VDD** &nbsp;(power) and **DGND&nbsp;** (digital ground) to power the MCP3008 chip. We also need&nbsp;four "SPI" data&nbsp;pins:&nbsp; **DOUT** &nbsp;(Data Out from MCP3008),&nbsp; **CLK** &nbsp;(Clock pin),&nbsp; **DIN** &nbsp;(Data In from Raspberry Pi), &nbsp;and / **CS** &nbsp;(Chip Select). &nbsp;Finally of course, a source of analog data. We'll be using&nbsp;the basic&nbsp;10k trim pot.&nbsp;  
  
The MCP3008 has a few more pins we need to connect:&nbsp; **AGND** &nbsp;(analog ground, used sometimes in precision circuitry, which this is not) connects to&nbsp; **GND** , and&nbsp; **VREF** &nbsp;(analog voltage reference, used for changing the "scale" - we want the full scale, so tie it to&nbsp; **3.3V** ).  
  
Below is a wiring diagram. Connect the 3.3V cobbler pin to the left + rail and the GND pin to the right - rail. Connect the following pins for the MCP chip

- MCP3008 VDD -\> 3.3V (red)
- MCP3008 VREF -\> 3.3V&nbsp;(red)
- MCP3008 AGND -\> GND&nbsp;(black)
- MCP3008&nbsp;CLK -\> SCLK (yellow)
- MCP3008 DOUT -\> MISO (purple)
- MCP3008 DIN -\> MOSI (white)
- MCP3008 CS -\> #22 (green)
- MCP3008 DGND -\> GND&nbsp;(black)

Next connect up the potentiometer.

- Pin #1 (left) goes to 3.3v (red)
- Pin #2 (middle) connects to MCP3008&nbsp; **CH0** &nbsp;(analog input #0) with a purple wire
- Pin #3 (right) connects to GND (black)

Below we provide to wiring diagrams that will work with all versions of Raspberry Pi released so far (except the compute node which has no header). The first diagram is for the most recent Pi v3 and Pi Zero models which have a 40-pin GPIO header. The second is for the first two generations of Raspberry Pi which had a smaller 26-pin header. In both cases we are using the same GPIOs so the code will not be any different.&nbsp;

## T-Cobbler Plus Wiring 40-Pin Pi (v3, Zero)
![](https://cdn-learn.adafruit.com/assets/assets/000/071/321/medium800/learn_raspberry_pi_Analog_Inputs_for_Raspberry_Pi_Using_the_MCP3008-T-Cobbler-Plus_bb.png?1550268331)

## Pi Cobbler Wiring 26-Pin Pi (v1, v2)
![](https://cdn-learn.adafruit.com/assets/assets/000/071/320/medium800/learn_raspberry_pi_Analog_Inputs_for_Raspberry_Pi_Using_the_MCP3008-Pi-Cobbler_bb.png?1550268320)

# Analog Inputs for Raspberry Pi Using the MCP3008

## Necessary Packages

# Update Your Pi to the Latest Raspbian
Your Pi will need to be running the latest version of Raspbian. This tutorial was written using Raspbian Stretch (Nov. 2018). Checkout our guide for [Preparing an SD Card for your Raspberry Pi](https://learn.adafruit.com/adafruit-raspberry-pi-lesson-1-preparing-and-sd-card-for-your-raspberry-pi) if you have not done so already. After the installation is complete be sure and run the following commands to make sure your installation packages are up to date.&nbsp;

```auto
$ sudo apt-get update -y
$ sudo apt-get upgrade -y

```

[You will also need to enable SPI](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-spi)

# Install pip3
pip3 is already installed with a full Raspbian installation, but the Raspbian Lite does not include pip3 so it needs to be installed as shown below. We will use pip3 to install the necessary CircuitPython libraries.

```auto
$ sudo apt-get install python3-pip
```

# Install adafruit-blinka
```auto
$ sudo pip3 install adafruit-blinka

```

# Install mcp3008
```auto
$ sudo pip3 install adafruit-circuitpython-mcp3xxx
```

# Analog Inputs for Raspberry Pi Using the MCP3008

## Python Script

The following code can be downloaded directly to your Raspberry Pi. It will read the trimpot value, translate the reading to a volume range and modify the OS output volume level on your Raspberry Pi.&nbsp;

The remap\_range() method is being used to convert the 16-bit analog in range 0 - 65,535 to volume 0-100%.

# The Code
https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Analog_Inputs_for_Raspberry_Pi_Using_the_MCP3008/code.py

## Download the Code
Let's put this file right in your home directory for simplicity. The wget command makes things easy.

```auto
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Analog_Inputs_for_Raspberry_Pi_Using_the_MCP3008/code.py
```

# Analog Inputs for Raspberry Pi Using the MCP3008

## Run It

# Running the Code
The following command will start our volume control script. Adjusting the trimpot will printout the OS volume level on the screen.

```auto
$ sudo python3 ./code.py
```

## Testing the Volume Control
Opening a second terminal on your Raspberry Pi we can run the "speaker-test" command and listen to the volume change as we turn the trimpot.&nbsp;

```auto
$ speaker-test -t sine -f 440
```

## Controlling MP3 Volume
The mpg123 command can be used to play MP3 files on your Raspberry Pi. We can open an additional terminal and play MP3 files and adjust the volume with the trimpot we have wired up.

```auto
$ mpg123 <MP3 filename>
```


## Related Guides

- [MCP3008 - 8-Channel 10-Bit ADC With SPI Interface](https://learn.adafruit.com/mcp3008-spi-adc.md)
- [NeoPixel GoPro Lens Light](https://learn.adafruit.com/neopixel-gopro-lens-light.md)
- [Esenciales para CircuitPython](https://learn.adafruit.com/esenciales-para-circuitpython.md)
- [Quickstart IoT - Raspberry Pi Pico RP2040 with WiFi ](https://learn.adafruit.com/quickstart-rp2040-pico-with-wifi-and-circuitpython.md)
- [CircuitPython Libraries on Linux and ODROID C2](https://learn.adafruit.com/circuitpython-libaries-linux-odroid-c2.md)
- [Adafruit IO Basics: Analog Input](https://learn.adafruit.com/adafruit-io-basics-analog-input.md)
- [AdaBox 002](https://learn.adafruit.com/adabox002.md)
- [Wireless Dual Stepper Control with Adafruit IO, Raspberry Pi and Python](https://learn.adafruit.com/wireless-stepper-control-with-adafruit-io-circuitpython-raspberry-pi-python.md)
- [CircuitPython Libraries on Linux and Raspberry Pi](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux.md)
- [Adafruit IO Basics: Servo](https://learn.adafruit.com/adafruit-io-basics-servo.md)
- [NES Emulator for RP2040 & RP2350 DVI Boards](https://learn.adafruit.com/nes-emulator-for-rp2040-dvi-boards.md)
- [IoT Temperature Logger with Analog Devices ADT7410, Raspberry Pi, and Adafruit IO](https://learn.adafruit.com/iot-temperature-logger-with-python-and-adafruit-io.md)
- [CircuitPython Libraries on Linux and the NVIDIA Jetson Nano](https://learn.adafruit.com/circuitpython-libraries-on-linux-and-the-nvidia-jetson-nano.md)
- [Adafruit IO Basics: Digital Input](https://learn.adafruit.com/adafruit-io-basics-digital-input.md)
- [Raspberry Pi E-Ink Weather Station using Python](https://learn.adafruit.com/raspberry-pi-e-ink-weather-station-using-python.md)
- [Grand Central USB MIDI Controller in CircuitPython](https://learn.adafruit.com/grand-central-usb-midi-controller-in-circuitpython.md)
