Contributors

Overview

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, but an external ADC (such as the MCP3008) can be used, along with some bit banged 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:

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 to it. The MCP3008 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, FSRs or potentiometers, thermistors, etc.!

Wiring Diagram

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

VDD (power) and DGND (digital ground) to power the MCP3008 chip. We also need four "SPI" data pins: DOUT (Data Out from MCP3008), CLK (Clock pin), DIN (Data In from Raspberry Pi), and /CS (Chip Select). Finally of course, a source of analog data. We'll be using the basic 10k trim pot.

The MCP3008 has a few more pins we need to connect: AGND (analog ground, used sometimes in precision circuitry, which this is not) connects to GND, and VREF (analog voltage reference, used for changing the "scale" - we want the full scale, so tie it to 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

Advanced users may note that the Raspberry Pi does have a hardware SPI interface (the Cobbler pins are labeled MISO/MOSI/SCLK/CE0/CE1). The hardware SPI interface is super fast but not included in all distributions. For that reason we are using a bit banged SPI implementation so the SPI pins can be any of the Raspberry Pi's GPIOs (assuming you update the script).

Here's a Fritzing sketch of the Cobbler Plus version for Model B+ / Pi 2 (click for a bigger image):

Python Script

This ~100 line python script can be pasted into an editor and saved on your raspberry pi. You can also grab it directly from the pi if it's connected to the Internet by running git clone git://gist.github.com/3151375.git

The script is fairly simple. Half of the code (the readadc function) is a function that will 'talk' to the MCP3008 chip using four digital pins to 'bit bang' the SPI interface (this is because not all Raspberry Pi's have the hardware SPI function).

The MCP3008 is a 10-bit ADC. That means it will read a value from 0 to 1023 (210 = 1024 values) where 0 is the same as "ground" and "1023" is the same as "3.3 volts". We don't convert the number to voltage, although its easy to do that by multiplying the number by (3.3 / 1023).

We check to see if the pot was turned more than 5 counts - this keeps us from being too "jittery" and resetting the volume too often.

The raw analog count number is then converted into a volume percentage of 0%-100%. When the trimpot is turned up or down it will print the volume level to STDOUT and adjust the audio level of the playing file by telling the mixer to adjust the volume.

OUT OF STOCK NOTIFICATION

YOUR NAME

YOUR EMAIL

You have been successfully subscribed to the Notification List for this product and will therefore receive an e-mail from us when it is back in stock!

For security reasons, an e-mail has been sent to you acknowledging your subscription. Please remember that this subscription will not result in you receiving any e-mail from us about anything other than the restocking of this item.

If, for any reason, you would like to unsubscribe from the Notification List for this product you will find details of how to do so in the e-mail that has just been sent to you!